@commandgarden/cli 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.js +62 -22
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -8,6 +8,8 @@ var __export = (target, all) => {
|
|
|
8
8
|
// src/main.ts
|
|
9
9
|
import { Command } from "commander";
|
|
10
10
|
import { join as join3, dirname } from "path";
|
|
11
|
+
import { existsSync as existsSync4 } from "fs";
|
|
12
|
+
import { createRequire } from "module";
|
|
11
13
|
import { homedir } from "os";
|
|
12
14
|
import { fileURLToPath } from "url";
|
|
13
15
|
|
|
@@ -4872,7 +4874,7 @@ async function executeConfigSet(client, key, value) {
|
|
|
4872
4874
|
|
|
4873
4875
|
// src/commands/gui-cmd.ts
|
|
4874
4876
|
import { spawn as spawn2 } from "child_process";
|
|
4875
|
-
import { readFileSync as readFileSync5, writeFileSync as writeFileSync2, existsSync as existsSync3, unlinkSync as unlinkSync2, mkdirSync as mkdirSync2 } from "fs";
|
|
4877
|
+
import { readFileSync as readFileSync5, writeFileSync as writeFileSync2, existsSync as existsSync3, unlinkSync as unlinkSync2, mkdirSync as mkdirSync2, openSync, closeSync } from "fs";
|
|
4876
4878
|
import { join as join2 } from "path";
|
|
4877
4879
|
import { exec } from "child_process";
|
|
4878
4880
|
import { parse as parseYaml2 } from "yaml";
|
|
@@ -4907,35 +4909,33 @@ async function executeGuiStart(baseUrl, cgHome, appScript, opts) {
|
|
|
4907
4909
|
}
|
|
4908
4910
|
mkdirSync2(cgHome, { recursive: true });
|
|
4909
4911
|
if (opts.background) {
|
|
4910
|
-
const
|
|
4912
|
+
const logPath = join2(cgHome, "app.log");
|
|
4913
|
+
const logFd = openSync(logPath, "a");
|
|
4914
|
+
const child2 = spawn2("node", [appScript], { detached: true, stdio: ["ignore", logFd, logFd] });
|
|
4915
|
+
closeSync(logFd);
|
|
4911
4916
|
if (child2.pid) writeFileSync2(pidPath, String(child2.pid));
|
|
4912
4917
|
child2.unref();
|
|
4913
4918
|
if (!opts.noOpen) {
|
|
4914
4919
|
const appUrl = `http://127.0.0.1:${appPort}`;
|
|
4915
|
-
|
|
4916
|
-
|
|
4917
|
-
|
|
4918
|
-
|
|
4920
|
+
const ready = await waitForServer(appUrl, child2);
|
|
4921
|
+
if (ready) {
|
|
4922
|
+
openBrowser(appUrl);
|
|
4923
|
+
} else {
|
|
4924
|
+
if (child2.pid) try {
|
|
4925
|
+
process.kill(child2.pid, "SIGTERM");
|
|
4919
4926
|
} catch {
|
|
4920
4927
|
}
|
|
4921
|
-
|
|
4928
|
+
if (existsSync3(pidPath)) unlinkSync2(pidPath);
|
|
4929
|
+
return `GUI failed to start. Check ${logPath} for errors.`;
|
|
4922
4930
|
}
|
|
4923
|
-
openBrowser(appUrl);
|
|
4924
4931
|
}
|
|
4925
4932
|
return `GUI started (PID: ${child2.pid ?? "unknown"}).`;
|
|
4926
4933
|
}
|
|
4927
4934
|
const child = spawn2("node", [appScript], { stdio: "inherit" });
|
|
4928
4935
|
if (!opts.noOpen) {
|
|
4929
4936
|
const appUrl = `http://127.0.0.1:${appPort}`;
|
|
4930
|
-
|
|
4931
|
-
|
|
4932
|
-
await fetch(appUrl);
|
|
4933
|
-
break;
|
|
4934
|
-
} catch {
|
|
4935
|
-
}
|
|
4936
|
-
await new Promise((r) => setTimeout(r, 250));
|
|
4937
|
-
}
|
|
4938
|
-
openBrowser(appUrl);
|
|
4937
|
+
const ready = await waitForServer(appUrl, child);
|
|
4938
|
+
if (ready) openBrowser(appUrl);
|
|
4939
4939
|
}
|
|
4940
4940
|
await new Promise((resolve) => child.on("exit", () => resolve()));
|
|
4941
4941
|
return "GUI stopped.";
|
|
@@ -4964,6 +4964,22 @@ function executeGuiStatus(cgHome) {
|
|
|
4964
4964
|
return "GUI: not running (stale PID file).";
|
|
4965
4965
|
}
|
|
4966
4966
|
}
|
|
4967
|
+
async function waitForServer(url, child) {
|
|
4968
|
+
for (let i = 0; i < 40; i++) {
|
|
4969
|
+
try {
|
|
4970
|
+
if (child.pid) process.kill(child.pid, 0);
|
|
4971
|
+
} catch {
|
|
4972
|
+
return false;
|
|
4973
|
+
}
|
|
4974
|
+
try {
|
|
4975
|
+
await fetch(url);
|
|
4976
|
+
return true;
|
|
4977
|
+
} catch {
|
|
4978
|
+
}
|
|
4979
|
+
await new Promise((r) => setTimeout(r, 500));
|
|
4980
|
+
}
|
|
4981
|
+
return false;
|
|
4982
|
+
}
|
|
4967
4983
|
function openBrowser(url) {
|
|
4968
4984
|
const cmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "start" : "xdg-open";
|
|
4969
4985
|
exec(`${cmd} ${url}`);
|
|
@@ -4995,8 +5011,32 @@ async function executeDown(cgHome) {
|
|
|
4995
5011
|
// src/main.ts
|
|
4996
5012
|
var __filename = fileURLToPath(import.meta.url);
|
|
4997
5013
|
var __dirname = dirname(__filename);
|
|
4998
|
-
|
|
4999
|
-
|
|
5014
|
+
function resolveScript(relativePath, packageName, entryPoint) {
|
|
5015
|
+
const relative = join3(__dirname, relativePath);
|
|
5016
|
+
if (existsSync4(relative)) return relative;
|
|
5017
|
+
try {
|
|
5018
|
+
const require2 = createRequire(import.meta.url);
|
|
5019
|
+
const pkgJsonPath = require2.resolve(`${packageName}/package.json`);
|
|
5020
|
+
const resolved = join3(dirname(pkgJsonPath), entryPoint);
|
|
5021
|
+
if (existsSync4(resolved)) return resolved;
|
|
5022
|
+
} catch {
|
|
5023
|
+
}
|
|
5024
|
+
console.error(`Cannot find ${packageName} entry point.`);
|
|
5025
|
+
console.error(`Looked at:
|
|
5026
|
+
- ${relative}`);
|
|
5027
|
+
console.error(`
|
|
5028
|
+
If @commandgarden/cli is installed globally, also run:`);
|
|
5029
|
+
console.error(` cd <monorepo>/commandGarden/${packageName.split("/").pop()} && npm link`);
|
|
5030
|
+
process.exit(1);
|
|
5031
|
+
}
|
|
5032
|
+
var _daemonScript;
|
|
5033
|
+
function getDaemonScript() {
|
|
5034
|
+
return _daemonScript ??= resolveScript("../../daemon/dist/main.js", "@commandgarden/daemon", "dist/main.js");
|
|
5035
|
+
}
|
|
5036
|
+
var _appScript;
|
|
5037
|
+
function getAppScript() {
|
|
5038
|
+
return _appScript ??= resolveScript("../../app/dist/server/main.js", "@commandgarden/app", "dist/server/main.js");
|
|
5039
|
+
}
|
|
5000
5040
|
var CG_HOME = join3(homedir(), ".commandgarden");
|
|
5001
5041
|
var TOKEN_PATH = join3(CG_HOME, "session-token");
|
|
5002
5042
|
var CONFIG_PATH = join3(CG_HOME, "config.yaml");
|
|
@@ -5033,7 +5073,7 @@ daemon.command("status").description("Check daemon status").action(async () => {
|
|
|
5033
5073
|
console.log(await executeDaemonStatus(BASE_URL));
|
|
5034
5074
|
});
|
|
5035
5075
|
daemon.command("start").description("Start the daemon in background").action(async () => {
|
|
5036
|
-
console.log(await executeDaemonStart(BASE_URL, CG_HOME,
|
|
5076
|
+
console.log(await executeDaemonStart(BASE_URL, CG_HOME, getDaemonScript()));
|
|
5037
5077
|
});
|
|
5038
5078
|
daemon.command("stop").description("Stop the daemon").action(async () => {
|
|
5039
5079
|
console.log(await executeDaemonStop(CG_HOME));
|
|
@@ -5070,7 +5110,7 @@ config.command("set <key> <value>").description("Set a configuration value (e.g.
|
|
|
5070
5110
|
});
|
|
5071
5111
|
var gui = program.command("gui").description("Manage the GUI app server");
|
|
5072
5112
|
gui.command("start", { isDefault: true }).description("Start the GUI (foreground by default)").option("-b, --background", "Run in background").option("--no-open", "Do not open browser").action(async (opts) => {
|
|
5073
|
-
console.log(await executeGuiStart(BASE_URL, CG_HOME,
|
|
5113
|
+
console.log(await executeGuiStart(BASE_URL, CG_HOME, getAppScript(), {
|
|
5074
5114
|
background: opts.background,
|
|
5075
5115
|
noOpen: opts.open === false,
|
|
5076
5116
|
configPath: CONFIG_PATH
|
|
@@ -5083,7 +5123,7 @@ gui.command("status").description("Check GUI status").action(() => {
|
|
|
5083
5123
|
console.log(executeGuiStatus(CG_HOME));
|
|
5084
5124
|
});
|
|
5085
5125
|
program.command("up").description("Start daemon + GUI, open browser").action(async () => {
|
|
5086
|
-
console.log(await executeUp(BASE_URL, CG_HOME,
|
|
5126
|
+
console.log(await executeUp(BASE_URL, CG_HOME, getDaemonScript(), getAppScript(), CONFIG_PATH));
|
|
5087
5127
|
});
|
|
5088
5128
|
program.command("down").description("Stop GUI + daemon").action(async () => {
|
|
5089
5129
|
console.log(await executeDown(CG_HOME));
|