diff --git a/packages/cli/src/index.js b/packages/cli/src/index.js index fd633598a3..29878c55ec 100644 --- a/packages/cli/src/index.js +++ b/packages/cli/src/index.js @@ -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' diff --git a/packages/cli/src/run.js b/packages/cli/src/run.js index 9d4979d43c..b9fde5758d 100644 --- a/packages/cli/src/run.js +++ b/packages/cli/src/run.js @@ -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') } diff --git a/packages/cli/src/utils/dir.js b/packages/cli/src/utils/dir.js new file mode 100644 index 0000000000..c96208d508 --- /dev/null +++ b/packages/cli/src/utils/dir.js @@ -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 +} diff --git a/packages/cli/test/unit/run.test.js b/packages/cli/test/unit/run.test.js index 6134085795..0b99bd18d7 100644 --- a/packages/cli/test/unit/run.test.js +++ b/packages/cli/test/unit/run.test.js @@ -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 aliases to nuxt dev ', 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 () => {