Move a lot of shell stuff over to Node so that Windows users need not install git bash.

This commit is contained in:
00Fjongl 2024-08-07 13:16:29 -05:00
parent 92909e4ab7
commit bc858ebf22
4 changed files with 82 additions and 34 deletions

View file

@ -1,8 +0,0 @@
# File created to force npm execution in shell (instead of potentially Windows batch).
# This script can be run manually if esbuild is replaced by the following:
# ./node_modules/.bin/esbuild
# Make the ./views/dist directory in case it doesn't exist, and also make it empty.
dist='./views/dist'; mkdir "$dist"; find "$dist" -mindepth 1 -delete &&
# Curly bracket wildcards are not supported by some clients, so this is quite long.
esbuild --platform=browser --sourcemap --minify --bundle --external:*.png --external:*.jpg --external:*.jpeg --external:*.webp --external:*.svg --outdir="$dist" ./views/uv/*.js ./views/assets/js/*.js ./views/assets/js/*/*.js ./views/assets/css/*.css

View file

@ -7,11 +7,13 @@
"scripts": {
"start": "npm install && npm run build && npm run manual-start",
"restart": "npm stop ; npm run manual-start",
"stop": "( pm2 stop ecosystem.config.js ) ; node shutdown.mjs",
"stop": "node run-command.mjs stop",
"test": "npm run proxy-validator",
"monit": "pm2 monit",
"manual-start": "node read-config.mjs 'production' && ( pm2 start ecosystem.config.js --env production ) || ( node backend.js & pkill -n npm || true )",
"build": "sh ./build-assets.sh && cd lib/rammerhead && npm install && npm run build",
"pm2-start": "pm2 start ecosystem.config.js --env production",
"pm2-stop": "pm2 stop ecosystem.config.js",
"pm2-monit": "pm2 monit",
"manual-start": "node run-command.mjs start",
"build": "node run-command.mjs build && cd lib/rammerhead && npm install && npm run build",
"start-test-server": "timeout 5 node backend.js; test $? -eq 124 && ( npm run manual-start & ) || exit 1",
"proxy-validator": "node proxyServiceValidator.js"
},

75
run-command.mjs Normal file
View file

@ -0,0 +1,75 @@
import { readFile, writeFile, unlink, mkdir, rm } from 'node:fs/promises';
import { exec, fork } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { build } from 'esbuild';
const config = Object.freeze(JSON.parse(await readFile(new URL("./src/config.json", import.meta.url))));
const serverUrl = (base => {
try {
base = new URL(config.host);
} catch (e) {
base = new URL("http://a");
base.host = config.host;
}
base.port = process.env.PORT || config.port;
return Object.freeze(base);
})();
const shutdown = new URL("./src/.shutdown", import.meta.url);
for(let i = 2; i < process.argv.length; i++)
switch (process.argv[i]) {
case "start": {
if (config.production)
exec("npm run pm2-start", (error, stdout) => {
if (error) throw error;
console.log(stdout);
});
else {
const server = fork(
new URL("./backend.js", import.meta.url),
{detached: true}
);
server.unref();
server.disconnect();
}
break;
}
case "stop":
await writeFile(shutdown, "");
try {await fetch(new URL("/test-shutdown", serverUrl))}
catch (e) {await unlink(shutdown)}
if (config.production)
exec("npm run pm2-stop", (error, stdout) => {
if (error) throw error;
console.log(stdout);
});
break;
case "build": {
const dist = fileURLToPath(new URL("./views/dist", import.meta.url));
await rm(dist, {force: true, recursive: true});
await mkdir(dist);
await build({
entryPoints: [
"./views/uv/**/*.js",
"./views/assets/js/**/*.js",
"./views/assets/css/**/*.css"
],
platform: "browser",
sourcemap: true,
bundle: true,
minify: true,
external: ["*.png", "*.jpg", "*.jpeg", "*.webp", "*.svg"],
outdir: dist
});
break;
}
// No default case.
}
process.exitCode = 0;

View file

@ -1,21 +0,0 @@
import { readFile, writeFile, unlink } from 'node:fs/promises';
const config = Object.freeze(JSON.parse(await readFile(new URL("./src/config.json", import.meta.url))));
const serverUrl = (base => {
try {
base = new URL(config.host);
} catch (e) {
base = new URL("http://a");
base.host = config.host;
}
base.port = process.env.PORT || config.port;
return Object.freeze(base);
})();
const shutdown = new URL("./src/.shutdown", import.meta.url);
await writeFile(shutdown, "");
try {await fetch(new URL("/test-shutdown", serverUrl))}
catch (e) {await unlink(shutdown)}
process.exitCode = 0;