2019-10-17 12:14:31 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that semver v1 is greater than or equal to semver v2.
|
|
|
|
*
|
|
|
|
* We don't `require('semver')` to allow this script to be run as a npm
|
|
|
|
* 'preinstall' hook, at which point no dependencies have been installed.
|
2022-01-04 14:50:54 +00:00
|
|
|
*
|
|
|
|
* @param {string} version
|
2019-10-17 12:14:31 +00:00
|
|
|
*/
|
2020-03-24 17:52:05 +00:00
|
|
|
function parseSemver(version) {
|
|
|
|
const match = /v?(\d+)\.(\d+).(\d+)/.exec(version);
|
|
|
|
if (match == null) {
|
|
|
|
throw new Error(`Invalid semver version: ${version}`);
|
2019-10-17 12:14:31 +00:00
|
|
|
}
|
2020-03-24 17:52:05 +00:00
|
|
|
const [, major, minor, patch] = match;
|
|
|
|
return [parseInt(major, 10), parseInt(minor, 10), parseInt(patch, 10)];
|
|
|
|
}
|
|
|
|
|
2022-01-04 14:50:54 +00:00
|
|
|
/**
|
|
|
|
* @param {string} v1
|
|
|
|
* @param {string} v2
|
|
|
|
*/
|
2020-03-24 17:52:05 +00:00
|
|
|
function semverGte(v1, v2) {
|
2023-10-27 14:57:07 +00:00
|
|
|
const v1Array = parseSemver(v1);
|
|
|
|
const v2Array = parseSemver(v2);
|
2019-10-17 12:14:31 +00:00
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
|
if (v1Array[i] < v2Array[i]) {
|
|
|
|
return false;
|
|
|
|
} else if (v1Array[i] > v2Array[i]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkNpmVersion() {
|
|
|
|
const execSync = require('child_process').execSync;
|
2020-06-15 22:53:07 +00:00
|
|
|
const npmVersion = execSync('npm --version').toString().trim();
|
2019-10-17 12:14:31 +00:00
|
|
|
const requiredVersion = '6.9.0';
|
|
|
|
if (!semverGte(npmVersion, requiredVersion)) {
|
2019-10-17 20:12:40 +00:00
|
|
|
// In case you take issue with the error message below:
|
|
|
|
// "At this point, however, your 'npm-shrinkwrap.json' file has
|
|
|
|
// already been damaged"
|
|
|
|
// ... and think: "why not add the check to the 'preinstall' hook?",
|
|
|
|
// the reason is that it would unnecessarily prevent end users from
|
|
|
|
// using npm v6.4.1 that ships with Node 8. (It is OK for the
|
|
|
|
// shrinkwrap file to get damaged if it is not going to be reused.)
|
2021-08-26 23:49:54 +00:00
|
|
|
throw new Error(`\
|
|
|
|
-----------------------------------------------------------------------------
|
2019-10-17 12:14:31 +00:00
|
|
|
Error: npm version '${npmVersion}' detected. Please upgrade to npm v${requiredVersion} or later
|
2019-10-17 20:12:40 +00:00
|
|
|
because of a bug that causes the 'npm-shrinkwrap.json' file to be damaged.
|
|
|
|
At this point, however, your 'npm-shrinkwrap.json' file has already been
|
|
|
|
damaged. Please revert it to the master branch state with a command such as:
|
|
|
|
"git checkout master -- npm-shrinkwrap.json"
|
|
|
|
Then re-run "npm install" using npm version ${requiredVersion} or later.
|
2021-08-26 23:49:54 +00:00
|
|
|
-----------------------------------------------------------------------------`);
|
2019-10-17 12:14:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 23:49:54 +00:00
|
|
|
function main() {
|
|
|
|
try {
|
|
|
|
checkNpmVersion();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e.message || e);
|
|
|
|
process.exitCode = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|