Skip to content

Commit 33b67be

Browse files
committed
fix: consolidate node version support logic
1 parent cd9e5d2 commit 33b67be

File tree

4 files changed

+52
-148
lines changed

4 files changed

+52
-148
lines changed

lib/cli.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,25 @@ module.exports = async process => {
1111
// so now both broken and unsupported use console, but only broken
1212
// will process.exit. It is important to now perform *both* of these
1313
// checks as early as possible so the user gets the error message.
14-
const { checkForBrokenNode, checkForUnsupportedNode } = require('./utils/unsupported.js')
15-
checkForBrokenNode()
16-
checkForUnsupportedNode()
14+
const semver = require('semver')
15+
const supported = require('../package.json').engines.node
16+
const knownBroken = '<6.2.0 || 9 <9.3.0'
17+
18+
const nodejsVersion = process.version.replace(/-.*$/, '')
19+
/* eslint-disable no-console */
20+
if (semver.satisfies(nodejsVersion, knownBroken)) {
21+
console.error('ERROR: npm is known not to run on Node.js ' + process.version)
22+
console.error("You'll need to upgrade to a newer Node.js version in order to use this")
23+
console.error('version of npm. You can find the latest version at https://nodejs.org/')
24+
process.exit(1)
25+
}
26+
if (!semver.satisfies(nodejsVersion, supported)) {
27+
console.error('npm does not support Node.js ' + process.version)
28+
console.error('You should probably upgrade to a newer version of node as we')
29+
console.error("can't make any promises that npm will work with this version.")
30+
console.error('You can find the latest version at https://nodejs.org/')
31+
}
32+
/* eslint-enable no-console */
1733

1834
const exitHandler = require('./utils/exit-handler.js')
1935
process.on('uncaughtException', exitHandler)

lib/utils/unsupported.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

test/lib/cli.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ const cliMock = async (t, mocks) => {
1616
const cli = t.mock('../../lib/cli.js', {
1717
'../../lib/npm.js': Npm,
1818
'../../lib/utils/update-notifier.js': async () => null,
19-
'../../lib/utils/unsupported.js': {
20-
checkForBrokenNode: () => {},
21-
checkForUnsupportedNode: () => {},
22-
},
2319
'../../lib/utils/exit-handler.js': exitHandlerMock,
2420
...logMocks,
2521
})
@@ -162,3 +158,36 @@ t.test('load error calls error handler', async t => {
162158
await cli(process)
163159
t.strictSame(exitHandlerCalled(), [err])
164160
})
161+
162+
t.test('known broken node version', async t => {
163+
const errors = []
164+
let exitCode
165+
mockGlobals(t, {
166+
'console.error': (msg) => errors.push(msg),
167+
'process.version': '6.0.0',
168+
'process.exit': e => exitCode = e,
169+
})
170+
const { cli } = await cliMock(t)
171+
await cli(process)
172+
t.match(errors, [
173+
'ERROR: npm is known not to run on Node.js 6.0.0',
174+
'You\'ll need to upgrade to a newer Node.js version in order to use this',
175+
'version of npm. You can find the latest version at https://nodejs.org/',
176+
])
177+
t.match(exitCode, 1)
178+
})
179+
180+
t.test('unsupported node version', async t => {
181+
const errors = []
182+
mockGlobals(t, {
183+
'console.error': (msg) => errors.push(msg),
184+
'process.version': '10.0.0',
185+
})
186+
const { cli } = await cliMock(t)
187+
await cli(process)
188+
t.match(errors, [
189+
'npm does not support Node.js 10.0.0',
190+
'You should probably upgrade to a newer version of node as we',
191+
'can\'t make any promises that npm will work with this version.',
192+
])
193+
})

test/lib/utils/unsupported.js

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)