diff --git a/packages/cli/src/run.js b/packages/cli/src/run.js index b9fde5758d..4687589926 100644 --- a/packages/cli/src/run.js +++ b/packages/cli/src/run.js @@ -1,24 +1,19 @@ +import { existsSync } from 'fs' +import { resolve } from 'path' import execa from 'execa' +import consola from 'consola' 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 { - require.resolve(name) - return true - } catch (e) { - return false - } -} - export default async function run (_argv, hooks = {}) { // Check for not installing both nuxt and nuxt-edge - const dupPkg = '@nuxt/' + (pkgName === '@nuxt/cli-edge' ? 'cli' : 'cli-edge') - if (packageExists(dupPkg)) { - throw new Error('Both `nuxt` and `nuxt-edge` dependencies are installed! This is unsupported, please choose one and remove the other one from dependencies.') + const dupPkg = pkgName === '@nuxt/cli-edge' ? 'cli' : 'cli-edge' + const dupPkgJSON = resolve(__dirname, '../..' /* dist/../.. */, dupPkg, 'package.json') + if (existsSync(dupPkgJSON) && require(dupPkgJSON).name !== '@nuxt/' + dupPkg) { + consola.warn('Both `nuxt` and `nuxt-edge` dependencies are installed! Please choose one and remove the other one from dependencies.') } // Read from process.argv diff --git a/packages/cli/test/unit/run-edge.test.js b/packages/cli/test/unit/run-edge.test.js index 366f91c746..3a70578447 100644 --- a/packages/cli/test/unit/run-edge.test.js +++ b/packages/cli/test/unit/run-edge.test.js @@ -1,12 +1,18 @@ +import consola from 'consola' +import getCommand from '../../src/commands' import run from '../../src/run' +jest.mock('../../src/commands') + jest.mock('../../package.json', () => ({ name: '@nuxt/cli-edge' })) describe('run in edge', () => { test('throws error if nuxt and nuxt-edge are installed', async () => { - await expect(run()) - .rejects.toThrow('Both `nuxt` and `nuxt-edge` dependencies are installed! This is unsupported, please choose one and remove the other one from dependencies.') + const mockedCommand = { run: jest.fn(() => Promise.resolve({})) } + getCommand.mockImplementationOnce(() => Promise.resolve(mockedCommand)) + await run() + expect(consola.warn).toHaveBeenCalledWith('Both `nuxt` and `nuxt-edge` dependencies are installed! Please choose one and remove the other one from dependencies.') }) })