bitburner-src/scripts/engines-check.js
Steven Evans 9e697a6809 [chore] Script to check the required version of Node & NPM
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.
2018-10-01 19:51:32 -05:00

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);
});