Skip to content

Commit

Permalink
feat(cli): skip git if already in a git repo, add --skipGit option
Browse files Browse the repository at this point in the history
close #967
  • Loading branch information
yyx990803 committed Apr 27, 2018
1 parent a0a7dc6 commit 23480ae
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/@vue/cli/bin/vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
37 changes: 30 additions & 7 deletions packages/@vue/cli/lib/Creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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')
}
Expand Down Expand Up @@ -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'])
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
}

0 comments on commit 23480ae

Please sign in to comment.