From 819afcdeb447b73b675cfc654f641fc2df5894f9 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Tue, 13 Oct 2020 12:20:04 +0200 Subject: [PATCH] fix(cli): avoid error about `nuxt-edge` if installed in parent `node_modules` (#8194) --- packages/cli/src/run.js | 19 +++++++------------ packages/cli/test/unit/run-edge.test.js | 10 ++++++++-- 2 files changed, 15 insertions(+), 14 deletions(-) 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.') }) })