mirror of
https://github.com/desktop/desktop
synced 2024-08-25 03:07:41 +00:00
Write and commit the README
This commit is contained in:
parent
b605fb01f7
commit
108bcc9a51
|
@ -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()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
25
app/src/ui/add-repository/write-default-readme.ts
Normal file
25
app/src/ui/add-repository/write-default-readme.ts
Normal 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()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue