Skip to content

Commit 7fc0f84

Browse files
authored
feat!: confirm for uncommitted changes before add/invoke/upgrade (#4275)
1 parent ddfdd1a commit 7fc0f84

File tree

6 files changed

+63
-0
lines changed

6 files changed

+63
-0
lines changed

packages/@vue/cli/__tests__/invoke.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,20 @@ test('invoking a plugin that renames files', async () => {
149149
await project.run(`${require.resolve('../bin/vue')} invoke typescript -d`)
150150
expect(project.has('src/main.js')).toBe(false)
151151
})
152+
153+
test('should prompt if invoking in a git repository with uncommited changes', async () => {
154+
delete process.env.VUE_CLI_SKIP_DIRTY_GIT_PROMPT
155+
const project = await create('invoke-dirty', {
156+
plugins: {
157+
'@vue/cli-plugin-babel': {}
158+
}
159+
})
160+
await project.write('some-random-file', '')
161+
expectPrompts([
162+
{
163+
message: `Still proceed?`,
164+
confirm: true
165+
}
166+
])
167+
await invoke(`babel`, {}, project.dir)
168+
})

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ const {
1010
resolvePluginId,
1111
resolveModule
1212
} = require('@vue/cli-shared-utils')
13+
const confirmIfGitDirty = require('./util/confirmIfGitDirty')
1314

1415
async function add (pluginName, options = {}, context = process.cwd()) {
16+
if (!(await confirmIfGitDirty(context))) {
17+
return
18+
}
19+
1520
const packageName = resolvePluginId(pluginName)
1621

1722
log()

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
const Generator = require('./Generator')
1919
const { loadOptions } = require('./options')
2020
const { installDeps } = require('./util/installDeps')
21+
const confirmIfGitDirty = require('./util/confirmIfGitDirty')
2122
const readFiles = require('./util/readFiles')
2223

2324
function getPkg (context) {
@@ -33,6 +34,10 @@ function getPkg (context) {
3334
}
3435

3536
async function invoke (pluginName, options = {}, context = process.cwd()) {
37+
if (!(await confirmIfGitDirty(context))) {
38+
return
39+
}
40+
3641
delete options._
3742
const pkg = getPkg(context)
3843

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const getPackageJson = require('./util/getPackageJson')
2727
const getInstalledVersion = require('./util/getInstalledVersion')
2828
const tryGetNewerRange = require('./util/tryGetNewerRange')
2929
const readFiles = require('./util/readFiles')
30+
const confirmIfGitDirty = require('./util/confirmIfGitDirty')
3031

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

@@ -245,6 +246,10 @@ async function upgradeAll (context) {
245246
}
246247

247248
async function upgrade (packageName, options, context = process.cwd()) {
249+
if (!(await confirmIfGitDirty(context))) {
250+
return
251+
}
252+
248253
if (!packageName) {
249254
if (options.to) {
250255
error(`Must specify a package name to upgrade to ${options.to}`)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const execa = require('execa')
2+
const inquirer = require('inquirer')
3+
4+
const { warn, hasProjectGit } = require('@vue/cli-shared-utils')
5+
6+
module.exports = async function confirmIfGitDirty (context) {
7+
if (process.env.VUE_CLI_SKIP_DIRTY_GIT_PROMPT) {
8+
return true
9+
}
10+
11+
if (!hasProjectGit(context)) {
12+
return true
13+
}
14+
15+
const { stdout } = await execa('git', ['status', '--porcelain'], { cwd: context })
16+
if (!stdout) {
17+
return true
18+
}
19+
20+
warn(`There are uncommited changes in the current repository, it's recommended to commit or stash them first.`)
21+
const { ok } = await inquirer.prompt([
22+
{
23+
name: 'ok',
24+
type: 'confirm',
25+
message: 'Still proceed',
26+
default: false
27+
}
28+
])
29+
return ok
30+
}

scripts/testSetup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
process.env.VUE_CLI_TEST = true
2+
process.env.VUE_CLI_SKIP_DIRTY_GIT_PROMPT = true

0 commit comments

Comments
 (0)