feat(cli): skip git if already in a git repo, add --skipGit option

close #967
This commit is contained in:
Evan You
2018-04-27 12:03:23 -04:00
parent a0a7dc6c03
commit 23480ae8f5
2 changed files with 32 additions and 8 deletions

View File

@@ -39,9 +39,10 @@ program
.option('-p, --preset <presetName>', 'Skip prompts and use saved or remote preset')
.option('-d, --default', 'Skip prompts and use default preset')
.option('-i, --inlinePreset <json>', 'Skip prompts and use inline JSON string as preset')
.option('-g, --initialCommit <message>', 'Specify initial commit message (when git is available)')
.option('-m, --packageManager <command>', 'Use specified npm client when installing dependencies')
.option('-r, --registry <url>', 'Use specified npm registry when installing dependencies (only for npm)')
.option('-s, --skipGit', 'Do not setup git repository when creating project')
.option('-g, --git <message>', 'Specify initial commit message (when git is available)')
.option('-f, --force', 'Overwrite target directory if it exists')
.option('-c, --clone', 'Use git clone when fetching remote preset')
.action((name, cmd) => {

View File

@@ -46,17 +46,15 @@ module.exports = class Creator {
this.promptCompleteCbs = []
this.createCompleteCbs = []
this.run = this.run.bind(this)
const promptAPI = new PromptModuleAPI(this)
promptModules.forEach(m => m(promptAPI))
}
async create (cliOptions = {}) {
const isTestOrDebug = process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG
const { name, context, createCompleteCbs } = this
const run = (command, args) => {
if (!args) { [command, ...args] = command.split(/\s+/) }
return execa(command, args, { cwd: context })
}
const { run, name, context, createCompleteCbs } = this
let preset
if (cliOptions.preset) {
@@ -114,7 +112,8 @@ module.exports = class Creator {
// intilaize git repository before installing deps
// so that vue-cli-service can setup git hooks.
if (hasGit()) {
const shouldInitGit = await this.shouldInitGit(cliOptions)
if (shouldInitGit) {
logWithSpinner(`🗃`, `Initializing git repository...`)
await run('git init')
}
@@ -158,7 +157,7 @@ module.exports = class Creator {
}
// commit initial state
if (hasGit()) {
if (shouldInitGit) {
await run('git add -A')
if (isTestOrDebug) {
await run('git', ['config', 'user.name', 'test'])
@@ -181,6 +180,11 @@ module.exports = class Creator {
generator.printExitLogs()
}
run (command, args) {
if (!args) { [command, ...args] = command.split(/\s+/) }
return execa(command, args, { cwd: this.context })
}
async promptAndResolvePreset () {
// prompt
await clearConsole(true)
@@ -379,4 +383,23 @@ module.exports = class Creator {
debug('vue-cli:prompts')(prompts)
return prompts
}
async shouldInitGit (cliOptions) {
if (!hasGit()) {
return false
}
if (cliOptions.skipGit) {
return false
}
// check if we are in a git repo already
try {
await this.run('git', ['status'])
} catch (e) {
// if git status failed, let's create a fresh repo
return true
}
// if git status worked, it means we are already in a git repo
// so don't init again.
return false
}
}