Skip to content

Commit 97650b7

Browse files
committed
refactor: extract the utility to detect changed files in git
1 parent 3cfbd00 commit 97650b7

File tree

3 files changed

+49
-63
lines changed

3 files changed

+49
-63
lines changed

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

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@ const fs = require('fs')
22
const path = require('path')
33
const {
44
chalk,
5-
execa,
65
semver,
76

87
log,
98
done,
10-
119
logWithSpinner,
1210
stopSpinner,
1311

1412
isPlugin,
1513
resolvePluginId,
16-
loadModule,
17-
18-
hasProjectGit
14+
loadModule
1915
} = require('@vue/cli-shared-utils')
2016

2117
const Migrator = require('./Migrator')
2218
const tryGetNewerRange = require('./util/tryGetNewerRange')
2319
const readFiles = require('./util/readFiles')
20+
const getChangedFiles = require('./util/getChangedFiles')
2421

2522
const getPackageJson = require('./util/getPackageJson')
2623
const PackageManager = require('./util/ProjectPackageManager')
@@ -155,34 +152,23 @@ module.exports = class Upgrader {
155152
log()
156153
}
157154

158-
log(`${chalk.green('✔')} Successfully invoked migrator for plugin: ${chalk.cyan(plugin.id)}`)
159-
if (!process.env.VUE_CLI_TEST && hasProjectGit(this.context)) {
160-
const { stdout } = await execa('git', [
161-
'ls-files',
162-
'--exclude-standard',
163-
'--modified',
164-
'--others'
165-
], {
166-
cwd: this.context
167-
})
168-
if (stdout.trim()) {
169-
log(` The following files have been updated / added:\n`)
170-
log(
171-
chalk.red(
172-
stdout
173-
.split(/\r?\n/g)
174-
.map(line => ` ${line}`)
175-
.join('\n')
176-
)
177-
)
178-
log()
179-
log(
180-
` You should review these changes with ${chalk.cyan(
181-
`git diff`
182-
)} and commit them.`
183-
)
184-
log()
185-
}
155+
log(
156+
`${chalk.green(
157+
'✔'
158+
)} Successfully invoked migrator for plugin: ${chalk.cyan(plugin.id)}`
159+
)
160+
161+
const changedFiles = getChangedFiles(this.context)
162+
if (changedFiles.length) {
163+
log(` The following files have been updated / added:\n`)
164+
log(chalk.red(changedFiles.map(line => ` ${line}`).join('\n')))
165+
log()
166+
log(
167+
` You should review these changes with ${chalk.cyan(
168+
'git diff'
169+
)} and commit them.`
170+
)
171+
log()
186172
}
187173

188174
migrator.printExitLogs()

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

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ const path = require('path')
33
const inquirer = require('inquirer')
44
const {
55
chalk,
6-
execa,
76

87
log,
98
error,
109
logWithSpinner,
1110
stopSpinner,
1211

13-
hasProjectGit,
14-
1512
resolvePluginId,
1613

1714
loadModule
@@ -21,6 +18,7 @@ const Generator = require('./Generator')
2118

2219
const confirmIfGitDirty = require('./util/confirmIfGitDirty')
2320
const readFiles = require('./util/readFiles')
21+
const getChangedFiles = require('./util/getChangedFiles')
2422
const PackageManager = require('./util/ProjectPackageManager')
2523

2624
function getPkg (context) {
@@ -152,33 +150,17 @@ async function runGenerator (context, plugin, pkg = getPkg(context)) {
152150
}
153151

154152
log(`${chalk.green('✔')} Successfully invoked generator for plugin: ${chalk.cyan(plugin.id)}`)
155-
if (!process.env.VUE_CLI_TEST && hasProjectGit(context)) {
156-
const { stdout } = await execa('git', [
157-
'ls-files',
158-
'--exclude-standard',
159-
'--modified',
160-
'--others'
161-
], {
162-
cwd: context
163-
})
164-
if (stdout.trim()) {
165-
log(` The following files have been updated / added:\n`)
166-
log(
167-
chalk.red(
168-
stdout
169-
.split(/\r?\n/g)
170-
.map(line => ` ${line}`)
171-
.join('\n')
172-
)
173-
)
174-
log()
175-
log(
176-
` You should review these changes with ${chalk.cyan(
177-
`git diff`
178-
)} and commit them.`
179-
)
180-
log()
181-
}
153+
const changedFiles = getChangedFiles(context)
154+
if (changedFiles.length) {
155+
log(` The following files have been updated / added:\n`)
156+
log(chalk.red(changedFiles.map(line => ` ${line}`).join('\n')))
157+
log()
158+
log(
159+
` You should review these changes with ${chalk.cyan(
160+
'git diff'
161+
)} and commit them.`
162+
)
163+
log()
182164
}
183165

184166
generator.printExitLogs()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { execa, hasProjectGit } = require('@vue/cli-shared-utils')
2+
3+
module.exports = async function getChangedFiles (context) {
4+
if (!hasProjectGit(context)) return []
5+
6+
const { stdout } = await execa('git', [
7+
'ls-files',
8+
'-o',
9+
'--exclude-standard',
10+
'--full-name'
11+
], {
12+
cwd: context
13+
})
14+
if (stdout.trim()) {
15+
return stdout.split(/\r?\n/g)
16+
}
17+
return []
18+
}

0 commit comments

Comments
 (0)