Skip to content

Commit

Permalink
fix: consolidate node version support logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar authored and lukekarrys committed Mar 28, 2022
1 parent f76d4f2 commit 57d8f75
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 148 deletions.
22 changes: 19 additions & 3 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,25 @@ module.exports = async process => {
// so now both broken and unsupported use console, but only broken
// will process.exit. It is important to now perform *both* of these
// checks as early as possible so the user gets the error message.
const { checkForBrokenNode, checkForUnsupportedNode } = require('./utils/unsupported.js')
checkForBrokenNode()
checkForUnsupportedNode()
const semver = require('semver')
const supported = require('../package.json').engines.node
const knownBroken = '<6.2.0 || 9 <9.3.0'

const nodejsVersion = process.version.replace(/-.*$/, '')
/* eslint-disable no-console */
if (semver.satisfies(nodejsVersion, knownBroken)) {
console.error('ERROR: npm is known not to run on Node.js ' + process.version)
console.error("You'll need to upgrade to a newer Node.js version in order to use this")
console.error('version of npm. You can find the latest version at https://nodejs.org/')
process.exit(1)
}
if (!semver.satisfies(nodejsVersion, supported)) {
console.error('npm does not support Node.js ' + process.version)
console.error('You should probably upgrade to a newer version of node as we')
console.error("can't make any promises that npm will work with this version.")
console.error('You can find the latest version at https://nodejs.org/')
}
/* eslint-enable no-console */

const exitHandler = require('./utils/exit-handler.js')
process.on('uncaughtException', exitHandler)
Expand Down
39 changes: 0 additions & 39 deletions lib/utils/unsupported.js

This file was deleted.

39 changes: 35 additions & 4 deletions test/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ const cliMock = async (t, opts) => {
const { Npm, outputs, logMocks, logs } = await loadMockNpm(t, { ...opts, init: false })
const cli = t.mock('../../lib/cli.js', {
'../../lib/npm.js': Npm,
'../../lib/utils/unsupported.js': {
checkForBrokenNode: () => {},
checkForUnsupportedNode: () => {},
},
'../../lib/utils/exit-handler.js': exitHandlerMock,
...logMocks,
})
Expand Down Expand Up @@ -175,3 +171,38 @@ t.test('load error calls error handler', async t => {
await cli(process)
t.strictSame(exitHandlerCalled(), [err])
})

t.test('known broken node version', async t => {
const errors = []
let exitCode
const { cli } = await cliMock(t, {
globals: {
'console.error': (msg) => errors.push(msg),
'process.version': '6.0.0',
'process.exit': e => exitCode = e,
},
})
await cli(process)
t.match(errors, [
'ERROR: npm is known not to run on Node.js 6.0.0',
'You\'ll need to upgrade to a newer Node.js version in order to use this',
'version of npm. You can find the latest version at https://nodejs.org/',
])
t.match(exitCode, 1)
})

t.test('unsupported node version', async t => {
const errors = []
const { cli } = await cliMock(t, {
globals: {
'console.error': (msg) => errors.push(msg),
'process.version': '10.0.0',
},
})
await cli(process)
t.match(errors, [
'npm does not support Node.js 10.0.0',
'You should probably upgrade to a newer version of node as we',
'can\'t make any promises that npm will work with this version.',
])
})
102 changes: 0 additions & 102 deletions test/lib/utils/unsupported.js

This file was deleted.

0 comments on commit 57d8f75

Please sign in to comment.