added smart compilation to decrease startup time

This commit is contained in:
WorldEditAxe 2023-06-21 09:04:25 +00:00
parent 81f190ed87
commit 1f37152843
5 changed files with 75 additions and 14 deletions

38
.config/build.sh Normal file → Executable file
View File

@ -1,11 +1,33 @@
#!/usr/bin/env bash
set -e
rm -rf .build
mkdir -p ./.build
cd ~/$REPL_SLUG/
npm install typescript
echo "installing dependencies..."
npm install
npx tsc
echo "compiling eaglerproxy, please wait... "
echo "this may take a while when you first compile the proxy, or when the proxy has to be recompiled (usually due to modification of anything in the src folder, including config.ts)"
echo "after the initial compile, startups will be a lot more faster and snappier"
set +e
cd ~/$REPL_SLUG/replit_runtime
node ./index.js
exit_code=$?
if [ $exit_code -eq 0 ]; then
set -e
echo "detected that recompilation is not required, skipping and directly launching..."
elif [ $exit_code -eq 2 ]; then
set -e
echo "recompiling proxy..."
rm -rf .build
mkdir -p ./.build
cd ~/$REPL_SLUG/
npm install typescript
npx tsc
echo "finished compiling, launching..."
else
echo "received non-zero exit code ($?), exiting!"
set -e
exit 1
fi

0
replit_launch.sh Normal file → Executable file
View File

View File

@ -1,5 +1,6 @@
const path = require("path")
const os = require("os")
module.exports = {
sourceDir: path.resolve("~", process.env.REPL_SLUG)
sourceDir: path.resolve(os.homedir(), path.join(process.env.REPL_SLUG, "src"))
}

View File

@ -39,9 +39,9 @@ class Logger {
async function recursiveFileSearch(dir) {
const fileList = []
for (const file of await fs.readdir(dir, { withFileTypes: true })) {
let pathDir = path.resolve(dir, file)
let pathDir = path.resolve(dir, file.name)
if (file.isFile()) {
fileList.push(file)
fileList.push(pathDir)
} else if (file.isDirectory()) {
fileList.push(...(await recursiveFileSearch(pathDir)))
} else {
@ -51,7 +51,10 @@ async function recursiveFileSearch(dir) {
return fileList
}
const logger = new Logger("Launcher", process.env.DEBUG == "true"),
const logger = new Logger({
name: "launcher",
logDebug: process.env.DEBUG == "true"
}),
LINE_SEPERATOR = "-----------------------------------"
if (!process.env.REPL_SLUG) {
@ -83,7 +86,14 @@ fs.readFile(path.join(__dirname, ".sourcehash"))
process.exit(0)
} else {
logger.info("Source has been changed, recompiling...")
process.exit(2)
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)
})
}
})
})
@ -95,8 +105,31 @@ fs.readFile(path.join(__dirname, ".sourcehash"))
.catch(err => {
if (err.code == "ENOENT") {
logger.warn("Previous source hash not found! Assuming a clean install is being used.")
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)
})
} 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}`)
process.exit(2)
process.exit(1)
}
})

View File

@ -0,0 +1,5 @@
{
"name": "replit_runtime",
"description": "Runtime preparation utility for Repls. Determines whether or not a recompilation is necessary.",
"type": "commonjs"
}