fix(cli): improve project dir detection for external commands (#7860)

This commit is contained in:
Ahad Birang 2020-08-06 00:53:25 +04:30 committed by GitHub
parent c30499d7e0
commit 4a9c9a15f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 4 deletions

View File

@ -13,3 +13,4 @@ export { default as setup } from './setup'
export { default as run } from './run'
export { loadNuxtConfig } from './utils/config'
export { getWebpackConfig } from './utils/webpack'
export { isNuxtDir } from './utils/dir'

View File

@ -1,9 +1,9 @@
import fs from 'fs'
import execa from 'execa'
import { name as pkgName } from '../package.json'
import NuxtCommand from './command'
import setup from './setup'
import getCommand from './commands'
import { isNuxtDir } from './utils/dir'
function packageExists (name) {
try {
@ -28,7 +28,7 @@ export default async function run (_argv, hooks = {}) {
let cmd = await getCommand(argv[0])
// Matching `nuxt` or `nuxt [dir]` or `nuxt -*` for `nuxt dev` shortcut
if (!cmd && (!argv[0] || argv[0][0] === '-' || (argv[0] !== 'static' && fs.existsSync(argv[0])))) {
if (!cmd && (!argv[0] || argv[0][0] === '-' || isNuxtDir(argv[0]))) {
argv.unshift('dev')
cmd = await getCommand('dev')
}

View File

@ -0,0 +1,11 @@
import fs from 'fs'
import path from 'path'
export function isNuxtDir (rootDir) {
if (fs.existsSync(path.join(rootDir, 'nuxt.config.js')) ||
fs.existsSync(path.join(rootDir, 'pages')) ||
fs.existsSync(path.join(rootDir, 'nuxt.config.ts'))) {
return true
}
return false
}

View File

@ -1,3 +1,4 @@
import path from 'path'
import execa from 'execa'
import run from '../../src/run'
import getCommand from '../../src/commands'
@ -37,9 +38,10 @@ describe('run', () => {
})
test('nuxt <dir> aliases to nuxt dev <dir>', async () => {
await run([__dirname])
const rootDir = path.resolve(__dirname, '../fixtures')
await run([rootDir])
expect(getCommand).toHaveBeenCalledWith('dev')
expect(NuxtCommand.run).toHaveBeenCalledWith(expect.anything(), [__dirname], {})
expect(NuxtCommand.run).toHaveBeenCalledWith(expect.anything(), [rootDir], {})
})
test('external commands', async () => {