Skip to content

Commit 8e5448c

Browse files
author
Guillaume Chau
committed
refactor: hasProjectYarn & hasProjectGit
1 parent 841b470 commit 8e5448c

File tree

11 files changed

+90
-36
lines changed

11 files changed

+90
-36
lines changed

packages/@vue/cli-service/lib/PluginAPI.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class PluginAPI {
1717
this.service = service
1818
}
1919

20+
/**
21+
* Current working directory.
22+
*/
23+
getCwd () {
24+
return this.service.context
25+
}
26+
2027
/**
2128
* Resolve path for a project.
2229
*

packages/@vue/cli-service/lib/commands/serve.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const {
22
info,
3-
hasYarn,
3+
hasProjectYarn,
44
openBrowser,
55
IpcMessenger
66
} = require('@vue/cli-shared-utils')
@@ -193,7 +193,7 @@ module.exports = (api, options) => {
193193
isFirstCompile = false
194194

195195
if (!isProduction) {
196-
const buildCommand = hasYarn() ? `yarn build` : `npm run build`
196+
const buildCommand = hasProjectYarn(api.getCwd()) ? `yarn build` : `npm run build`
197197
console.log(` Note that the development build is not optimized.`)
198198
console.log(` To create a production build, run ${chalk.cyan(buildCommand)}.`)
199199
} else {

packages/@vue/cli-shared-utils/lib/env.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const { execSync } = require('child_process')
2+
const fs = require('fs')
3+
const path = require('path')
24

35
let _hasYarn
6+
const _yarnProjects = new Map()
47
let _hasGit
8+
const _gitProjects = new Map()
59

610
// env detection
711
exports.hasYarn = () => {
@@ -19,6 +23,22 @@ exports.hasYarn = () => {
1923
}
2024
}
2125

26+
exports.hasProjectYarn = (cwd) => {
27+
if (_yarnProjects.has(cwd)) {
28+
return checkYarn(_yarnProjects.get(cwd))
29+
}
30+
31+
const lockFile = path.join(cwd, 'yarn.lock')
32+
const result = fs.existsSync(lockFile)
33+
_yarnProjects.set(cwd, result)
34+
return checkYarn(result)
35+
}
36+
37+
function checkYarn (result) {
38+
if (result && !exports.hasYarn()) throw new Error(`The project seems to require yarn but it's not installed.`)
39+
return result
40+
}
41+
2242
exports.hasGit = () => {
2343
if (process.env.VUE_CLI_TEST) {
2444
return true
@@ -33,3 +53,20 @@ exports.hasGit = () => {
3353
return (_hasGit = false)
3454
}
3555
}
56+
57+
exports.hasProjectGit = (cwd) => {
58+
if (_gitProjects.has(cwd)) {
59+
return _gitProjects.get(cwd)
60+
}
61+
62+
let result
63+
try {
64+
execSync('git status', { stdio: 'ignore', cwd })
65+
result = true
66+
} catch (e) {
67+
result = false
68+
}
69+
console.log('hasProjectGit', cwd, result)
70+
_gitProjects.set(cwd, result)
71+
return result
72+
}

packages/@vue/cli-ui/apollo-server/connectors/dependencies.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function install ({ id, type }, context) {
150150
status: 'dependency-install',
151151
args: [id]
152152
})
153-
await installPackage(cwd.get(), getCommand(), null, id, type === 'devDependencies')
153+
await installPackage(cwd.get(), getCommand(cwd.get()), null, id, type === 'devDependencies')
154154

155155
logs.add({
156156
message: `Dependency ${id} installed`,
@@ -178,7 +178,7 @@ function uninstall ({ id }, context) {
178178

179179
const dep = findOne(id, context)
180180

181-
await uninstallPackage(cwd.get(), getCommand(), null, id)
181+
await uninstallPackage(cwd.get(), getCommand(cwd.get()), null, id)
182182

183183
logs.add({
184184
message: `Dependency ${id} uninstalled`,
@@ -204,7 +204,7 @@ function update ({ id }, context) {
204204

205205
const dep = findOne(id, context)
206206
const { current, wanted } = await getVersion(dep, context)
207-
await updatePackage(cwd.get(), getCommand(), null, id)
207+
await updatePackage(cwd.get(), getCommand(cwd.get()), null, id)
208208

209209
logs.add({
210210
message: `Dependency ${id} updated from ${current} to ${wanted}`,
@@ -249,7 +249,7 @@ function updateAll (context) {
249249
args: [updatedDeps.length]
250250
})
251251

252-
await updatePackage(cwd.get(), getCommand(), null, updatedDeps.map(
252+
await updatePackage(cwd.get(), getCommand(cwd.get()), null, updatedDeps.map(
253253
p => p.id
254254
).join(' '))
255255

packages/@vue/cli-ui/apollo-server/connectors/git.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ const path = require('path')
33
const parseDiff = require('../util/parse-diff')
44
// Connectors
55
const cwd = require('./cwd')
6+
// Utils
7+
const { hasProjectGit } = require('@vue/cli-shared-utils')
68

79
async function getNewFiles (context) {
10+
if (!hasProjectGit(cwd.get())) return []
11+
812
const { stdout } = await execa('git', [
913
'ls-files',
1014
'-o',
@@ -20,6 +24,8 @@ async function getNewFiles (context) {
2024
}
2125

2226
async function getDiffs (context) {
27+
if (!hasProjectGit(cwd.get())) return []
28+
2329
const newFiles = await getNewFiles(context)
2430
await execa('git', ['add', '-N', '*'], {
2531
cwd: cwd.get()
@@ -40,6 +46,8 @@ async function getDiffs (context) {
4046
}
4147

4248
async function commit (message, context) {
49+
if (!hasProjectGit(cwd.get())) return false
50+
4351
await execa('git', ['add', '*'], {
4452
cwd: cwd.get()
4553
})
@@ -50,13 +58,17 @@ async function commit (message, context) {
5058
}
5159

5260
async function reset (context) {
61+
if (!hasProjectGit(cwd.get())) return false
62+
5363
await execa('git', ['reset'], {
5464
cwd: cwd.get()
5565
})
5666
return true
5767
}
5868

5969
async function getRoot (context) {
70+
if (!hasProjectGit(cwd.get())) return cwd.get()
71+
6072
const { stdout } = await execa('git', [
6173
'rev-parse',
6274
'--show-toplevel'

packages/@vue/cli-ui/apollo-server/connectors/plugins.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ function install (id, context) {
217217
if (process.env.VUE_CLI_DEBUG && isOfficialPlugin(id)) {
218218
mockInstall(id, context)
219219
} else {
220-
await installPackage(cwd.get(), getCommand(), null, id)
220+
await installPackage(cwd.get(), getCommand(cwd.get()), null, id)
221221
}
222222
await initPrompts(id, context)
223223
installationStep = 'config'
@@ -250,7 +250,7 @@ function uninstall (id, context) {
250250
if (process.env.VUE_CLI_DEBUG && isOfficialPlugin(id)) {
251251
mockUninstall(id, context)
252252
} else {
253-
await uninstallPackage(cwd.get(), getCommand(), null, id)
253+
await uninstallPackage(cwd.get(), getCommand(cwd.get()), null, id)
254254
}
255255
currentPluginId = null
256256
installationStep = null
@@ -330,7 +330,7 @@ function update (id, context) {
330330
const plugin = findOne(id, context)
331331
const { current, wanted } = await dependencies.getVersion(plugin, context)
332332

333-
await updatePackage(cwd.get(), getCommand(), null, id)
333+
await updatePackage(cwd.get(), getCommand(cwd.get()), null, id)
334334

335335
logs.add({
336336
message: `Plugin ${id} updated from ${current} to ${wanted}`,
@@ -377,7 +377,7 @@ async function updateAll (context) {
377377
args: [updatedPlugins.length]
378378
})
379379

380-
await updatePackage(cwd.get(), getCommand(), null, updatedPlugins.map(
380+
await updatePackage(cwd.get(), getCommand(cwd.get()), null, updatedPlugins.map(
381381
p => p.id
382382
).join(' '))
383383

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
const {
2-
hasYarn
2+
hasYarn,
3+
hasProjectYarn
34
} = require('@vue/cli-shared-utils')
45
const { loadOptions } = require('@vue/cli/lib/options')
56

6-
exports.getCommand = function () {
7-
return loadOptions().packageManager || (hasYarn() ? 'yarn' : 'npm')
7+
exports.getCommand = function (cwd = undefined) {
8+
if (!cwd) {
9+
return loadOptions().packageManager || (hasYarn() ? 'yarn' : 'npm')
10+
}
11+
return hasProjectYarn(cwd) ? 'yarn' : 'npm'
812
}

packages/@vue/cli-ui/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"files-changed": "Files changed",
1717
"search-file": "Search file",
1818
"empty": "No change found",
19+
"error": "Couldn't get file changes",
1920
"modals": {
2021
"commit": {
2122
"title": "Commit changes",

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const {
3030
warn,
3131
error,
3232
hasGit,
33+
hasProjectGit,
3334
hasYarn,
3435
logWithSpinner,
3536
stopSpinner,
@@ -443,17 +444,9 @@ module.exports = class Creator extends EventEmitter {
443444
return false
444445
}
445446
if (cliOptions.git) {
446-
return cliOptions.git !== 'false'
447+
return cliOptions.git !== 'false' && cliOptions.git !== false
447448
}
448-
// check if we are in a git repo already
449-
try {
450-
await this.run('git', ['status'])
451-
} catch (e) {
452-
// if git status failed, let's create a fresh repo
453-
return true
454-
}
455-
// if git status worked, it means we are already in a git repo
456-
// so don't init again.
457-
return false
449+
450+
return !hasProjectGit(this.context)
458451
}
459452
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { resolveModule, loadModule } = require('./util/module')
66
const {
77
log,
88
error,
9-
hasYarn,
9+
hasProjectYarn,
1010
stopSpinner,
1111
resolvePluginId
1212
} = require('@vue/cli-shared-utils')
@@ -26,7 +26,7 @@ async function add (pluginName, options = {}, context = process.cwd()) {
2626
log(`📦 Installing ${chalk.cyan(packageName)}...`)
2727
log()
2828

29-
const packageManager = loadOptions().packageManager || (hasYarn() ? 'yarn' : 'npm')
29+
const packageManager = loadOptions().packageManager || (hasProjectYarn(context) ? 'yarn' : 'npm')
3030
await installPackage(context, packageManager, null, packageName)
3131

3232
stopSpinner()

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const normalizeFilePaths = require('./util/normalizeFilePaths')
1313
const {
1414
log,
1515
error,
16-
hasYarn,
17-
hasGit,
16+
hasProjectYarn,
17+
hasProjectGit,
1818
logWithSpinner,
1919
stopSpinner,
2020
resolvePluginId
@@ -122,7 +122,7 @@ async function runGenerator (context, plugin, pkg = getPkg(context)) {
122122
if (!isTestOrDebug && depsChanged) {
123123
log(`📦 Installing additional dependencies...`)
124124
const packageManager =
125-
loadOptions().packageManager || (hasYarn() ? 'yarn' : 'npm')
125+
loadOptions().packageManager || (hasProjectYarn(context) ? 'yarn' : 'npm')
126126
await installDeps(context, packageManager)
127127
}
128128

@@ -137,7 +137,7 @@ async function runGenerator (context, plugin, pkg = getPkg(context)) {
137137

138138
log()
139139
log(` Successfully invoked generator for plugin: ${chalk.cyan(plugin.id)}`)
140-
if (!process.env.VUE_CLI_TEST && hasGit()) {
140+
if (!process.env.VUE_CLI_TEST && hasProjectGit(context)) {
141141
const { stdout } = await execa('git', [
142142
'ls-files',
143143
'--exclude-standard',
@@ -157,14 +157,14 @@ async function runGenerator (context, plugin, pkg = getPkg(context)) {
157157
)
158158
)
159159
log()
160+
log(
161+
` You should review these changes with ${chalk.cyan(
162+
`git diff`
163+
)} and commit them.`
164+
)
165+
log()
160166
}
161167
}
162-
log(
163-
` You should review these changes with ${chalk.cyan(
164-
`git diff`
165-
)} and commit them.`
166-
)
167-
log()
168168

169169
generator.printExitLogs()
170170
}

0 commit comments

Comments
 (0)