diff --git a/.config/build.sh b/.config/build.sh old mode 100644 new mode 100755 index 8693842..659cbbe --- a/.config/build.sh +++ b/.config/build.sh @@ -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 + diff --git a/replit_launch.sh b/replit_launch.sh old mode 100644 new mode 100755 diff --git a/replit_runtime/config.js b/replit_runtime/config.js index b045050..33a9cbf 100644 --- a/replit_runtime/config.js +++ b/replit_runtime/config.js @@ -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")) } \ No newline at end of file diff --git a/replit_runtime/index.js b/replit_runtime/index.js index b8ae019..14b3bcc 100644 --- a/replit_runtime/index.js +++ b/replit_runtime/index.js @@ -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) } }) \ No newline at end of file diff --git a/replit_runtime/package.json b/replit_runtime/package.json new file mode 100644 index 0000000..75e7ea6 --- /dev/null +++ b/replit_runtime/package.json @@ -0,0 +1,5 @@ +{ + "name": "replit_runtime", + "description": "Runtime preparation utility for Repls. Determines whether or not a recompilation is necessary.", + "type": "commonjs" +} \ No newline at end of file