codekingpro/portable-devtools
114k
1// This is separate to indicate that it should contain code we expect to work in all versions of node >= 6.
2// This is a best effort to catch syntax errors to give users a good error message if they are using a node version that doesn't allow syntax we are using such as private properties, etc.
3// This file is linted with ecmaVersion=6 so we don't use invalid syntax, which is set in the .eslintrc.local.json file
4
5const { engines: { node: engines }, version } = require('../../package.json')
6const npm = `v${version}`
7
8module.exports = (process, getCli) => {
9 const node = process.version
10
11 const unsupportedMessage = `npm ${npm} does not support Node.js ${node}. This version of npm supports the following node versions: \`${engines}\`. You can find the latest version at https://nodejs.org/.`
12
13 const brokenMessage = `ERROR: npm ${npm} is known not to run on Node.js ${node}. This version of npm supports the following node versions: \`${engines}\`. You can find the latest version at https://nodejs.org/.`
14
15 // coverage ignored because this is only hit in very unsupported node versions and it's a best effort attempt to show something nice in those cases
16 /* istanbul ignore next */
17 const syntaxErrorHandler = (err) => {
18 if (err instanceof SyntaxError) {
19 // eslint-disable-next-line no-console
20 console.error(`${brokenMessage}\n\nERROR:`)
21 // eslint-disable-next-line no-console
22 console.error(err)
23 return process.exit(1)
24 }
25 throw err
26 }
27
28 process.on('uncaughtException', syntaxErrorHandler)
29 process.on('unhandledRejection', syntaxErrorHandler)
30
31 // require this only after setting up the error handlers
32 const cli = getCli()
33 return cli(process, {
34 node,
35 npm,
36 engines,
37 unsupportedMessage,
38 off: () => {
39 process.off('uncaughtException', syntaxErrorHandler)
40 process.off('unhandledRejection', syntaxErrorHandler)
41 },
42 })
43}
44 