-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactor engines validation to lint syntax
- Loading branch information
1 parent
173bc89
commit 3e6b2a8
Showing
7 changed files
with
123 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { resolve, relative } = require('path') | ||
|
||
// Create an override to lockdown a file to es6 syntax only | ||
// and only allow it to require an allowlist of files | ||
const res = (p) => resolve(__dirname, p) | ||
const rel = (p) => relative(__dirname, res(p)) | ||
const braces = (a) => a.length > 1 ? `{${a.map(rel).join(',')}}` : a[0] | ||
|
||
const es6Files = (e) => Object.entries(e).map(([file, allow]) => ({ | ||
files: `./${rel(file)}`, | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
}, | ||
rules: { | ||
'node/no-restricted-require': ['error', [{ | ||
name: ['/**', `!${__dirname}/${braces(allow)}`], | ||
message: `This file can only require: ${allow.join(',')}`, | ||
}]], | ||
}, | ||
})) | ||
|
||
module.exports = { | ||
rules: { | ||
'no-console': 'error', | ||
}, | ||
overrides: es6Files({ | ||
'index.js': ['lib/es6/validate-engines.js'], | ||
'lib/es6/validate-engines.js': ['package.json', 'lib/cli.js'], | ||
}), | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
if (require.main === module) { | ||
require('./lib/cli.js')(process) | ||
require('./lib/es6/validate-engines.js')( | ||
process, | ||
() => require(require('path').resolve(__dirname, './lib/cli.js')) | ||
) | ||
} else { | ||
throw new Error('The programmatic API was removed in npm v8.0.0') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// This is separate to indicate that it should contain code we expect to work in | ||
// all versions of node >= 6. 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. This file is | ||
// linted with ecmaVersion=6 so we don't use invalid syntax, which is set in the | ||
// .eslintrc.local.json file | ||
|
||
const { engines: { node: engines }, version } = require('../../package.json') | ||
const npm = `v${version}` | ||
|
||
module.exports = (process, getCli) => { | ||
const node = process.version.replace(/-.*$/, '') | ||
|
||
/* eslint-disable-next-line max-len */ | ||
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/.` | ||
|
||
/* eslint-disable-next-line max-len */ | ||
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/.` | ||
|
||
// 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 | ||
/* istanbul ignore next */ | ||
const syntaxErrorHandler = (err) => { | ||
if (err instanceof SyntaxError) { | ||
// eslint-disable-next-line no-console | ||
console.error(`${brokenMessage}\n\nERROR:`) | ||
// eslint-disable-next-line no-console | ||
console.error(err) | ||
return process.exit(1) | ||
} | ||
throw err | ||
} | ||
|
||
process.on('uncaughtException', syntaxErrorHandler) | ||
process.on('unhandledRejection', syntaxErrorHandler) | ||
|
||
// require this only after setting up the error handlers | ||
const cli = getCli() | ||
return cli(process, { | ||
node, | ||
npm, | ||
engines, | ||
unsupportedMessage, | ||
off: () => { | ||
process.off('uncaughtException', syntaxErrorHandler) | ||
process.off('unhandledRejection', syntaxErrorHandler) | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const t = require('tap') | ||
const mockGlobals = require('@npmcli/mock-globals') | ||
const tmock = require('../../fixtures/tmock') | ||
|
||
const mockValidateEngines = (t) => { | ||
const validateEngines = tmock(t, '{LIB}/es6/validate-engines.js', { | ||
'{ROOT}/package.json': { version: '1.2.3', engines: { node: '>=0' } }, | ||
}) | ||
mockGlobals(t, { 'process.version': 'v4.5.6' }) | ||
return validateEngines(process, () => (_, r) => r) | ||
} | ||
|
||
t.test('validate engines', async t => { | ||
t.equal(process.listenerCount('uncaughtException'), 0) | ||
t.equal(process.listenerCount('unhandledRejection'), 0) | ||
|
||
const result = mockValidateEngines(t) | ||
|
||
t.equal(process.listenerCount('uncaughtException'), 1) | ||
t.equal(process.listenerCount('unhandledRejection'), 1) | ||
|
||
t.match(result, { | ||
node: 'v4.5.6', | ||
npm: 'v1.2.3', | ||
engines: '>=0', | ||
/* eslint-disable-next-line max-len */ | ||
unsupportedMessage: 'npm v1.2.3 does not support Node.js v4.5.6. This version of npm supports the following node versions: `>=0`. You can find the latest version at https://nodejs.org/.', | ||
}) | ||
|
||
result.off() | ||
|
||
t.equal(process.listenerCount('uncaughtException'), 0) | ||
t.equal(process.listenerCount('unhandledRejection'), 0) | ||
}) |