Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit the npm version to be 6 #29204

Merged
merged 2 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions bin/check-latest-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@
const { green, red, yellow } = require( 'chalk' );
const { get } = require( 'https' );
const { spawn } = require( 'child_process' );
const semver = require( 'semver' );

/**
* Internal dependencies
*/
const {
engines: { npm: npmRange },
// Ignore reason: `package.json` exists outside `bin` `rootDir`.
// @ts-ignore
} = require( '../package.json' );

/**
* Returns a promise resolving with the version number of the latest available
* version of NPM.
* version of npm.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed from NPM to npm since it's actually supposed to be all-lowercase 😅 .

*
* @return {Promise<string>} Promise resolving with latest NPM version.
* @return {Promise<string>} Promise resolving with latest npm version.
*/
async function getLatestNPMVersion() {
return new Promise( ( resolve, reject ) => {
Expand All @@ -30,7 +40,7 @@ async function getLatestNPMVersion() {
async ( response ) => {
if ( response.statusCode !== 200 ) {
return reject(
new Error( 'Package data for NPM not found' )
new Error( 'Package data for npm not found' )
);
}

Expand All @@ -45,23 +55,25 @@ async function getLatestNPMVersion() {
} catch {
return reject(
new Error(
'Package data for NPM returned invalid response body'
'Package data for npm returned invalid response body'
)
);
}

resolve( data[ 'dist-tags' ].latest );
const versions = Object.values( data[ 'dist-tags' ] );

resolve( semver.maxSatisfying( versions, npmRange ) );
}
).on( 'error', ( error ) => {
if (
/** @type {NodeJS.ErrnoException} */ ( error ).code ===
'ENOTFOUND'
) {
error = new Error( `Could not contact the NPM registry to determine latest version.
error = new Error( `Could not contact the npm registry to determine latest version.

This could be due to an intermittent outage of the service, or because you are not connected to the internet.

Because it is important that \`package-lock.json\` files only be committed while running the latest version of NPM, this commit has been blocked.
Because it is important that \`package-lock.json\` files only be committed while running the latest version of npm, this commit has been blocked.

If you are certain of your changes and desire to commit anyways, you should either connect to the internet or bypass commit verification using ${ yellow(
'git commit --no-verify'
Expand All @@ -75,9 +87,9 @@ If you are certain of your changes and desire to commit anyways, you should eith

/**
* Returns a promise resolving with the version number of the local installed
* version of NPM.
* version of npm.
*
* @return {Promise<string>} Promise resolving with local installed NPM version.
* @return {Promise<string>} Promise resolving with local installed npm version.
*/
async function getLocalNPMVersion() {
return new Promise( async ( resolve ) => {
Expand All @@ -99,23 +111,23 @@ Promise.all( [ getLatestNPMVersion(), getLocalNPMVersion() ] )
.then( ( [ latest, local ] ) => {
if ( latest !== local ) {
throw new Error(
`The local NPM version does not match the expected latest version. Expected ${ green(
`The local npm version does not match the expected latest version. Expected ${ green(
latest
) }, found ${ red( local ) }.

It is required that you have the latest version of NPM installed in order to commit a change to the package-lock.json file.
It is required that you have the expected latest version of npm installed in order to commit a change to the package-lock.json file.

Run ${ yellow(
'npm install --global npm@latest'
) } to install the latest version of NPM. Before retrying your commit, run ${ yellow(
`npm install --global npm@${ latest }`
) } to install the expected latest version of npm. Before retrying your commit, run ${ yellow(
'npm install'
) } once more to ensure the package-lock.json contents are correct. If there are any changes to the file, they should be included in your commit.`
);
}
} )
.catch( ( error ) => {
console.error(
'Latest NPM check failed!\n\n' + error.toString() + '\n'
'Latest npm check failed!\n\n' + error.toString() + '\n'
);
process.exitCode = 1;
} );
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"engines": {
"node": ">=10.0.0",
"npm": ">=6.9.0"
"npm": ">=6.9.0 <7"
},
"config": {
"GUTENBERG_PHASE": 2,
Expand Down