The bot will now only re-compile when changed
The bot will now only re-compile when the plugins or source code changes. (computed using a hash) This reduces the average startup time by 50%.
This commit is contained in:
@@ -1,16 +1,92 @@
|
|||||||
const fs = require("fs")
|
|
||||||
const ts = require("typescript")
|
|
||||||
const nodepath = require('path')
|
|
||||||
|
|
||||||
/////////////// STARTUP FLAGS ///////////////
|
/////////////// STARTUP FLAGS ///////////////
|
||||||
const flags = [
|
const flags = [
|
||||||
|
// Edit flags here when being unable to use the flags in the command prompt.
|
||||||
//PTERODACTYL PANEL
|
//PTERODACTYL PANEL
|
||||||
//add startup flags here (e.g. "--no-compile") when running via the panel
|
//add startup flags here (e.g. "--no-compile") when running via the panel
|
||||||
]
|
]
|
||||||
process.argv.push(...flags)
|
process.argv.push(...flags)
|
||||||
/////////////// STARTUP FLAGS ///////////////
|
/////////////// STARTUP FLAGS ///////////////
|
||||||
|
|
||||||
|
/*
|
||||||
|
██████╗ ██████╗ ███████╗███╗ ██╗ ████████╗██╗ ██████╗██╗ ██╗███████╗████████╗
|
||||||
|
██╔═══██╗██╔══██╗██╔════╝████╗ ██║ ╚══██╔══╝██║██╔════╝██║ ██╔╝██╔════╝╚══██╔══╝
|
||||||
|
██║ ██║██████╔╝█████╗ ██╔██╗ ██║ ██║ ██║██║ █████╔╝ █████╗ ██║
|
||||||
|
██║ ██║██╔═══╝ ██╔══╝ ██║╚██╗██║ ██║ ██║██║ ██╔═██╗ ██╔══╝ ██║
|
||||||
|
╚██████╔╝██║ ███████╗██║ ╚████║ ██║ ██║╚██████╗██║ ██╗███████╗ ██║
|
||||||
|
╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝╚══════╝ ╚═╝
|
||||||
|
v4.0.0 - Made by DJj123dj & Contributors
|
||||||
|
|
||||||
|
Discord: https://discord.dj-dj.be
|
||||||
|
Docs: https://otdocs.dj-dj.be
|
||||||
|
Support Us: https://github.com/sponsors/DJj123dj/
|
||||||
|
|
||||||
|
*/
|
||||||
|
///////////////////////////////////////////
|
||||||
|
////////// COMPILATION + STARTUP //////////
|
||||||
|
///////////////////////////////////////////
|
||||||
|
const fs = require("fs")
|
||||||
|
const ts = require("typescript")
|
||||||
|
const {createHash,Hash} = require("crypto")
|
||||||
|
const nodepath = require('path')
|
||||||
|
|
||||||
|
/** ## What is this?
|
||||||
|
* This is a function which compares `./src/` with a hash stored in `./dist/hash.txt`.
|
||||||
|
* The hash is based on the modified date & file metadata of all files in `./src/`.
|
||||||
|
*
|
||||||
|
* If the hash is different, the bot will automatically re-compile.
|
||||||
|
* This will help you save CPU resources because the bot shouldn't re-compile when nothing has been changed :)
|
||||||
|
*
|
||||||
|
* @param {string} dir
|
||||||
|
* @param {Hash|null} upperHash
|
||||||
|
*/
|
||||||
|
function computeSourceHash(dir,upperHash){
|
||||||
|
const hash = upperHash ? upperHash : createHash("sha256")
|
||||||
|
const info = fs.readdirSync(dir,{withFileTypes:true})
|
||||||
|
|
||||||
|
for (const file of info) {
|
||||||
|
const fullPath = nodepath.join(dir,file.name)
|
||||||
|
if (file.isFile()){
|
||||||
|
const statInfo = fs.statSync(fullPath)
|
||||||
|
//compute hash using file metadata
|
||||||
|
const fileInfo = `${fullPath}:${statInfo.size}:${statInfo.mtimeMs}`
|
||||||
|
hash.update(fileInfo)
|
||||||
|
|
||||||
|
}else if (file.isDirectory()){
|
||||||
|
//recursively compute all folders
|
||||||
|
computeSourceHash(fullPath,hash)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//return when not being called recursively
|
||||||
|
if (!upperHash) {
|
||||||
|
return hash.digest("hex")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function requiresCompilation(){
|
||||||
|
//check hashes when not using "--compile-only" flag
|
||||||
|
if (process.argv.includes("--compile-only")) return true
|
||||||
|
|
||||||
|
console.log("OT: Comparing prebuilds with source...")
|
||||||
|
const sourceHash = computeSourceHash("./src/")
|
||||||
|
const pluginHash = computeSourceHash("./plugins/")
|
||||||
|
const hash = sourceHash+":"+pluginHash
|
||||||
|
|
||||||
|
if (fs.existsSync("./dist/hash.txt")){
|
||||||
|
const distHash = fs.readFileSync("./dist/hash.txt").toString()
|
||||||
|
if (distHash === hash) return false
|
||||||
|
else return true
|
||||||
|
}else return true
|
||||||
|
}
|
||||||
|
function saveNewCompilationHash(){
|
||||||
|
const sourceHash = computeSourceHash("./src/")
|
||||||
|
const pluginHash = computeSourceHash("./plugins/")
|
||||||
|
const hash = sourceHash+":"+pluginHash
|
||||||
|
fs.writeFileSync("./dist/hash.txt",hash)
|
||||||
|
}
|
||||||
|
|
||||||
if (!process.argv.includes("--no-compile")){
|
if (!process.argv.includes("--no-compile")){
|
||||||
|
if (requiresCompilation()){
|
||||||
|
console.log("OT: Compilation Required...")
|
||||||
|
|
||||||
//REMOVE EXISTING BUILDS
|
//REMOVE EXISTING BUILDS
|
||||||
console.log("OT: Removing Prebuilds...")
|
console.log("OT: Removing Prebuilds...")
|
||||||
fs.rmSync("./dist",{recursive:true,force:true})
|
fs.rmSync("./dist",{recursive:true,force:true})
|
||||||
@@ -48,6 +124,10 @@ if (!process.argv.includes("--no-compile")){
|
|||||||
console.log("OT: Compilation Failed!")
|
console.log("OT: Compilation Failed!")
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
}else console.log("OT: No Compilation Required...")
|
||||||
|
|
||||||
|
//save new compilation hash
|
||||||
|
saveNewCompilationHash()
|
||||||
}
|
}
|
||||||
|
|
||||||
//START BOT
|
//START BOT
|
||||||
|
|||||||
Reference in New Issue
Block a user