bitburner-src/scripts/engines-check.js

82 lines
2.1 KiB
JavaScript
Raw Normal View History

#!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");
2021-10-11 22:38:50 +02:00
const getPackageJson = () =>
new Promise((resolve, reject) => {
try {
2021-10-11 22:38:50 +02:00
/* eslint-disable-next-line global-require */
resolve(require(path.resolve(process.cwd(), "package.json")));
} catch (error) {
2021-10-11 22:38:50 +02:00
reject(error);
}
2021-10-11 22:38:50 +02:00
});
2021-10-11 22:38:50 +02:00
const getEngines = (data) =>
new Promise((resolve, reject) => {
let versions = null;
if (data.engines) {
2021-10-11 22:38:50 +02:00
versions = data.engines;
}
if (versions) {
2021-10-11 22:38:50 +02:00
resolve(versions);
} else {
2021-10-11 22:38:50 +02:00
reject("Missing or improper 'engines' property in 'package.json'");
}
2021-10-11 22:38:50 +02:00
});
2021-10-11 22:38:50 +02:00
const checkNpmVersion = (engines) =>
new Promise((resolve, reject) => {
exec("npm -v", (error, stdout, stderr) => {
2021-10-11 22:38:50 +02:00
if (error) {
reject(`Unable to find NPM version\n${stderr}`);
}
2021-10-11 22:38:50 +02:00
const npmVersion = stdout.trim();
const engineVersion = engines.npm || ">=0";
2021-10-11 22:38:50 +02:00
if (semver.satisfies(npmVersion, engineVersion)) {
resolve();
} else {
reject(
`Incorrect npm version\n'package.json' specifies "${engineVersion}", you are currently running "${npmVersion}".`,
);
}
});
2021-10-11 22:38:50 +02:00
});
2021-10-11 22:38:50 +02:00
const checkNodeVersion = (engines) =>
new Promise((resolve, reject) => {
const nodeVersion = process.version.substring(1);
if (semver.satisfies(nodeVersion, engines.node)) {
2021-10-11 22:38:50 +02:00
resolve(engines);
} else {
2021-10-11 22:38:50 +02:00
reject(
`Incorrect node version\n'package.json' specifies "${engines.node}", you are currently running "${process.version}".`,
);
}
2021-10-11 22:38:50 +02:00
});
getPackageJson()
2021-10-11 22:38:50 +02:00
.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);
},
);