feat: warn if run instant prototyping in a project directory (#3508)

closes #2473
This commit is contained in:
Haoqun Jiang
2019-02-28 15:04:35 +08:00
committed by GitHub
parent f5b174ff79
commit 2de215e849
3 changed files with 69 additions and 6 deletions

View File

@@ -10,7 +10,7 @@
"pretest": "yarn clean",
"lint": "eslint --fix packages/**/*.js packages/**/bin/*",
"check-links": "node scripts/checkLinks.js",
"clean": "rimraf packages/test/*",
"clean": "rimraf packages/test/* packages/**/temp/*",
"sync": "node scripts/syncDeps.js",
"boot": "node scripts/bootstrap.js",
"release": "yarn --pure-lockfile && yarn clean && node scripts/release.js",

View File

@@ -22,7 +22,7 @@ import App from './Other.vue'
new Vue({ render: h => h(App) }).$mount('#app')
`.trim()
beforeAll(async () => {
beforeEach(async () => {
await fs.ensureDir(cwd)
await write('App.vue', entryVue)
await write('Other.vue', entryVue)
@@ -75,6 +75,35 @@ test('global build', async () => {
expect(h1Text).toMatch('hi')
})
test('warn if run plain `vue build` or `vue serve` alongside a `package.json` file', async () => {
await write('package.json', `{
"name": "hello-world",
"version": "1.0.0",
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build"
}
}`)
// Warn if a package.json with corresponding `script` field exists
const { stdout } = await execa(binPath, ['build'], { cwd })
expect(stdout).toMatch(/Did you mean .*(yarn|npm run) build/)
await fs.unlink(path.join(cwd, 'App.vue'))
// Fail if no entry file exists, also show a hint for npm scripts
expect(() => {
execa.sync(binPath, ['build'], { cwd })
}).toThrow(/Did you mean .*(yarn|npm run) build/)
expect(() => {
execa.sync(binPath, ['serve'], { cwd })
}).toThrow(/Did you mean .*(yarn|npm run) serve/)
// clean up, otherwise this file will affect other tests
await fs.unlink(path.join(cwd, 'package.json'))
})
afterAll(async () => {
if (browser) {
await browser.close()

View File

@@ -8,9 +8,36 @@ const babelPlugin = toPlugin('@vue/cli-plugin-babel')
const eslintPlugin = toPlugin('@vue/cli-plugin-eslint')
const globalConfigPlugin = require('./lib/globalConfigPlugin')
function resolveEntry (entry) {
const context = process.cwd()
const context = process.cwd()
function warnAboutNpmScript (cmd) {
const packageJsonPath = path.join(context, 'package.json')
if (!fs.existsSync(packageJsonPath)) {
return
}
let pkg
try {
pkg = require(packageJsonPath)
} catch (e) {
return
}
if (!pkg.scripts || !pkg.scripts[cmd]) {
return
}
let script = `npm run ${cmd}`
if (fs.existsSync(path.join(context, 'yarn.lock'))) {
script = `yarn ${cmd}`
}
console.log(`There's a ${chalk.yellow('package.json')} in the current directory.`)
console.log(`Did you mean ${chalk.yellow(script)}?`)
}
function resolveEntry (entry, cmd) {
entry = entry || findExisting(context, [
'main.js',
'index.js',
@@ -21,14 +48,21 @@ function resolveEntry (entry) {
if (!entry) {
console.log(chalk.red(`Failed to locate entry file in ${chalk.yellow(context)}.`))
console.log(chalk.red(`Valid entry file should be one of: main.js, index.js, App.vue or app.vue.`))
console.log()
warnAboutNpmScript(cmd)
process.exit(1)
}
if (!fs.existsSync(path.join(context, entry))) {
console.log(chalk.red(`Entry file ${chalk.yellow(entry)} does not exist.`))
console.log()
warnAboutNpmScript(cmd)
process.exit(1)
}
warnAboutNpmScript(cmd)
return {
context,
entry
@@ -50,12 +84,12 @@ function createService (context, entry, asLib) {
}
exports.serve = (_entry, args) => {
const { context, entry } = resolveEntry(_entry)
const { context, entry } = resolveEntry(_entry, 'serve')
createService(context, entry).run('serve', args)
}
exports.build = (_entry, args) => {
const { context, entry } = resolveEntry(_entry)
const { context, entry } = resolveEntry(_entry, 'build')
const asLib = args.target && args.target !== 'app'
if (asLib) {
args.entry = entry