@codelia/cli 0.1.86 → 0.1.90

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,66 @@
1
+ // src/commands/auth.ts
2
+ import {
3
+ authLockPathFor,
4
+ CODELIA_AUTH_KEYCHAIN_SERVICE,
5
+ CODELIA_MCP_AUTH_KEYCHAIN_SERVICE,
6
+ KeychainError,
7
+ keychainAccountForConfigDir,
8
+ MacOsKeychainClient,
9
+ resolveStoragePaths,
10
+ withAuthStoreLock
11
+ } from "@codelia/storage";
12
+ var USAGE = "usage: codelia auth authorize-keychain";
13
+ var runAuthCommand = async (args) => {
14
+ if (args.length === 1 && ["-h", "--help", "help"].includes(args[0])) {
15
+ console.log(USAGE);
16
+ return 0;
17
+ }
18
+ if (args.length !== 1 || args[0] !== "authorize-keychain") {
19
+ console.error(USAGE);
20
+ return 1;
21
+ }
22
+ if (process.platform !== "darwin" || !process.stdin.isTTY || !process.stderr.isTTY) {
23
+ console.error("Run this command from a local macOS terminal.");
24
+ return 1;
25
+ }
26
+ try {
27
+ const paths = resolveStoragePaths();
28
+ const client = new MacOsKeychainClient();
29
+ const account = keychainAccountForConfigDir(paths.configDir);
30
+ const probe = await client.probe();
31
+ if (!probe.available) {
32
+ console.error(probe.message);
33
+ return 1;
34
+ }
35
+ console.error(
36
+ "If macOS requests access, choose Always Allow to authorize Keychain access."
37
+ );
38
+ let failed = false;
39
+ await withAuthStoreLock(authLockPathFor(paths.credentialsDir), async () => {
40
+ for (const service of [
41
+ CODELIA_AUTH_KEYCHAIN_SERVICE,
42
+ CODELIA_MCP_AUTH_KEYCHAIN_SERVICE
43
+ ]) {
44
+ try {
45
+ const found = await client.authorize(service, account);
46
+ console.log(
47
+ `${service}: ${found ? "access granted" : "no item found"}`
48
+ );
49
+ } catch (error) {
50
+ failed = true;
51
+ const message = error instanceof KeychainError ? error.message : "Keychain authorization failed.";
52
+ console.error(`${service}: ${message}`);
53
+ }
54
+ }
55
+ });
56
+ return failed ? 1 : 0;
57
+ } catch (error) {
58
+ console.error(
59
+ error instanceof KeychainError ? error.message : "Keychain authorization failed."
60
+ );
61
+ return 1;
62
+ }
63
+ };
64
+ export {
65
+ runAuthCommand
66
+ };
@@ -0,0 +1,202 @@
1
+ import {
2
+ CLI_VERSION,
3
+ resolveCliPackageRoot,
4
+ resolveLoadedCliPackageRoot
5
+ } from "./chunk-PG452V5E.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
+ };
@@ -0,0 +1,45 @@
1
+ // src/package-root.ts
2
+ import fsSync from "fs";
3
+ import path from "path";
4
+ import { fileURLToPath } from "url";
5
+ var installedCliRoot = (file) => {
6
+ let candidate = file;
7
+ while (path.dirname(candidate) !== candidate) {
8
+ const scope = path.dirname(candidate);
9
+ if (path.basename(candidate) === "cli" && path.basename(scope) === "@codelia" && path.basename(path.dirname(scope)) === "node_modules") {
10
+ return candidate;
11
+ }
12
+ candidate = scope;
13
+ }
14
+ return null;
15
+ };
16
+ var resolveCliPackageRoot = (scriptPath = process.argv[1]) => {
17
+ if (!scriptPath) return null;
18
+ try {
19
+ const resolvedScriptPath = fsSync.realpathSync(scriptPath);
20
+ const installed = installedCliRoot(resolvedScriptPath);
21
+ if (installed) return installed;
22
+ const scriptDir = path.dirname(resolvedScriptPath);
23
+ if (path.basename(scriptDir) === "dist") {
24
+ return path.dirname(scriptDir);
25
+ }
26
+ return scriptDir;
27
+ } catch {
28
+ return null;
29
+ }
30
+ };
31
+ var resolveLoadedCliPackageRoot = () => {
32
+ const fromArgv = resolveCliPackageRoot();
33
+ if (fromArgv) return fromArgv;
34
+ const moduleDir = typeof __dirname === "string" ? __dirname : path.dirname(fileURLToPath(import.meta.url));
35
+ return installedCliRoot(moduleDir) ?? (path.basename(moduleDir) === "dist" ? path.dirname(moduleDir) : null);
36
+ };
37
+
38
+ // src/version.ts
39
+ var CLI_VERSION = "0.1.90".trim().length > 0 ? "0.1.90" : "0.0.0";
40
+
41
+ export {
42
+ resolveCliPackageRoot,
43
+ resolveLoadedCliPackageRoot,
44
+ CLI_VERSION
45
+ };
@@ -0,0 +1,156 @@
1
+ import {
2
+ UPDATE_NOTICE_FILE_ENV,
3
+ UPDATE_REEXEC_ENV,
4
+ applyUpdate,
5
+ buildStartupNotices,
6
+ computeRegistryId,
7
+ detectInstallMethod,
8
+ isMatchingCheckScope,
9
+ isNewerVersion,
10
+ isValidVersion,
11
+ launcherEntry,
12
+ readUpdateState,
13
+ reexecLauncher,
14
+ removeNoticeFileSync,
15
+ resolveEffectiveChannel,
16
+ resolveRegistryUrl,
17
+ resolveUpdatePaths,
18
+ resolveUpdateSettings,
19
+ runRegistryCheck,
20
+ shouldCheckNow,
21
+ writeStartupNoticeFile
22
+ } from "./chunk-S7OVJIGV.js";
23
+
24
+ // src/update/launch.ts
25
+ import { resolveStoragePaths } from "@codelia/storage";
26
+ var NOOP_LAUNCH_UPDATE_SESSION = {
27
+ tuiEnv: {},
28
+ startBackground: async () => void 0,
29
+ dispose: () => void 0
30
+ };
31
+ var tryResolveRegistryId = (env) => {
32
+ try {
33
+ return computeRegistryId(resolveRegistryUrl(env));
34
+ } catch {
35
+ return null;
36
+ }
37
+ };
38
+ var prepareTuiLaunch = async (options) => {
39
+ const env = options.env ?? process.env;
40
+ const interactive = options.interactive ?? Boolean(process.stdin.isTTY && process.stdout.isTTY);
41
+ const storage = resolveStoragePaths();
42
+ const paths = resolveUpdatePaths(options.cacheDir ?? storage.cacheDir);
43
+ const configFile = options.configFile ?? storage.configFile;
44
+ const settings = await resolveUpdateSettings({
45
+ env,
46
+ interactive,
47
+ configFile
48
+ });
49
+ const method = options.packageRoot ? detectInstallMethod({
50
+ packageRoot: options.packageRoot,
51
+ env,
52
+ resolveNpm: options.resolveNpm
53
+ }) : null;
54
+ const rawReexecFrom = env[UPDATE_REEXEC_ENV];
55
+ const reexecFrom = isValidVersion(rawReexecFrom) ? rawReexecFrom : void 0;
56
+ if (settings.mode === "off" && !reexecFrom) {
57
+ return { kind: "launch", session: NOOP_LAUNCH_UPDATE_SESSION };
58
+ }
59
+ let state = await readUpdateState(paths.stateFile);
60
+ const currentRegistryId = tryResolveRegistryId(env);
61
+ const effectiveChannel = resolveEffectiveChannel(
62
+ options.currentVersion,
63
+ settings.channel
64
+ );
65
+ const cacheMatchesScope = currentRegistryId !== null && isMatchingCheckScope(state, {
66
+ channel: effectiveChannel,
67
+ configuredChannel: settings.channel,
68
+ registryId: currentRegistryId
69
+ });
70
+ const cachedTarget = cacheMatchesScope ? state.latest_version : void 0;
71
+ const canAutoApply = settings.mode === "auto" && !reexecFrom && Boolean(method?.autoApply.supported) && Boolean(cachedTarget) && isNewerVersion(cachedTarget, options.currentVersion) && state.apply_failure?.version !== cachedTarget;
72
+ if (canAutoApply && method && cachedTarget) {
73
+ const applied = await applyUpdate({
74
+ paths,
75
+ method,
76
+ from: options.currentVersion,
77
+ to: cachedTarget,
78
+ configuredChannel: settings.channel,
79
+ env,
80
+ runner: options.runner,
81
+ now: options.now
82
+ });
83
+ if (applied.status === "installed") {
84
+ const reexec = options.reexec ?? reexecLauncher;
85
+ const exitCode = await reexec({
86
+ entry: launcherEntry(method.packageRoot),
87
+ args: options.args,
88
+ env: {
89
+ ...env,
90
+ [UPDATE_REEXEC_ENV]: options.currentVersion
91
+ }
92
+ });
93
+ if (exitCode !== null) {
94
+ return { kind: "exit", exitCode };
95
+ }
96
+ }
97
+ state = await readUpdateState(paths.stateFile);
98
+ }
99
+ const notices = buildStartupNotices(
100
+ cacheMatchesScope ? state : { version: 1 },
101
+ {
102
+ currentVersion: options.currentVersion,
103
+ mode: settings.mode,
104
+ method,
105
+ reexecFrom
106
+ }
107
+ );
108
+ let noticeFile = null;
109
+ try {
110
+ noticeFile = await writeStartupNoticeFile(paths, notices);
111
+ } catch {
112
+ noticeFile = null;
113
+ }
114
+ const abortController = new AbortController();
115
+ const startBackground = async () => {
116
+ if (settings.mode === "off" || method?.kind === "development") return;
117
+ const regId = tryResolveRegistryId(env);
118
+ if (!regId) return;
119
+ const latestState = await readUpdateState(paths.stateFile);
120
+ if (!shouldCheckNow(latestState, {
121
+ now: options.now?.(),
122
+ currentVersion: options.currentVersion,
123
+ configuredChannel: settings.channel,
124
+ registryId: regId
125
+ })) {
126
+ return;
127
+ }
128
+ await runRegistryCheck({
129
+ paths,
130
+ currentVersion: options.currentVersion,
131
+ configuredChannel: settings.channel,
132
+ env,
133
+ fetchImpl: options.fetchImpl,
134
+ signal: abortController.signal,
135
+ now: options.now
136
+ }).catch(() => void 0);
137
+ };
138
+ const dispose = () => {
139
+ abortController.abort();
140
+ removeNoticeFileSync(noticeFile);
141
+ noticeFile = null;
142
+ };
143
+ return {
144
+ kind: "launch",
145
+ session: {
146
+ tuiEnv: noticeFile ? { [UPDATE_NOTICE_FILE_ENV]: noticeFile } : {},
147
+ startBackground,
148
+ dispose
149
+ }
150
+ };
151
+ };
152
+
153
+ export {
154
+ NOOP_LAUNCH_UPDATE_SESSION,
155
+ prepareTuiLaunch
156
+ };