@@ -55,6 +55,7 @@ process.on("uncaughtException", error => {
5555
5656const ms = require ( "ms" ) ;
5757const https = require ( "https" ) ;
58+ const path = require ( "path" ) ;
5859
5960const packageJSON = require ( "./package.json" ) ;
6061const configuration = require ( "./configurations/configuration.json" ) ;
@@ -72,6 +73,8 @@ const developerIDs = getConfiguration("developerIDs");
7273const developmentGuildIDs = getConfiguration ( "developmentGuildIDs" ) ;
7374const token = getConfiguration ( "token" ) ;
7475const developmentEnvironment = getConfiguration ( "developmentEnvironment" ) ;
76+ const cliProcessTimeout = getConfiguration ( "cliProcessTimeout" ) ;
77+ const libraryMap = getLibraries ( ) ;
7578const remindmeMaxTimeStringLength = 100 ;
7679// 100 is the limit for ms library, see https://github.com/zeit/ms/blob/2.1.1/index.js#L50
7780const 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-
150140if ( 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+
786820function bold ( string ) {
787821 if ( ! ( typeof string === "string" || string instanceof String ) ) {
788822 throw new TypeError ( "Incorrect type for bold argument!" ) ;
0 commit comments