@codelia/cli 0.1.85 → 0.1.89

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.
@@ -0,0 +1,202 @@
1
+ import {
2
+ CLI_VERSION,
3
+ resolveCliPackageRoot,
4
+ resolveLoadedCliPackageRoot
5
+ } from "./chunk-3YQPBHM7.js";
6
+ import {
7
+ NOOP_LAUNCH_UPDATE_SESSION
8
+ } from "./chunk-QYWLK4HY.js";
9
+ import {
10
+ UPDATE_REEXEC_ENV
11
+ } from "./chunk-S7OVJIGV.js";
12
+
13
+ // src/tui/launcher.ts
14
+ import { spawn } from "child_process";
15
+ import fsSync from "fs";
16
+ import { createRequire } from "module";
17
+ import path from "path";
18
+ import { ensureStorageDirs, resolveStoragePaths } from "@codelia/storage";
19
+ var resolveTuiBinaryName = (platform) => platform === "win32" ? "codelia-tui.exe" : "codelia-tui";
20
+ var TUI_PACKAGE_BY_TARGET = {
21
+ "darwin-arm64": "@codelia/tui-darwin-arm64",
22
+ "darwin-x64": "@codelia/tui-darwin-x64",
23
+ "linux-arm64": "@codelia/tui-linux-arm64",
24
+ "linux-x64": "@codelia/tui-linux-x64",
25
+ "win32-x64": "@codelia/tui-win32-x64"
26
+ };
27
+ var describeError = (error) => error instanceof Error ? error.message : String(error);
28
+ var splitArgs = (value) => value.split(/\s+/).map((part) => part.trim()).filter((part) => part.length > 0);
29
+ var quoteShellWord = (value) => `'${value.replace(/'/g, "'\\''")}'`;
30
+ var isExecutable = (candidate) => {
31
+ try {
32
+ fsSync.accessSync(candidate, fsSync.constants.X_OK);
33
+ return true;
34
+ } catch {
35
+ return false;
36
+ }
37
+ };
38
+ var initStorage = async () => {
39
+ try {
40
+ const paths = resolveStoragePaths();
41
+ await ensureStorageDirs(paths);
42
+ } catch (error) {
43
+ console.warn(
44
+ `Failed to initialize storage directories: ${describeError(error)}`
45
+ );
46
+ }
47
+ };
48
+ var resolveRuntimeRequireBase = () => {
49
+ const cliPackageRoot = resolveCliPackageRoot();
50
+ if (cliPackageRoot) {
51
+ return path.resolve(cliPackageRoot, "package.json");
52
+ }
53
+ return path.resolve(process.cwd(), "package.json");
54
+ };
55
+ var runtimeRequire = createRequire(resolveRuntimeRequireBase());
56
+ var resolveBundledRuntimeEntry = () => {
57
+ try {
58
+ return runtimeRequire.resolve("@codelia/runtime");
59
+ } catch {
60
+ return null;
61
+ }
62
+ };
63
+ var resolveInstalledPackageJson = (packageName) => {
64
+ try {
65
+ return runtimeRequire.resolve(`${packageName}/package.json`);
66
+ } catch {
67
+ return null;
68
+ }
69
+ };
70
+ var resolvePlatformTuiPackageName = (platform = process.platform, arch = process.arch) => TUI_PACKAGE_BY_TARGET[`${platform}-${arch}`] ?? null;
71
+ var resolveOptionalTuiBinaryPath = (platform = process.platform, arch = process.arch, resolvePackageJson = resolveInstalledPackageJson) => {
72
+ const packageName = resolvePlatformTuiPackageName(platform, arch);
73
+ if (!packageName) {
74
+ return null;
75
+ }
76
+ const packageJsonPath = resolvePackageJson(packageName);
77
+ if (!packageJsonPath) {
78
+ return null;
79
+ }
80
+ return path.resolve(
81
+ path.dirname(packageJsonPath),
82
+ "bin",
83
+ resolveTuiBinaryName(platform)
84
+ );
85
+ };
86
+ var resolveRuntimeEnvForTui = (env, resolveRuntimeEntry = resolveBundledRuntimeEntry) => {
87
+ if (env.CODELIA_RUNTIME_CMD || env.CODELIA_RUNTIME_ARGS) {
88
+ return env;
89
+ }
90
+ const runtimeEntry = resolveRuntimeEntry();
91
+ if (!runtimeEntry) {
92
+ return env;
93
+ }
94
+ return {
95
+ ...env,
96
+ CODELIA_RUNTIME_CMD: process.execPath,
97
+ CODELIA_RUNTIME_ARGS: quoteShellWord(runtimeEntry)
98
+ };
99
+ };
100
+ var resolveLaunchEnvForTui = (env, cliVersion = CLI_VERSION, resolveRuntimeEntry = resolveBundledRuntimeEntry) => {
101
+ const runtimeEnv = resolveRuntimeEnvForTui(env, resolveRuntimeEntry);
102
+ if (runtimeEnv.CODELIA_CLI_VERSION || cliVersion.trim().length === 0) {
103
+ return runtimeEnv;
104
+ }
105
+ return {
106
+ ...runtimeEnv,
107
+ CODELIA_CLI_VERSION: cliVersion
108
+ };
109
+ };
110
+ var resolveTuiCommand = (options = {}) => {
111
+ const env = options.env ?? process.env;
112
+ if (env.CODELIA_TUI_CMD) {
113
+ return env.CODELIA_TUI_CMD;
114
+ }
115
+ const platform = options.platform ?? process.platform;
116
+ const arch = options.arch ?? process.arch;
117
+ const resolveOptionalBinary = options.resolveOptionalTuiBinary ?? resolveOptionalTuiBinaryPath;
118
+ const isExecutableCandidate = options.isExecutableCandidate ?? isExecutable;
119
+ const tuiBinaryName = resolveTuiBinaryName(platform);
120
+ const cwd = options.cwd ?? process.cwd();
121
+ const cliPackageRoot = options.cliPackageRoot === void 0 ? resolveCliPackageRoot() : options.cliPackageRoot;
122
+ const seen = /* @__PURE__ */ new Set();
123
+ const optionalTuiBinary = resolveOptionalBinary(platform, arch);
124
+ const localCandidates = [
125
+ ...cliPackageRoot ? [
126
+ path.resolve(cliPackageRoot, "dist/bin", tuiBinaryName),
127
+ path.resolve(
128
+ cliPackageRoot,
129
+ "../../crates/tui/target/release",
130
+ tuiBinaryName
131
+ ),
132
+ path.resolve(
133
+ cliPackageRoot,
134
+ "../../crates/tui/target/debug",
135
+ tuiBinaryName
136
+ )
137
+ ] : [],
138
+ path.resolve(cwd, "target/release", tuiBinaryName),
139
+ path.resolve(cwd, "target/debug", tuiBinaryName),
140
+ path.resolve(cwd, "crates/tui/target/release", tuiBinaryName),
141
+ path.resolve(cwd, "crates/tui/target/debug", tuiBinaryName)
142
+ ];
143
+ const candidates = [
144
+ ...localCandidates,
145
+ ...optionalTuiBinary ? [optionalTuiBinary] : []
146
+ ];
147
+ for (const candidate of candidates) {
148
+ if (seen.has(candidate)) continue;
149
+ seen.add(candidate);
150
+ if (isExecutableCandidate(candidate)) {
151
+ return candidate;
152
+ }
153
+ }
154
+ return tuiBinaryName;
155
+ };
156
+ var runTui = (args, update = NOOP_LAUNCH_UPDATE_SESSION, cliPackageRoot = resolveLoadedCliPackageRoot()) => {
157
+ void initStorage();
158
+ const tuiCmd = resolveTuiCommand({ cliPackageRoot });
159
+ const tuiArgs = process.env.CODELIA_TUI_ARGS ? splitArgs(process.env.CODELIA_TUI_ARGS) : [];
160
+ const tuiEnv = { ...resolveLaunchEnvForTui(process.env), ...update.tuiEnv };
161
+ delete tuiEnv[UPDATE_REEXEC_ENV];
162
+ const child = spawn(tuiCmd, [...tuiArgs, ...args], {
163
+ stdio: "inherit",
164
+ env: tuiEnv
165
+ });
166
+ process.once("exit", () => update.dispose());
167
+ child.once("spawn", () => {
168
+ void update.startBackground();
169
+ });
170
+ child.on("exit", (code) => {
171
+ update.dispose();
172
+ process.exitCode = code ?? 0;
173
+ });
174
+ child.on("error", (error) => {
175
+ update.dispose();
176
+ const code = typeof error === "object" && error !== null && "code" in error ? String(error.code ?? "") : "";
177
+ const expectedPackage = resolvePlatformTuiPackageName();
178
+ console.error(`Failed to launch TUI (${tuiCmd}): ${describeError(error)}`);
179
+ if (code === "EACCES") {
180
+ console.error(
181
+ "Permission denied while launching TUI. Verify execute permission and PATH entries."
182
+ );
183
+ } else if (code === "ENOENT") {
184
+ console.error(
185
+ expectedPackage ? `TUI binary not found. Ensure \`${expectedPackage}\` is installed, or run \`bun run build\` (or \`bun run tui:build\`) and retry.` : "TUI binary not found. Run `bun run build` (or `bun run tui:build`) and retry."
186
+ );
187
+ }
188
+ console.error(
189
+ "Set CODELIA_TUI_CMD/CODELIA_TUI_ARGS to use a custom TUI command."
190
+ );
191
+ process.exitCode = 1;
192
+ });
193
+ };
194
+
195
+ export {
196
+ resolvePlatformTuiPackageName,
197
+ resolveOptionalTuiBinaryPath,
198
+ resolveRuntimeEnvForTui,
199
+ resolveLaunchEnvForTui,
200
+ resolveTuiCommand,
201
+ runTui
202
+ };