Files
server-config-cli/bin/server-config.js
hanruo 857cb55159
All checks were successful
release / release (push) Successful in 17s
feat: "支持更新"
2026-05-27 16:21:08 +08:00

62 lines
1.8 KiB
JavaScript
Executable File

#!/usr/bin/env node
"use strict";
const fs = require("node:fs");
const path = require("node:path");
const { spawnSync } = require("node:child_process");
const { pathToFileURL } = require("node:url");
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(
"server-config: bundle not built. Run `npm run build` from the package root first.\n",
);
process.exit(1);
}
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");
}