Write and commit the README

This commit is contained in:
joshaber 2016-12-07 14:27:24 -05:00
parent b605fb01f7
commit 108bcc9a51
2 changed files with 46 additions and 4 deletions

View file

@ -5,13 +5,14 @@ import * as OS from 'os'
import * as FS from 'fs'
import { Dispatcher } from '../../lib/dispatcher'
import { initGitRepository } from '../../lib/git'
import { initGitRepository, createCommit, getStatus } from '../../lib/git'
import { sanitizedRepositoryName } from './sanitized-repository-name'
import { Form } from '../lib/form'
import { TextBox } from '../lib/text-box'
import { Button } from '../lib/button'
import { Row } from '../lib/row'
import { Checkbox, CheckboxValue } from '../lib/checkbox'
import { writeDefaultReadme } from './write-default-readme'
interface ICreateRepositoryProps {
readonly dispatcher: Dispatcher
@ -20,6 +21,8 @@ interface ICreateRepositoryProps {
interface ICreateRepositoryState {
readonly path: string
readonly name: string
/** Should the repository be created with a default README? */
readonly createWithReadme: boolean
}
@ -74,11 +77,25 @@ export class CreateRepository extends React.Component<ICreateRepositoryProps, IC
await initGitRepository(fullPath)
const repositories = await this.props.dispatcher.addRepositories([ fullPath ])
if (repositories.length < 1) { return }
if (repositories.length > 0) {
this.props.dispatcher.selectRepository(repositories[0])
this.props.dispatcher.closePopup()
const repository = repositories[0]
if (this.state.createWithReadme) {
try {
await writeDefaultReadme(fullPath, this.state.name)
const status = await getStatus(repository)
const wd = status.workingDirectory
await createCommit(repository, 'Initial commit', wd.files)
} catch (e) {
console.error('Error writing & committing the default README:')
console.error(e)
}
}
this.props.dispatcher.selectRepository(repository)
this.props.dispatcher.closePopup()
})
})
}

View file

@ -0,0 +1,25 @@
import * as FS from 'fs'
import * as Path from 'path'
const DefaultReadmeName = 'README.md'
function defaultReadmeContents(name: string): string {
return `# ${name}\n`
}
/**
* Write the default README to the repository with the given name at the path.
*/
export function writeDefaultReadme(path: string, name: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
const fullPath = Path.join(path, DefaultReadmeName)
const contents = defaultReadmeContents(name)
FS.writeFile(fullPath, contents, err => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
}