mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-17 13:13:49 +01:00
9e697a6809
Apparently the "engines" files of package.json is no longer checked regularly when performing installs. With the changes and different behaviors across different version of Node & NPM, and having no other way to ensure everybody contributing is on the same page, this preinstall check is necessary. This heavily relies on "SemVer" NPM package, but in order to actually check this prior to the first set of packages installed the code needs to be there. So this is a slimmed down version of SemVer and syntax has been updated.
71 lines
2.1 KiB
JavaScript
71 lines
2.1 KiB
JavaScript
#!node
|
|
|
|
/*
|
|
This ultimately is derived from https://github.com/daton89-topperblues/node-engine-strict
|
|
Since this needs to run *before* any dependencies are installed, it must be inlined here.
|
|
*/
|
|
const path = require("path");
|
|
const exec = require("child_process").exec;
|
|
const semver = require("./semver");
|
|
|
|
const getPackageJson = () => new Promise((resolve, reject) => {
|
|
try {
|
|
/* eslint-disable-next-line global-require */
|
|
resolve(require(path.resolve(process.cwd(), "package.json")));
|
|
} catch (error) {
|
|
reject(error);
|
|
}
|
|
});
|
|
|
|
const getEngines = (data) => new Promise((resolve, reject) => {
|
|
let versions = null;
|
|
|
|
if (data.engines) {
|
|
versions = data.engines;
|
|
}
|
|
|
|
if (versions) {
|
|
resolve(versions);
|
|
} else {
|
|
reject("Missing or improper 'engines' property in 'package.json'");
|
|
}
|
|
});
|
|
|
|
const checkNpmVersion = (engines) => new Promise((resolve, reject) => {
|
|
exec("npm -v", (error, stdout, stderr) => {
|
|
if (error) {
|
|
reject(`Unable to find NPM version\n${stderr}`);
|
|
}
|
|
|
|
const npmVersion = stdout.trim();
|
|
const engineVersion = engines.npm || ">=0";
|
|
|
|
if (semver.satisfies(npmVersion, engineVersion)) {
|
|
resolve();
|
|
} else {
|
|
reject(`Incorrect npm version\n'package.json' specifies "${engineVersion}", you are currently running "${npmVersion}".`);
|
|
}
|
|
});
|
|
});
|
|
|
|
const checkNodeVersion = (engines) => new Promise((resolve, reject) => {
|
|
const nodeVersion = process.version.substring(1);
|
|
|
|
if (semver.satisfies(nodeVersion, engines.node)) {
|
|
resolve(engines);
|
|
} else {
|
|
reject(`Incorrect node version\n'package.json' specifies "${engines.node}", you are currently running "${process.version}".`);
|
|
}
|
|
});
|
|
|
|
getPackageJson()
|
|
.then(getEngines)
|
|
.then(checkNodeVersion)
|
|
.then(checkNpmVersion)
|
|
.then(() => true, (error) => {
|
|
// Specifically disable these as the error message gets lost in the normal unhandled output.
|
|
/* eslint-disable no-console, no-process-exit */
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|