fix(cli): avoid error about nuxt-edge if installed in parent node_modules (#8194)

This commit is contained in:
pooya parsa 2020-10-13 12:20:04 +02:00 committed by GitHub
parent 5b87d1cfc0
commit 819afcdeb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 14 deletions

View File

@ -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

View File

@ -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.')
})
})