feat(cli): warn if incompatible node/package versions detected (#8792)

Co-authored-by: Pooya Parsa <pyapar@gmail.com>
This commit is contained in:
Daniel Roe 2021-02-09 17:35:22 +00:00 committed by GitHub
parent 346a57eb34
commit e5d202badb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 0 deletions

View File

@ -31,6 +31,7 @@
"minimist": "^1.2.5", "minimist": "^1.2.5",
"opener": "1.5.2", "opener": "1.5.2",
"pretty-bytes": "^5.5.0", "pretty-bytes": "^5.5.0",
"semver": "^7.3.4",
"serve-static": "^1.14.1", "serve-static": "^1.14.1",
"std-env": "^2.2.1", "std-env": "^2.2.1",
"upath": "^2.0.1", "upath": "^2.0.1",

View File

@ -1,5 +1,6 @@
import consola from 'consola' import consola from 'consola'
import exit from 'exit' import exit from 'exit'
import { checkDependencies } from './utils/dependencies'
import { fatalBox } from './utils/formatting' import { fatalBox } from './utils/formatting'
let _setup = false let _setup = false
@ -15,6 +16,8 @@ export default function setup ({ dev }) {
} }
_setup = true _setup = true
checkDependencies()
// Global error handler // Global error handler
/* istanbul ignore next */ /* istanbul ignore next */
process.on('unhandledRejection', (err) => { process.on('unhandledRejection', (err) => {

View File

@ -0,0 +1,35 @@
import consola from 'consola'
import { satisfies } from 'semver'
import { getPKG } from '@nuxt/utils'
const dependencies = {
postcss: '^7.0.32',
webpack: '^4.46.0',
'sass-loader': '^10.1.1'
}
const nodeVersion = '>=12.0.0'
function getInstalledVersion (name) {
try {
return getPKG(name).version
} catch { }
}
export function checkDependencies () {
for (const name in dependencies) {
const installedVersion = getInstalledVersion(name)
if (!installedVersion) {
return // Ignore to avoid false-positive warnings
}
const expectedRange = dependencies[name]
if (!satisfies(installedVersion, expectedRange)) {
consola.warn(`${name}@${installedVersion} is installed but ${expectedRange} is expected`)
}
}
// Check Node versions
if (!satisfies(process.version, nodeVersion)) {
consola.warn(`You are using an unsupported version of Node.js (${process.version}). It is recommended to use the latest LTS version (https://nodejs.org/en/about/releases)`)
}
}

View File

@ -0,0 +1,25 @@
import { consola } from '../utils'
import { checkDependencies } from '../../src/utils/dependencies'
jest.mock('webpack/package.json', () => ({
version: '5.0.0'
}))
describe('cli/utils', () => {
afterEach(() => jest.resetAllMocks())
test('checkDependencies', () => {
checkDependencies()
expect(consola.warn).toHaveBeenCalledWith(
expect.stringMatching(
/webpack@.+ is installed but .+ is expected/
)
)
expect(consola.warn).toHaveBeenCalledTimes(1)
if (process.version.startsWith('v10')) {
expect(consola.warn).toHaveBeenCalledTimes(2)
} else {
expect(consola.warn).toHaveBeenCalledTimes(1)
}
})
})