Skip to content

Commit 2de215e

Browse files
authoredFeb 28, 2019
feat: warn if run instant prototyping in a project directory (#3508)
closes #2473
1 parent f5b174f commit 2de215e

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"pretest": "yarn clean",
1111
"lint": "eslint --fix packages/**/*.js packages/**/bin/*",
1212
"check-links": "node scripts/checkLinks.js",
13-
"clean": "rimraf packages/test/*",
13+
"clean": "rimraf packages/test/* packages/**/temp/*",
1414
"sync": "node scripts/syncDeps.js",
1515
"boot": "node scripts/bootstrap.js",
1616
"release": "yarn --pure-lockfile && yarn clean && node scripts/release.js",

‎packages/@vue/cli-service-global/__tests__/globalService.spec.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import App from './Other.vue'
2222
new Vue({ render: h => h(App) }).$mount('#app')
2323
`.trim()
2424

25-
beforeAll(async () => {
25+
beforeEach(async () => {
2626
await fs.ensureDir(cwd)
2727
await write('App.vue', entryVue)
2828
await write('Other.vue', entryVue)
@@ -75,6 +75,35 @@ test('global build', async () => {
7575
expect(h1Text).toMatch('hi')
7676
})
7777

78+
test('warn if run plain `vue build` or `vue serve` alongside a `package.json` file', async () => {
79+
await write('package.json', `{
80+
"name": "hello-world",
81+
"version": "1.0.0",
82+
"scripts": {
83+
"serve": "vue-cli-service serve",
84+
"build": "vue-cli-service build"
85+
}
86+
}`)
87+
88+
// Warn if a package.json with corresponding `script` field exists
89+
const { stdout } = await execa(binPath, ['build'], { cwd })
90+
expect(stdout).toMatch(/Did you mean .*(yarn|npm run) build/)
91+
92+
await fs.unlink(path.join(cwd, 'App.vue'))
93+
94+
// Fail if no entry file exists, also show a hint for npm scripts
95+
expect(() => {
96+
execa.sync(binPath, ['build'], { cwd })
97+
}).toThrow(/Did you mean .*(yarn|npm run) build/)
98+
99+
expect(() => {
100+
execa.sync(binPath, ['serve'], { cwd })
101+
}).toThrow(/Did you mean .*(yarn|npm run) serve/)
102+
103+
// clean up, otherwise this file will affect other tests
104+
await fs.unlink(path.join(cwd, 'package.json'))
105+
})
106+
78107
afterAll(async () => {
79108
if (browser) {
80109
await browser.close()

‎packages/@vue/cli-service-global/index.js

+38-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,36 @@ const babelPlugin = toPlugin('@vue/cli-plugin-babel')
88
const eslintPlugin = toPlugin('@vue/cli-plugin-eslint')
99
const globalConfigPlugin = require('./lib/globalConfigPlugin')
1010

11-
function resolveEntry (entry) {
12-
const context = process.cwd()
11+
const context = process.cwd()
1312

13+
function warnAboutNpmScript (cmd) {
14+
const packageJsonPath = path.join(context, 'package.json')
15+
16+
if (!fs.existsSync(packageJsonPath)) {
17+
return
18+
}
19+
20+
let pkg
21+
try {
22+
pkg = require(packageJsonPath)
23+
} catch (e) {
24+
return
25+
}
26+
27+
if (!pkg.scripts || !pkg.scripts[cmd]) {
28+
return
29+
}
30+
31+
let script = `npm run ${cmd}`
32+
if (fs.existsSync(path.join(context, 'yarn.lock'))) {
33+
script = `yarn ${cmd}`
34+
}
35+
36+
console.log(`There's a ${chalk.yellow('package.json')} in the current directory.`)
37+
console.log(`Did you mean ${chalk.yellow(script)}?`)
38+
}
39+
40+
function resolveEntry (entry, cmd) {
1441
entry = entry || findExisting(context, [
1542
'main.js',
1643
'index.js',
@@ -21,14 +48,21 @@ function resolveEntry (entry) {
2148
if (!entry) {
2249
console.log(chalk.red(`Failed to locate entry file in ${chalk.yellow(context)}.`))
2350
console.log(chalk.red(`Valid entry file should be one of: main.js, index.js, App.vue or app.vue.`))
51+
52+
console.log()
53+
warnAboutNpmScript(cmd)
2454
process.exit(1)
2555
}
2656

2757
if (!fs.existsSync(path.join(context, entry))) {
2858
console.log(chalk.red(`Entry file ${chalk.yellow(entry)} does not exist.`))
59+
60+
console.log()
61+
warnAboutNpmScript(cmd)
2962
process.exit(1)
3063
}
3164

65+
warnAboutNpmScript(cmd)
3266
return {
3367
context,
3468
entry
@@ -50,12 +84,12 @@ function createService (context, entry, asLib) {
5084
}
5185

5286
exports.serve = (_entry, args) => {
53-
const { context, entry } = resolveEntry(_entry)
87+
const { context, entry } = resolveEntry(_entry, 'serve')
5488
createService(context, entry).run('serve', args)
5589
}
5690

5791
exports.build = (_entry, args) => {
58-
const { context, entry } = resolveEntry(_entry)
92+
const { context, entry } = resolveEntry(_entry, 'build')
5993
const asLib = args.target && args.target !== 'app'
6094
if (asLib) {
6195
args.entry = entry

0 commit comments

Comments
 (0)
Please sign in to comment.