Skip to content

Commit 23480ae

Browse files
committed
feat(cli): skip git if already in a git repo, add --skipGit option
close #967
1 parent a0a7dc6 commit 23480ae

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

packages/@vue/cli/bin/vue.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ program
3939
.option('-p, --preset <presetName>', 'Skip prompts and use saved or remote preset')
4040
.option('-d, --default', 'Skip prompts and use default preset')
4141
.option('-i, --inlinePreset <json>', 'Skip prompts and use inline JSON string as preset')
42-
.option('-g, --initialCommit <message>', 'Specify initial commit message (when git is available)')
4342
.option('-m, --packageManager <command>', 'Use specified npm client when installing dependencies')
4443
.option('-r, --registry <url>', 'Use specified npm registry when installing dependencies (only for npm)')
44+
.option('-s, --skipGit', 'Do not setup git repository when creating project')
45+
.option('-g, --git <message>', 'Specify initial commit message (when git is available)')
4546
.option('-f, --force', 'Overwrite target directory if it exists')
4647
.option('-c, --clone', 'Use git clone when fetching remote preset')
4748
.action((name, cmd) => {

packages/@vue/cli/lib/Creator.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,15 @@ module.exports = class Creator {
4646
this.promptCompleteCbs = []
4747
this.createCompleteCbs = []
4848

49+
this.run = this.run.bind(this)
50+
4951
const promptAPI = new PromptModuleAPI(this)
5052
promptModules.forEach(m => m(promptAPI))
5153
}
5254

5355
async create (cliOptions = {}) {
5456
const isTestOrDebug = process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG
55-
const { name, context, createCompleteCbs } = this
56-
const run = (command, args) => {
57-
if (!args) { [command, ...args] = command.split(/\s+/) }
58-
return execa(command, args, { cwd: context })
59-
}
57+
const { run, name, context, createCompleteCbs } = this
6058

6159
let preset
6260
if (cliOptions.preset) {
@@ -114,7 +112,8 @@ module.exports = class Creator {
114112

115113
// intilaize git repository before installing deps
116114
// so that vue-cli-service can setup git hooks.
117-
if (hasGit()) {
115+
const shouldInitGit = await this.shouldInitGit(cliOptions)
116+
if (shouldInitGit) {
118117
logWithSpinner(`🗃`, `Initializing git repository...`)
119118
await run('git init')
120119
}
@@ -158,7 +157,7 @@ module.exports = class Creator {
158157
}
159158

160159
// commit initial state
161-
if (hasGit()) {
160+
if (shouldInitGit) {
162161
await run('git add -A')
163162
if (isTestOrDebug) {
164163
await run('git', ['config', 'user.name', 'test'])
@@ -181,6 +180,11 @@ module.exports = class Creator {
181180
generator.printExitLogs()
182181
}
183182

183+
run (command, args) {
184+
if (!args) { [command, ...args] = command.split(/\s+/) }
185+
return execa(command, args, { cwd: this.context })
186+
}
187+
184188
async promptAndResolvePreset () {
185189
// prompt
186190
await clearConsole(true)
@@ -379,4 +383,23 @@ module.exports = class Creator {
379383
debug('vue-cli:prompts')(prompts)
380384
return prompts
381385
}
386+
387+
async shouldInitGit (cliOptions) {
388+
if (!hasGit()) {
389+
return false
390+
}
391+
if (cliOptions.skipGit) {
392+
return false
393+
}
394+
// check if we are in a git repo already
395+
try {
396+
await this.run('git', ['status'])
397+
} catch (e) {
398+
// if git status failed, let's create a fresh repo
399+
return true
400+
}
401+
// if git status worked, it means we are already in a git repo
402+
// so don't init again.
403+
return false
404+
}
382405
}

0 commit comments

Comments
 (0)