Skip to content

feat!: confirm before invoke/add/upgrade if project has uncommitted changes #4275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/@vue/cli/__tests__/invoke.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,20 @@ test('invoking a plugin that renames files', async () => {
await project.run(`${require.resolve('../bin/vue')} invoke typescript -d`)
expect(project.has('src/main.js')).toBe(false)
})

test('should prompt if invoking in a git repository with uncommited changes', async () => {
delete process.env.VUE_CLI_SKIP_DIRTY_GIT_PROMPT
const project = await create('invoke-dirty', {
plugins: {
'@vue/cli-plugin-babel': {}
}
})
await project.write('some-random-file', '')
expectPrompts([
{
message: `Still proceed?`,
confirm: true
}
])
await invoke(`babel`, {}, project.dir)
})
5 changes: 5 additions & 0 deletions packages/@vue/cli/lib/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ const {
resolvePluginId,
resolveModule
} = require('@vue/cli-shared-utils')
const confirmIfGitDirty = require('./util/confirmIfGitDirty')

async function add (pluginName, options = {}, context = process.cwd()) {
if (!(await confirmIfGitDirty(context))) {
return
}

const packageName = resolvePluginId(pluginName)

log()
Expand Down
5 changes: 5 additions & 0 deletions packages/@vue/cli/lib/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
const Generator = require('./Generator')
const { loadOptions } = require('./options')
const { installDeps } = require('./util/installDeps')
const confirmIfGitDirty = require('./util/confirmIfGitDirty')
const readFiles = require('./util/readFiles')

function getPkg (context) {
Expand All @@ -33,6 +34,10 @@ function getPkg (context) {
}

async function invoke (pluginName, options = {}, context = process.cwd()) {
if (!(await confirmIfGitDirty(context))) {
return
}

delete options._
const pkg = getPkg(context)

Expand Down
5 changes: 5 additions & 0 deletions packages/@vue/cli/lib/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const getPackageJson = require('./util/getPackageJson')
const getInstalledVersion = require('./util/getInstalledVersion')
const tryGetNewerRange = require('./util/tryGetNewerRange')
const readFiles = require('./util/readFiles')
const confirmIfGitDirty = require('./util/confirmIfGitDirty')

const isTestOrDebug = process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG

Expand Down Expand Up @@ -245,6 +246,10 @@ async function upgradeAll (context) {
}

async function upgrade (packageName, options, context = process.cwd()) {
if (!(await confirmIfGitDirty(context))) {
return
}

if (!packageName) {
if (options.to) {
error(`Must specify a package name to upgrade to ${options.to}`)
Expand Down
30 changes: 30 additions & 0 deletions packages/@vue/cli/lib/util/confirmIfGitDirty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const execa = require('execa')
const inquirer = require('inquirer')

const { warn, hasProjectGit } = require('@vue/cli-shared-utils')

module.exports = async function confirmIfGitDirty (context) {
if (process.env.VUE_CLI_SKIP_DIRTY_GIT_PROMPT) {
return true
}

if (!hasProjectGit(context)) {
return true
}

const { stdout } = await execa('git', ['status', '--porcelain'], { cwd: context })
if (!stdout) {
return true
}

warn(`There are uncommited changes in the current repository, it's recommended to commit or stash them first.`)
const { ok } = await inquirer.prompt([
{
name: 'ok',
type: 'confirm',
message: 'Still proceed',
default: false
}
])
return ok
}
1 change: 1 addition & 0 deletions scripts/testSetup.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
process.env.VUE_CLI_TEST = true
process.env.VUE_CLI_SKIP_DIRTY_GIT_PROMPT = true