Skip to content

Commit 47e48a6

Browse files
committed
Added version number of npm
- Added version number of npm to the open source libraries/tools license information. This is done by reading the stdout of npm cli process, so the feature will be unavailable when using other package managers (pnpm, for example) - Refactored the construction of map for open source libraries/tools license information into helper function getLibraries() - Added new configuration "cliProcessTimeout", used as timeout for cli processes (like git, npm) that are spawned synchronously - Added missing line break of logging message - Removed unnecessary "return" from the last statement in arrow function body - Removed unnecessary env option when spawning new child processes, as process.env is the default - Disallow 0 as value for all timeout configurations - Bumped version to 2.3.0-alpha1
1 parent 2e09695 commit 47e48a6

File tree

3 files changed

+58
-23
lines changed

3 files changed

+58
-23
lines changed

configurations/configuration.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
"developmentGuildIDs": [
1313
"511620513643364353"
1414
],
15-
"developmentEnvironment": false
15+
"developmentEnvironment": false,
16+
"cliProcessTimeout": 5000
1617
}

index.js

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ process.on("uncaughtException", error => {
5555

5656
const ms = require("ms");
5757
const https = require("https");
58+
const path = require("path");
5859

5960
const packageJSON = require("./package.json");
6061
const configuration = require("./configurations/configuration.json");
@@ -72,6 +73,8 @@ const developerIDs = getConfiguration("developerIDs");
7273
const developmentGuildIDs = getConfiguration("developmentGuildIDs");
7374
const token = getConfiguration("token");
7475
const developmentEnvironment = getConfiguration("developmentEnvironment");
76+
const cliProcessTimeout = getConfiguration("cliProcessTimeout");
77+
const libraryMap = getLibraries();
7578
const remindmeMaxTimeStringLength = 100;
7679
// 100 is the limit for ms library, see https://github.com/zeit/ms/blob/2.1.1/index.js#L50
7780
const licenseInfoCooldown = 1000 * 60 * 30;
@@ -134,19 +137,6 @@ const response = {
134137
]
135138
};
136139

137-
const libraryMap = new Map()
138-
.set("node", process.version)
139-
.set("npm", "");
140-
for (const library in packageJSON.dependencies) {
141-
libraryMap.set(library, packageJSON.dependencies[library]);
142-
}
143-
libraryMap.set("async-limiter", "")
144-
.set("long", "")
145-
.set("prism-media", "")
146-
.set("snekfetch", "")
147-
.set("tweetnacl-js", "")
148-
.set("ws", "");
149-
150140
if (process.env.RESTARTED !== undefined) {
151141
console.log("This process was started by the previous process.");
152142
if (restartTimeout >= 1000) {
@@ -507,9 +497,9 @@ client.on("ready", () => {
507497
let raw = "";
508498
response.on("data", chunk => raw += chunk)
509499
.on("error", error => {
510-
console.error(`An error occured while attempting to fetch changelog!\n\nFull details:${error}`);
511-
return channel.send(errorFetchingChangelogString)
512-
.catch(error => console.error(`An error occured while sending message "${errorFetchingChangelogString}"!\n\nFull details:\n${error}`));
500+
console.error(`An error occured while attempting to fetch changelog!\n\nFull details:\n${error}`);
501+
channel.send(errorFetchingChangelogString)
502+
.catch(error => console.error(`An error occured while sending message "${errorFetchingChangelogString}"!\n\nFull details:\n${error}`));
513503
}).on("end", () => {
514504
if (!response.complete) {
515505
console.error("Unable to get changelog, connection was terminated while response was still not fully received!");
@@ -602,7 +592,6 @@ function exitProcess(exitCode, shouldRestart) {
602592
}
603593
process.env.RESTARTED = exitCode === 0 ? "normal" : unexpectedRestartIdentifier;
604594
childProcess.spawn(newArgs[0], newArgs.slice(1), {
605-
env: process.env,
606595
stdio: "ignore",
607596
detached: process.platform === "win32",
608597
shell: true
@@ -637,8 +626,8 @@ function getConfiguration(configurationType) {
637626
if (!(typeof result === "number" && isInteger(result.toString()))) {
638627
throw new TypeError("Invalid restartTimeout value!");
639628
}
640-
if (result < 0) {
641-
throw new Error("Invalid restartTimeout value, restartTimeout must not be less than 0!");
629+
if (result <= 0) {
630+
throw new Error("Invalid restartTimeout value, restartTimeout must not be less than or equal to 0!");
642631
}
643632
break;
644633
}
@@ -700,8 +689,8 @@ function getConfiguration(configurationType) {
700689
if (!(typeof result === "number" && isInteger(result.toString()))) {
701690
throw new TypeError("Invalid changelogFetchTimeout value!");
702691
}
703-
if (result < 0) {
704-
throw new Error("Invalid changelogFetchTimeout value, changelogFetchTimeout must not be less than 0!");
692+
if (result <= 0) {
693+
throw new Error("Invalid changelogFetchTimeout value, changelogFetchTimeout must not be less than or equal to 0!");
705694
}
706695
break;
707696
}
@@ -776,13 +765,58 @@ function getConfiguration(configurationType) {
776765
break;
777766
}
778767

768+
case "cliProcessTimeout": {
769+
if (result === useProcessEnvIdentifier) {
770+
result = parseInt(process.env.BOT_CLI_PROCESS_TIMEOUT, 10);
771+
}
772+
if (!(typeof result === "number" && isInteger(result.toString()))) {
773+
throw new TypeError("Invalid cliProcessTimeout value!");
774+
}
775+
if (result <= 0) {
776+
throw new Error("Invalid cliProcessTimeout value, cliProcessTimeout must not be less than or equal to 0!");
777+
}
778+
break;
779+
}
780+
779781
default: {
780782
throw new Error("Invalid configuration to fetch!");
781783
}
782784
}
783785
return result;
784786
}
785787

788+
function getLibraries() {
789+
const map = new Map().set("node", process.version);
790+
let npmPath = path.resolve(path.dirname(process.argv[0]), (process.platform === "win32" ? "./npm.cmd" : "./npm"));
791+
if (npmPath.includes(" ")) {
792+
npmPath = `"${npmPath}"`;
793+
}
794+
const npmProcess = childProcess.spawnSync(npmPath, ["-v"], {
795+
timeout: cliProcessTimeout,
796+
shell: true,
797+
windowsHide: true
798+
});
799+
let npmVersion = "";
800+
if (npmProcess.error !== undefined && npmProcess.error !== null) {
801+
console.error(`An error occured while attempting to read stdout of NPM process!\n\nFull details:\n${npmProcess.error}`);
802+
} else {
803+
const bufferString = npmProcess.stdout.toString();
804+
if (/^\s*(\d+\.){2}\d+\s*$/gi.test(bufferString)) {
805+
npmVersion = bufferString.trim();
806+
}
807+
}
808+
map.set("npm", npmVersion);
809+
for (const library in packageJSON.dependencies) {
810+
map.set(library, packageJSON.dependencies[library]);
811+
}
812+
return map.set("async-limiter", "")
813+
.set("long", "")
814+
.set("prism-media", "")
815+
.set("snekfetch", "")
816+
.set("tweetnacl-js", "")
817+
.set("ws", "");
818+
}
819+
786820
function bold(string) {
787821
if (!(typeof string === "string" || string instanceof String)) {
788822
throw new TypeError("Incorrect type for bold argument!");

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "chillbot",
3-
"version": "2.2.16-beta1",
3+
"version": "2.3.0-alpha1",
44
"description": "Chill!",
55
"keywords": [
66
"discord",

0 commit comments

Comments
 (0)