2023-06-20 18:26:19 -07:00
// libraries
const fs = require ( "fs/promises" )
const path = require ( "path" )
const crypto = require ( "crypto" )
const {
sourceDir
} = require ( "./config.js" )
class Logger {
constructor ( { name , logDebug } ) {
this . name = name
this . debug = logDebug
}
_log ( logType , data , method ) {
console [ method ] ( ` [ ${ this . name } ] [ ${ logType } ] ${ typeof data == "string" ? data : data . toString ( ) } ` )
}
info ( data ) {
this . _log ( "info" , data , "log" )
}
warn ( data ) {
this . _log ( "warn" , data , "error" )
}
error ( data ) {
this . _log ( "error" , data , "error" )
}
debug ( data ) {
if ( this . debug ) {
this . _log ( "debug" , data , "error" )
}
}
}
async function recursiveFileSearch ( dir ) {
const fileList = [ ]
for ( const file of await fs . readdir ( dir , { withFileTypes : true } ) ) {
2023-06-21 02:04:25 -07:00
let pathDir = path . resolve ( dir , file . name )
2023-06-20 18:26:19 -07:00
if ( file . isFile ( ) ) {
2023-06-21 02:04:25 -07:00
fileList . push ( pathDir )
2023-06-20 18:26:19 -07:00
} else if ( file . isDirectory ( ) ) {
fileList . push ( ... ( await recursiveFileSearch ( pathDir ) ) )
} else {
logger . warn ( ` Found directory entry that is neither a file or directory ( ${ pathDir } ), ignoring! ` )
}
}
return fileList
}
2023-06-21 02:04:25 -07:00
const logger = new Logger ( {
name : "launcher" ,
logDebug : process . env . DEBUG == "true"
} ) ,
2023-06-20 18:26:19 -07:00
LINE _SEPERATOR = "-----------------------------------"
if ( ! process . env . REPL _SLUG ) {
logger . error ( LINE _SEPERATOR )
logger . error ( "Repl not detected!" )
logger . error ( "" )
logger . error ( "This file is meant to be ran in a Repl" )
logger . error ( LINE _SEPERATOR )
}
logger . info ( LINE _SEPERATOR )
logger . info ( "Checking if the proxy needs to be recompiled..." )
logger . info ( LINE _SEPERATOR )
fs . readFile ( path . join ( _ _dirname , ".sourcehash" ) )
. then ( data => {
let oldHash = data . toString ( )
logger . info ( "Found old hash, calculating hash of source files..." )
recursiveFileSearch ( sourceDir )
. then ( files => {
Promise . all ( files . map ( f => fs . readFile ( f ) ) )
. then ( data => {
const hash = crypto . createHash ( "sha256" )
data . forEach ( d => hash . update ( d ) )
let sourceHash = hash . digest ( ) . toString ( )
if ( sourceHash === oldHash ) {
logger . info ( "Source hasn't been changed, skipping compilation..." )
process . exit ( 0 )
} else {
logger . info ( "Source has been changed, recompiling..." )
2023-06-21 02:04:25 -07:00
fs . writeFile ( path . join ( _ _dirname , ".sourcehash" ) , sourceHash )
. then ( ( ) => {
process . exit ( 2 )
} )
. catch ( err => {
logger . error ( ` Could not write new hash to disk! \n ${ err . stack } ` )
process . exit ( 1 )
} )
2023-06-20 18:26:19 -07:00
}
} )
} )
. catch ( err => {
logger . error ( ` Could not calculate file hashes for files in directory ${ sourceDir } ! \n ${ err . stack } ` )
process . exit ( 1 )
} )
} )
. catch ( err => {
if ( err . code == "ENOENT" ) {
logger . warn ( "Previous source hash not found! Assuming a clean install is being used." )
2023-06-21 02:04:25 -07:00
logger . info ( "Calculating hash..." )
recursiveFileSearch ( sourceDir )
. then ( files => {
Promise . all ( files . map ( f => fs . readFile ( f ) ) )
. then ( data => {
const hash = crypto . createHash ( "sha256" )
data . forEach ( d => hash . update ( d ) )
let sourceHash = hash . digest ( ) . toString ( )
fs . writeFile ( path . join ( _ _dirname , ".sourcehash" ) , sourceHash )
. then ( ( ) => {
logger . info ( "Saved hash to disk." )
process . exit ( 2 )
} )
. catch ( err => {
logger . error ( ` Could not write new hash to disk! \n ${ err . stack } ` )
process . exit ( 1 )
} )
} )
} )
. catch ( err => {
logger . error ( ` Could not calculate file hashes for files in directory ${ sourceDir } ! \n ${ err . stack } ` )
process . exit ( 1 )
} )
2023-06-20 18:26:19 -07:00
} else {
logger . error ( ` Could not read .sourcehash file in ${ path . join ( _ _dirname , ".sourcehash" ) } due to an unknown error! Try again with a clean repl? \n ${ err . stack } ` )
2023-06-21 02:04:25 -07:00
process . exit ( 1 )
2023-06-20 18:26:19 -07:00
}
} )