From 23480ae8f57c0b1e0b95282da5fb41c5ca8a52cf Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 27 Apr 2018 12:03:23 -0400 Subject: [PATCH] feat(cli): skip git if already in a git repo, add --skipGit option close #967 --- packages/@vue/cli/bin/vue.js | 3 ++- packages/@vue/cli/lib/Creator.js | 37 ++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/packages/@vue/cli/bin/vue.js b/packages/@vue/cli/bin/vue.js index 91948320bc..2072f03385 100755 --- a/packages/@vue/cli/bin/vue.js +++ b/packages/@vue/cli/bin/vue.js @@ -39,9 +39,10 @@ program .option('-p, --preset ', 'Skip prompts and use saved or remote preset') .option('-d, --default', 'Skip prompts and use default preset') .option('-i, --inlinePreset ', 'Skip prompts and use inline JSON string as preset') - .option('-g, --initialCommit ', 'Specify initial commit message (when git is available)') .option('-m, --packageManager ', 'Use specified npm client when installing dependencies') .option('-r, --registry ', 'Use specified npm registry when installing dependencies (only for npm)') + .option('-s, --skipGit', 'Do not setup git repository when creating project') + .option('-g, --git ', '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) => { diff --git a/packages/@vue/cli/lib/Creator.js b/packages/@vue/cli/lib/Creator.js index 92a245ff7a..bea20453e3 100644 --- a/packages/@vue/cli/lib/Creator.js +++ b/packages/@vue/cli/lib/Creator.js @@ -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 + } }