From 857cb55159ef03dd71c558a3e728d5685c317be5 Mon Sep 17 00:00:00 2001 From: hanruo <552455797@qq.com> Date: Wed, 27 May 2026 16:21:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20"=E6=94=AF=E6=8C=81=E6=9B=B4=E6=96=B0"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/server-config.js | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/bin/server-config.js b/bin/server-config.js index 33271f5..2514d6f 100755 --- a/bin/server-config.js +++ b/bin/server-config.js @@ -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"); +}