This commit is contained in:
@@ -3,9 +3,19 @@
|
||||
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const { spawnSync } = require("node:child_process");
|
||||
const { pathToFileURL } = require("node:url");
|
||||
|
||||
const bundle = path.join(__dirname, "..", "dist", "app.mjs");
|
||||
const packageRoot = path.join(__dirname, "..");
|
||||
const bundle = path.join(packageRoot, "dist", "app.mjs");
|
||||
|
||||
// `server-config update` self-updates the checkout: pull, reinstall deps, rebuild.
|
||||
// It runs outside the TUI because it must not depend on (or fight with) the bundle
|
||||
// it is about to replace.
|
||||
if (process.argv[2] === "update") {
|
||||
runUpdate();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!fs.existsSync(bundle)) {
|
||||
process.stderr.write(
|
||||
@@ -18,3 +28,34 @@ import(pathToFileURL(bundle).href).catch((error) => {
|
||||
process.stderr.write(`server-config: ${error?.stack || error}\n`);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
function runUpdate() {
|
||||
if (!fs.existsSync(path.join(packageRoot, ".git"))) {
|
||||
process.stderr.write(
|
||||
"server-config: cannot update — package root is not a git checkout.\n",
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const npm = process.platform === "win32" ? "npm.cmd" : "npm";
|
||||
const steps = [
|
||||
["git", ["pull", "--ff-only"]],
|
||||
[npm, ["install"]],
|
||||
[npm, ["run", "build"]],
|
||||
];
|
||||
|
||||
for (const [cmd, args] of steps) {
|
||||
process.stdout.write(`\n$ ${cmd} ${args.join(" ")}\n`);
|
||||
const result = spawnSync(cmd, args, { cwd: packageRoot, stdio: "inherit" });
|
||||
if (result.error) {
|
||||
process.stderr.write(`server-config: failed to run ${cmd}: ${result.error.message}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
if (result.status !== 0) {
|
||||
process.stderr.write(`server-config: \`${cmd} ${args.join(" ")}\` exited with code ${result.status}\n`);
|
||||
process.exit(result.status || 1);
|
||||
}
|
||||
}
|
||||
|
||||
process.stdout.write("\nserver-config: update complete.\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user