Skip to content

Commit bd3e781

Browse files
committed
fix: consolidate node version support logic
1 parent f50b093 commit bd3e781

File tree

4 files changed

+54
-148
lines changed

4 files changed

+54
-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: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ const cliMock = async (t, opts) => {
1414
const { Npm, outputs, logMocks, logs } = await loadMockNpm(t, { ...opts, init: false })
1515
const cli = t.mock('../../lib/cli.js', {
1616
'../../lib/npm.js': Npm,
17-
'../../lib/utils/unsupported.js': {
18-
checkForBrokenNode: () => {},
19-
checkForUnsupportedNode: () => {},
20-
},
2117
'../../lib/utils/exit-handler.js': exitHandlerMock,
2218
...logMocks,
2319
})
@@ -175,3 +171,38 @@ t.test('load error calls error handler', async t => {
175171
await cli(process)
176172
t.strictSame(exitHandlerCalled(), [err])
177173
})
174+
175+
t.test('known broken node version', async t => {
176+
const errors = []
177+
let exitCode
178+
const { cli } = await cliMock(t, {
179+
globals: {
180+
'console.error': (msg) => errors.push(msg),
181+
'process.version': '6.0.0',
182+
'process.exit': e => exitCode = e,
183+
},
184+
})
185+
await cli(process)
186+
t.match(errors, [
187+
'ERROR: npm is known not to run on Node.js 6.0.0',
188+
'You\'ll need to upgrade to a newer Node.js version in order to use this',
189+
'version of npm. You can find the latest version at https://nodejs.org/',
190+
])
191+
t.match(exitCode, 1)
192+
})
193+
194+
t.test('unsupported node version', async t => {
195+
const errors = []
196+
const { cli } = await cliMock(t, {
197+
globals: {
198+
'console.error': (msg) => errors.push(msg),
199+
'process.version': '10.0.0',
200+
},
201+
})
202+
await cli(process)
203+
t.match(errors, [
204+
'npm does not support Node.js 10.0.0',
205+
'You should probably upgrade to a newer version of node as we',
206+
'can\'t make any promises that npm will work with this version.',
207+
])
208+
})

test/lib/utils/unsupported.js

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

0 commit comments

Comments
 (0)