@my-life-buddies/cli-refinewidget 0.0.0-stage → 0.37.0-dev.2
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/README.md +19 -2
- package/dist/bin/buddy.js +821 -0
- package/dist/bin/buddy.js.map +7 -0
- package/dist/bin/command-runner.js +640 -0
- package/dist/bin/command-runner.js.map +7 -0
- package/dist/bin/core.js +12379 -0
- package/dist/bin/core.js.map +7 -0
- package/dist/bin/platform.js +597 -0
- package/dist/bin/platform.js.map +7 -0
- package/dist/bin/preview.js +6246 -0
- package/dist/bin/preview.js.map +7 -0
- package/dist/bin/resources/widget-document.js +176 -0
- package/dist/bin/runtime-preflight.js +212 -0
- package/dist/bin/runtime-preflight.js.map +7 -0
- package/install/windows-bin.cjs +108 -0
- package/package.json +66 -4
- package/resources/README.md +10 -0
- package/resources/agent-template/buddy-dev-control.mjs +51 -0
- package/resources/agent-template/buddy-dev-preload.mjs +47 -0
- package/resources/agent-template/package-lock.json +23 -0
- package/resources/agent-template/package.json +14 -0
- package/resources/widget-template/index.html +209 -0
- package/resources/widget-template/schema.json +15 -0
- package/resources/windows-job/win32-arm64/buddy_windows_job.node +0 -0
- package/resources/windows-job/win32-arm64/manifest.json +40 -0
- package/resources/windows-job/win32-x64/buddy_windows_job.node +0 -0
- package/resources/windows-job/win32-x64/manifest.json +41 -0
|
@@ -0,0 +1,821 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// apps/cli/src/bin/buddy.ts
|
|
4
|
+
import { CLI_COMMAND_NAME as CLI_COMMAND_NAME4, CLI_PACKAGE_NAME as CLI_PACKAGE_NAME4 } from "./platform.js";
|
|
5
|
+
import { stderr, stdout } from "node:process";
|
|
6
|
+
import { createRequire } from "node:module";
|
|
7
|
+
import { dirname as dirname4 } from "node:path";
|
|
8
|
+
|
|
9
|
+
// apps/cli/src/platform-support.ts
|
|
10
|
+
import { assertSupportedDeveloperPlatform, UnsupportedDeveloperPlatformError } from "./platform.js";
|
|
11
|
+
|
|
12
|
+
// apps/cli/src/self-update.ts
|
|
13
|
+
import { assertBuildManifest, CLI_AUTO_UPDATE_AVAILABLE, CLI_COMMAND_NAME as CLI_COMMAND_NAME2, CLI_PACKAGE_NAME, CLI_MAC_INSTALL_COMMAND, CLI_WINDOWS_INSTALL_COMMAND } from "./platform.js";
|
|
14
|
+
import { runNpm as executeNpm, LANE_BUILD, CLI_UPDATE_GUIDANCE } from "./platform.js";
|
|
15
|
+
import { readFile as readFile2 } from "node:fs/promises";
|
|
16
|
+
import { homedir as homedir2 } from "node:os";
|
|
17
|
+
import { join as join3 } from "node:path";
|
|
18
|
+
import { npmPackageManifest } from "./platform.js";
|
|
19
|
+
|
|
20
|
+
// apps/cli/src/update-lock.ts
|
|
21
|
+
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
22
|
+
import { dirname, join } from "node:path";
|
|
23
|
+
import { homedir } from "node:os";
|
|
24
|
+
import { createHash } from "node:crypto";
|
|
25
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
26
|
+
import { CLI_COMMAND_NAME } from "./platform.js";
|
|
27
|
+
var defaultUpdateCache = () => join(homedir(), ".cache", CLI_COMMAND_NAME, "updates", `${process.platform}-${process.arch}`);
|
|
28
|
+
var globalUpdateLock = (cache, prefix) => join(cache, "locks", createHash("sha256").update(prefix).digest("hex"));
|
|
29
|
+
async function withUpdateLock(path, signal, action) {
|
|
30
|
+
await mkdir(dirname(path), { recursive: true, mode: 448 });
|
|
31
|
+
const deadline = Date.now() + 2e5;
|
|
32
|
+
for (; ; ) {
|
|
33
|
+
signal?.throwIfAborted();
|
|
34
|
+
try {
|
|
35
|
+
await mkdir(path, { mode: 448 });
|
|
36
|
+
break;
|
|
37
|
+
} catch (cause) {
|
|
38
|
+
if (cause.code !== "EEXIST") throw cause;
|
|
39
|
+
if (Date.now() >= deadline) throw new Error(`\u7B49\u5F85\u5176\u4ED6 CLI \u66F4\u65B0\u8D85\u65F6\u3002\u8BF7\u786E\u8BA4\u66F4\u65B0\u8FDB\u7A0B\u5DF2\u9000\u51FA\uFF0C\u518D\u6E05\u7406\u9501\u76EE\u5F55\u5E76\u91CD\u8BD5\uFF1A${path}`);
|
|
40
|
+
await delay(100, void 0, { signal });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
await writeFile(join(path, "owner.json"), JSON.stringify({ pid: process.pid, startedAt: Date.now() }), { mode: 384 });
|
|
45
|
+
signal?.throwIfAborted();
|
|
46
|
+
return await action();
|
|
47
|
+
} finally {
|
|
48
|
+
await rm(path, { recursive: true, force: true });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// apps/cli/src/npm-update-install.ts
|
|
53
|
+
import { stripVTControlCharacters } from "node:util";
|
|
54
|
+
function redact(text) {
|
|
55
|
+
return text.replace(/https?:\/\/[^\s<>"']+/giu, (value) => {
|
|
56
|
+
try {
|
|
57
|
+
const url = new URL(value);
|
|
58
|
+
url.username = "";
|
|
59
|
+
url.password = "";
|
|
60
|
+
url.search = "";
|
|
61
|
+
url.hash = "";
|
|
62
|
+
return url.toString();
|
|
63
|
+
} catch {
|
|
64
|
+
return "[URL]";
|
|
65
|
+
}
|
|
66
|
+
}).replace(/\bBearer\s+\S+/giu, "Bearer [REDACTED]").replace(/\b(?:_authToken|_auth|token|password|authorization)\s*[:=]\s*\S+/giu, "[REDACTED]").replace(/\bnpm_[A-Za-z0-9]+/gu, "[REDACTED]");
|
|
67
|
+
}
|
|
68
|
+
function npmFailure(cause) {
|
|
69
|
+
const error = cause;
|
|
70
|
+
const stderr2 = stripVTControlCharacters(typeof error?.stderr === "string" ? error.stderr.slice(-32768) : "");
|
|
71
|
+
const lines = stderr2.split(/\r?\n/u).map((line) => line.match(/^npm (?:error|ERR!)\s+(.*)$/u)?.[1]).filter((line) => !!line);
|
|
72
|
+
const code = lines.map((line) => line.match(/^code\s+(E[A-Z0-9_]+)\b/u)?.[1]).find(Boolean) ?? (typeof error?.code === "string" && /^E[A-Z0-9_]+$/u.test(error.code) ? error.code : error?.killed ? "ETIMEDOUT" : "UNKNOWN");
|
|
73
|
+
const reason = lines.filter((line) => !/^(?:code |A complete log |A complete log can|Log files |In most cases|If you |Note:)/u.test(line) && !/debug[^\r\n]*\.log\s*$/u.test(line)).slice(0, 3).map(redact).join(" ").slice(0, 700);
|
|
74
|
+
return { code, reason };
|
|
75
|
+
}
|
|
76
|
+
async function installCliWithNpm(args, settings, runNpm2) {
|
|
77
|
+
const deadline = Date.now() + settings.timeout;
|
|
78
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
79
|
+
settings.signal?.throwIfAborted();
|
|
80
|
+
try {
|
|
81
|
+
const timeout = deadline - Date.now();
|
|
82
|
+
if (timeout <= 0) throw Object.assign(new Error("install timeout"), { code: "ETIMEDOUT" });
|
|
83
|
+
await runNpm2([...args, "--prefer-online"], { ...settings, timeout });
|
|
84
|
+
return;
|
|
85
|
+
} catch (cause) {
|
|
86
|
+
settings.signal?.throwIfAborted();
|
|
87
|
+
const { code, reason } = npmFailure(cause);
|
|
88
|
+
if (code === "ETARGET" && attempt === 0 && Date.now() < deadline) continue;
|
|
89
|
+
const hint = code === "ETARGET" ? `npm \u5C1A\u672A\u63D0\u4F9B\u6240\u9700\u7248\u672C\uFF1B${attempt > 0 ? "\u5DF2\u8054\u7F51\u5237\u65B0\u5E76\u91CD\u8BD5\u4E00\u6B21" : "\u8054\u7F51\u5237\u65B0\u540E\u4ECD\u627E\u4E0D\u5230\u8BE5\u7248\u672C"}\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5\u3002` : code === "EACCES" || code === "EPERM" ? "\u8BF7\u68C0\u67E5 npm \u5B89\u88C5\u76EE\u5F55\u548C\u7F13\u5B58\u76EE\u5F55\u7684\u5199\u5165\u6743\u9650\u3002" : code === "EBADENGINE" ? "\u8BF7\u68C0\u67E5 Node.js \u7248\u672C\u662F\u5426\u6EE1\u8DB3\u5305\u7684\u8981\u6C42\u3002" : ["ENOTFOUND", "ECONNRESET", "ECONNREFUSED", "ETIMEDOUT", "EAI_AGAIN"].includes(code) ? "\u8BF7\u68C0\u67E5\u5230 npm \u7684\u7F51\u7EDC\u8FDE\u63A5\u540E\u91CD\u8BD5\u3002" : "\u8BF7\u6839\u636E\u4EE5\u4E0A\u539F\u56E0\u5904\u7406\u540E\u91CD\u8BD5\u3002";
|
|
90
|
+
throw new Error(`npm \u5B89\u88C5\u672A\u5B8C\u6210\uFF08${code}\uFF09\u3002${reason ? `${reason}
|
|
91
|
+
` : ""}${hint}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// apps/cli/src/install-location.ts
|
|
97
|
+
import { lstat, readFile, realpath } from "node:fs/promises";
|
|
98
|
+
import { dirname as dirname2, join as join2, resolve } from "node:path";
|
|
99
|
+
async function globalInstallation(packageRoot, packageName, platform = process.platform) {
|
|
100
|
+
try {
|
|
101
|
+
if ((await lstat(packageRoot)).isSymbolicLink()) return void 0;
|
|
102
|
+
const root = await realpath(packageRoot);
|
|
103
|
+
let prefix = root;
|
|
104
|
+
const layout = platform === "win32" ? ["node_modules"] : ["lib", "node_modules"];
|
|
105
|
+
for (const _part of [...layout, ...packageName.split("/")]) prefix = dirname2(prefix);
|
|
106
|
+
if (root !== resolve(prefix, ...layout, packageName)) return void 0;
|
|
107
|
+
if ((await lstat(join2(prefix, ...layout, packageName))).isSymbolicLink()) return void 0;
|
|
108
|
+
if (platform === "win32") {
|
|
109
|
+
const manifest = JSON.parse(await readFile(join2(root, "package.json"), "utf8"));
|
|
110
|
+
const bins = Object.entries(manifest.bin ?? {});
|
|
111
|
+
if (!bins.length || manifest.name !== packageName) return void 0;
|
|
112
|
+
for (const [name, entry] of bins) {
|
|
113
|
+
if (!/^[a-zA-Z0-9_-]+$/u.test(name) || typeof entry !== "string" || entry.includes("..")) return void 0;
|
|
114
|
+
const shim = join2(prefix, `${name}.cmd`);
|
|
115
|
+
if (!(await lstat(shim)).isFile() || (await lstat(shim)).isSymbolicLink()) return void 0;
|
|
116
|
+
const source = (await readFile(shim, "utf8")).replaceAll("\\", "/").toLowerCase();
|
|
117
|
+
if (!source.includes(`node_modules/${packageName}/${entry}`.toLowerCase())) return void 0;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return { root, prefix };
|
|
121
|
+
} catch {
|
|
122
|
+
return void 0;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// apps/cli/src/self-update.ts
|
|
127
|
+
var PACKAGE = CLI_PACKAGE_NAME;
|
|
128
|
+
var REGISTRY = "https://registry.npmjs.org/";
|
|
129
|
+
var MANUAL_UPDATE = process.platform === "win32" ? CLI_WINDOWS_INSTALL_COMMAND : LANE_BUILD ? CLI_MAC_INSTALL_COMMAND : `npm install --global --prefix "$HOME/.local/share/buddy-cli/sit" ${PACKAGE}@dev --registry=${REGISTRY} --prefer-online`;
|
|
130
|
+
var HELP = `\u7528\u6CD5:
|
|
131
|
+
${CLI_COMMAND_NAME2} --version [--json]
|
|
132
|
+
${CLI_COMMAND_NAME2} update --check [--json]
|
|
133
|
+
${CLI_COMMAND_NAME2} update [--json]
|
|
134
|
+
|
|
135
|
+
--version \u67E5\u770B\u5F53\u524D\u8FD0\u884C\u7684 CLI \u7248\u672C\uFF08\u65E0\u9700\u8054\u7F51\uFF09
|
|
136
|
+
--check \u68C0\u67E5\u516C\u5171 npm \u7684 dev \u7248\u672C\uFF0C\u4E0D\u5B89\u88C5
|
|
137
|
+
--json \u8F93\u51FA JSON\uFF0C\u4F9B Coding Agent \u6216\u811A\u672C\u8BFB\u53D6
|
|
138
|
+
|
|
139
|
+
\u65E0\u9700\u767B\u5F55\u642D\u642D\u8D26\u53F7\uFF0C\u4E5F\u65E0\u9700\u8FDB\u5165\u642D\u5B50\u5DE5\u7A0B\u3002
|
|
140
|
+
\u6B63\u5E38\u547D\u4EE4\uFF08\u542B guide\uFF09\u4F1A\u5148\u8054\u7F51\u81EA\u68C0\uFF0C\u81EA\u52A8\u66F4\u65B0\u540E\u7531\u65B0\u7248\u7EE7\u7EED\u6267\u884C\uFF1B\u7B49\u5F85\u5B8C\u6210\u5373\u53EF\u3002
|
|
141
|
+
\u5B89\u88C5\u4E3B\u52A8\u5237\u65B0 npm \u7248\u672C\u7F13\u5B58\uFF0C\u627E\u4E0D\u5230\u7248\u672C\uFF08ETARGET\uFF09\u65F6\u6700\u591A\u91CD\u8BD5\u4E00\u6B21\uFF1B\u5931\u8D25\u8BF4\u660E\u5177\u4F53\u539F\u56E0\u3002
|
|
142
|
+
--help\u3001\u5404\u547D\u4EE4 --help \u4E0E --version \u79BB\u7EBF\u53EF\u7528\uFF1B\u67E5\u8BE2\u6216\u66F4\u65B0\u5931\u8D25\u65F6\u505C\u6B62\u4E1A\u52A1\u64CD\u4F5C\u3002
|
|
143
|
+
update \u66F4\u65B0\u5F53\u524D CLI \u6240\u5728\u7684 npm \u5B89\u88C5\u76EE\u5F55\uFF08\u542B --global --prefix \u81EA\u5B9A\u4E49\u76EE\u5F55\uFF09\uFF0C\u4E0D\u4FEE\u6539\u5176\u4ED6\u5B89\u88C5\u6216\u5DE5\u7A0B Runtime \u4F9D\u8D56\u3002
|
|
144
|
+
${LANE_BUILD ? `\u4EC5\u66F4\u65B0 ${PACKAGE} \u7684 dev \u7248\u672C\uFF0C\u4FDD\u7559\u5F53\u524D\u6CF3\u9053\u5730\u5740\uFF0C\u4E0D\u4FEE\u6539\u539F CLI \u5305\u3002` : `\u540C\u4E00\u5305 latest/dev \u9700\u5206\u76EE\u5F55\u5B89\u88C5\u624D\u80FD\u5171\u5B58\uFF1BSIT \u547D\u4EE4\u4E3A ${CLI_COMMAND_NAME2}\uFF0C\u6B63\u5F0F\u547D\u4EE4\u4E3A buddy-cli\u3002`}
|
|
145
|
+
\u5DF2\u7ECF\u662F\u6700\u65B0\u7248\u6216\u5F53\u524D\u7248\u672C\u66F4\u9AD8\u65F6\u4E0D\u5B89\u88C5\u3001\u4E0D\u964D\u7EA7\u3002
|
|
146
|
+
\u6E90\u7801\u3001npm link\u3001\u5C40\u90E8\u5B89\u88C5\u6216 npx \u7684\u6B63\u5E38\u547D\u4EE4\u4F7F\u7528\u7F13\u5B58\u7684\u65B0\u7248\u6267\u884C\uFF0C\u4E0D\u8986\u76D6\u6E90\u7801\u6216\u5DE5\u7A0B\u4F9D\u8D56\u3002
|
|
147
|
+
\u4E0A\u8FF0\u5B89\u88C5\u65B9\u5F0F\u4E0D\u80FD\u7528 update \u5355\u72EC\u4FEE\u6539\u539F\u5B89\u88C5\uFF0C\u8BF7\u6CBF\u7528\u539F\u6765\u7684\u5B89\u88C5\u65B9\u5F0F\u66F4\u65B0\u3002
|
|
148
|
+
\u65E7\u7248 CLI \u5C1A\u65E0 update \u547D\u4EE4\u65F6\uFF0C${process.platform === "win32" ? "\u5728 PowerShell \u6216 CMD \u4E2D\u8FD0\u884C" : "\u8FD0\u884C"}\uFF1A
|
|
149
|
+
${MANUAL_UPDATE}`;
|
|
150
|
+
function assertUpdateManifest(manifest, version) {
|
|
151
|
+
if (manifest.name !== PACKAGE || manifest.version !== version) throw new Error("CLI \u66F4\u65B0\u5305\u540D\u6216\u7248\u672C\u4E0D\u5339\u914D\u3002");
|
|
152
|
+
assertBuildManifest(manifest, process.platform, version);
|
|
153
|
+
}
|
|
154
|
+
function parseVersion(version) {
|
|
155
|
+
const numeric = "(?:0|[1-9][0-9]*)";
|
|
156
|
+
const identifier = `(?:${numeric}|[0-9]*[A-Za-z-][0-9A-Za-z-]*)`;
|
|
157
|
+
const pattern = new RegExp(`^(${numeric})\\.(${numeric})\\.(${numeric})(?:-(${identifier}(?:\\.${identifier})*))?(?:\\+[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?$`, "u");
|
|
158
|
+
const match = typeof version === "string" && version.length <= 256 ? pattern.exec(version) : null;
|
|
159
|
+
if (!match || match[0] !== version) throw new Error("\u7248\u672C\u4FE1\u606F\u4E0D\u662F\u6709\u6548\u7684 SemVer \u7248\u672C\u53F7\u3002");
|
|
160
|
+
return [match[1], match[2], match[3], ...match[4]?.split(".") ?? []];
|
|
161
|
+
}
|
|
162
|
+
function compareVersions(left, right) {
|
|
163
|
+
const a = parseVersion(left);
|
|
164
|
+
const b = parseVersion(right);
|
|
165
|
+
for (let i = 0; i < 3; i++) {
|
|
166
|
+
if (BigInt(a[i]) !== BigInt(b[i])) return BigInt(a[i]) > BigInt(b[i]) ? 1 : -1;
|
|
167
|
+
}
|
|
168
|
+
if (a.length === 3 || b.length === 3) return a.length === b.length ? 0 : a.length === 3 ? 1 : -1;
|
|
169
|
+
for (let i = 3; i < Math.max(a.length, b.length); i++) {
|
|
170
|
+
const x = a[i];
|
|
171
|
+
const y = b[i];
|
|
172
|
+
if (x === y) continue;
|
|
173
|
+
if (x === void 0 || y === void 0) return x === void 0 ? -1 : 1;
|
|
174
|
+
const xn = /^[0-9]+$/u.test(x);
|
|
175
|
+
const yn = /^[0-9]+$/u.test(y);
|
|
176
|
+
if (xn && yn) return BigInt(x) > BigInt(y) ? 1 : -1;
|
|
177
|
+
if (xn !== yn) return xn ? -1 : 1;
|
|
178
|
+
return x > y ? 1 : -1;
|
|
179
|
+
}
|
|
180
|
+
return 0;
|
|
181
|
+
}
|
|
182
|
+
async function latestVersion(options) {
|
|
183
|
+
const timeout = AbortSignal.timeout(1e4);
|
|
184
|
+
const signal = options.signal ? AbortSignal.any([options.signal, timeout]) : timeout;
|
|
185
|
+
try {
|
|
186
|
+
const metadata = await npmPackageManifest(PACKAGE, "dev", { fetch: options.fetch, signal });
|
|
187
|
+
if (metadata?.name !== PACKAGE) throw new Error("npm \u8FD4\u56DE\u7684\u5305\u540D\u4E0D\u5339\u914D");
|
|
188
|
+
parseVersion(metadata.version);
|
|
189
|
+
if (typeof metadata.version !== "string" || !/^\d+\.\d+\.\d+-dev\.\d+$/.test(metadata.version)) throw new Error("npm dev \u672A\u6307\u5411 x.y.z-dev.N \u7248\u672C\uFF1BSIT CLI \u4E0D\u4F1A\u8DE8\u6E20\u9053\u66F4\u65B0\uFF0C\u8BF7\u8054\u7CFB\u53D1\u5E03\u8005\u4FEE\u6B63 dev \u6807\u7B7E");
|
|
190
|
+
assertUpdateManifest(metadata, metadata.version);
|
|
191
|
+
return metadata.version;
|
|
192
|
+
} catch (cause) {
|
|
193
|
+
if (options.signal?.aborted) throw new Error("\u64CD\u4F5C\u5DF2\u53D6\u6D88\u3002");
|
|
194
|
+
if (timeout.aborted) throw Object.assign(new Error("\u68C0\u67E5\u66F4\u65B0\u8D85\u65F6\uFF0C\u8BF7\u68C0\u67E5\u7F51\u7EDC\u540E\u91CD\u8BD5\u3002", { cause }), { code: "CLI_UPDATE_CHECK_TIMEOUT" });
|
|
195
|
+
throw new Error(`\u65E0\u6CD5\u68C0\u67E5 npm \u66F4\u65B0\uFF1A${cause instanceof Error ? cause.message : String(cause)}\u3002\u8BF7\u7A0D\u540E\u91CD\u8BD5\u3002`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
async function installUpdate(version, options) {
|
|
199
|
+
if (!/^\d+\.\d+\.\d+-dev\.\d+$/.test(version)) throw new Error("SIT CLI \u53EA\u80FD\u66F4\u65B0 x.y.z-dev.N \u7248\u672C\uFF0C\u4E0D\u4F1A\u8DE8\u6E20\u9053\u5B89\u88C5\u3002");
|
|
200
|
+
const runNpm2 = options.runNpm ?? ((args, settings2) => executeNpm(args, { ...settings2, maxBuffer: 4 * 1024 * 1024 }));
|
|
201
|
+
const settings = { cwd: homedir2(), signal: options.signal, timeout: 1e4 };
|
|
202
|
+
const installation = await globalInstallation(options.packageRoot, PACKAGE);
|
|
203
|
+
if (!installation) {
|
|
204
|
+
throw new Error("\u5F53\u524D CLI \u662F\u6E90\u7801\u3001\u94FE\u63A5\u6216\u9879\u76EE\u5185\u5B89\u88C5\uFF0C\u4E0D\u80FD\u76F4\u63A5\u6539\u5199\u539F\u5B89\u88C5\u3002\u6B63\u5E38\u547D\u4EE4\u4F1A\u4ECE SIT \u72EC\u7ACB\u7F13\u5B58\u8FD0\u884C\u65B0\u7248\uFF1B\u5355\u72EC\u66F4\u65B0\u8BF7\u6CBF\u7528\u539F\u6765\u7684\u5B89\u88C5\u65B9\u5F0F\u3002");
|
|
205
|
+
}
|
|
206
|
+
const { root: installedRoot, prefix } = installation;
|
|
207
|
+
const manualUpdate = process.platform === "win32" ? MANUAL_UPDATE : `npm install --global --prefix '${prefix.replace(/'/gu, "'\\''")}' ${PACKAGE}@dev --registry=${REGISTRY} --prefer-online`;
|
|
208
|
+
await withUpdateLock(globalUpdateLock(options.cacheDirectory ?? defaultUpdateCache(), prefix), options.signal, async () => {
|
|
209
|
+
const current = JSON.parse(await readFile2(join3(installedRoot, "package.json"), "utf8"));
|
|
210
|
+
if (current.name !== PACKAGE || !/^\d+\.\d+\.\d+-dev\.\d+$/.test(current.version)) {
|
|
211
|
+
throw new Error("\u5F53\u524D\u5B89\u88C5\u76EE\u5F55\u5DF2\u88AB\u5176\u4ED6\u6E20\u9053\u66FF\u6362\uFF0C\u5DF2\u505C\u6B62\u66F4\u65B0\u3002\u8BF7\u5C06 latest \u548C dev \u5B89\u88C5\u5230\u4E0D\u540C\u76EE\u5F55\uFF0C\u518D\u91CD\u65B0\u8FD0\u884C " + CLI_COMMAND_NAME2 + "\u3002");
|
|
212
|
+
}
|
|
213
|
+
assertUpdateManifest(current, current.version);
|
|
214
|
+
if (current.name === PACKAGE && current.version === version) return;
|
|
215
|
+
if (current.name === PACKAGE && compareVersions(current.version, version) > 0) throw new Error("\u53E6\u4E00\u4E2A\u8FDB\u7A0B\u5DF2\u5B89\u88C5\u66F4\u65B0\u7684 CLI\uFF0C\u8BF7\u91CD\u65B0\u6267\u884C\u547D\u4EE4\u3002");
|
|
216
|
+
try {
|
|
217
|
+
await installCliWithNpm([
|
|
218
|
+
"install",
|
|
219
|
+
"--global",
|
|
220
|
+
`${PACKAGE}@${version}`,
|
|
221
|
+
"--prefix",
|
|
222
|
+
prefix,
|
|
223
|
+
`--registry=${REGISTRY}`,
|
|
224
|
+
`--@my-life-buddies:registry=${REGISTRY}`,
|
|
225
|
+
"--engine-strict",
|
|
226
|
+
"--no-audit",
|
|
227
|
+
"--no-fund"
|
|
228
|
+
], { ...settings, timeout: 18e4 }, runNpm2);
|
|
229
|
+
} catch (cause) {
|
|
230
|
+
if (options.signal?.aborted) throw new Error("\u66F4\u65B0\u5DF2\u53D6\u6D88\uFF0C\u8BF7\u8FD0\u884C " + CLI_COMMAND_NAME2 + " --version \u68C0\u67E5\u5B89\u88C5\u72B6\u6001\u3002");
|
|
231
|
+
throw new Error(`${cause instanceof Error ? cause.message : "npm \u5B89\u88C5\u672A\u5B8C\u6210\u3002"}
|
|
232
|
+
\u4E5F\u53EF\u624B\u52A8\u5B89\u88C5${process.platform === "win32" ? "\uFF08\u5728 PowerShell \u6216 CMD \u4E2D\u8FD0\u884C\uFF09" : ""}\uFF1A${manualUpdate}`);
|
|
233
|
+
}
|
|
234
|
+
const installed = JSON.parse(await readFile2(join3(installedRoot, "package.json"), "utf8"));
|
|
235
|
+
if (installed.name !== PACKAGE || installed.version !== version) {
|
|
236
|
+
throw new Error(`npm \u5DF2\u7ED3\u675F\uFF0C\u4F46\u672A\u786E\u8BA4\u5B89\u88C5\u5230 ${version}\u3002\u8BF7\u8FD0\u884C ${CLI_COMMAND_NAME2} --version \u68C0\u67E5\uFF0C\u5FC5\u8981\u65F6${process.platform === "win32" ? "\u5728 PowerShell \u6216 CMD \u4E2D" : ""}\u91CD\u8BD5\uFF1A${manualUpdate}`);
|
|
237
|
+
}
|
|
238
|
+
assertUpdateManifest(installed, version);
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
async function runSelfUpdateCommand(args, io, options) {
|
|
242
|
+
const versionOnly = args[0] === "--version" || args[0] === "-v";
|
|
243
|
+
if (!versionOnly && args[0] !== "update") return void 0;
|
|
244
|
+
const json = args.includes("--json");
|
|
245
|
+
const fail = (message, exitCode) => {
|
|
246
|
+
if (json) io.write(JSON.stringify({ package: PACKAGE, currentVersion: options.version, error: message }));
|
|
247
|
+
else io.writeError(`${CLI_COMMAND_NAME2}: ${message}`);
|
|
248
|
+
return { exitCode };
|
|
249
|
+
};
|
|
250
|
+
const flags = args.slice(1);
|
|
251
|
+
const allowed = versionOnly ? ["--json", "--help", "-h"] : ["--check", "--json", "--help", "-h"];
|
|
252
|
+
if (flags.some((flag) => !allowed.includes(flag)) || new Set(flags).size !== flags.length) {
|
|
253
|
+
return fail("\u53C2\u6570\u65E0\u6548\uFF0C\u8BF7\u8FD0\u884C " + CLI_COMMAND_NAME2 + " update --help \u67E5\u770B\u7528\u6CD5\u3002", 2);
|
|
254
|
+
}
|
|
255
|
+
if (flags.includes("--help") || flags.includes("-h")) {
|
|
256
|
+
const help = !CLI_AUTO_UPDATE_AVAILABLE ? `${CLI_COMMAND_NAME2} update\uFF1A${CLI_UPDATE_GUIDANCE}` : HELP;
|
|
257
|
+
if (json) io.write(JSON.stringify({ help }));
|
|
258
|
+
else io.write(help);
|
|
259
|
+
return { exitCode: 0 };
|
|
260
|
+
}
|
|
261
|
+
try {
|
|
262
|
+
parseVersion(options.version);
|
|
263
|
+
if (versionOnly) {
|
|
264
|
+
io.write(json ? JSON.stringify({ package: PACKAGE, version: options.version, ...LANE_BUILD ? { lane: LANE_BUILD } : {} }) : options.version);
|
|
265
|
+
return { exitCode: 0 };
|
|
266
|
+
}
|
|
267
|
+
if (!CLI_AUTO_UPDATE_AVAILABLE) throw new Error(CLI_UPDATE_GUIDANCE);
|
|
268
|
+
const pin = process.env.BUDDY_CLI_PIN_VERSION;
|
|
269
|
+
if (pin !== void 0) throw new Error("\u5F53\u524D\u5DF2\u7528 BUDDY_CLI_PIN_VERSION \u9501\u5B9A CLI\uFF1B\u5982\u9700\u66F4\u65B0\uFF0C\u8BF7\u5148\u79FB\u9664 BUDDY_CLI_PIN_VERSION \u73AF\u5883\u53D8\u91CF\u3002");
|
|
270
|
+
const latest = await latestVersion(options);
|
|
271
|
+
const comparison = compareVersions(latest, options.version);
|
|
272
|
+
const check = flags.includes("--check");
|
|
273
|
+
const status = comparison > 0 ? "update_available" : comparison < 0 ? "ahead" : "up_to_date";
|
|
274
|
+
if (!check && comparison > 0) {
|
|
275
|
+
if (!json) io.write(`\u6B63\u5728\u66F4\u65B0 CLI\uFF1A${options.version} \u2192 ${latest}\u2026`);
|
|
276
|
+
await installUpdate(latest, options);
|
|
277
|
+
}
|
|
278
|
+
const updated = !check && comparison > 0;
|
|
279
|
+
if (json) {
|
|
280
|
+
io.write(JSON.stringify({
|
|
281
|
+
package: PACKAGE,
|
|
282
|
+
currentVersion: options.version,
|
|
283
|
+
latestVersion: latest,
|
|
284
|
+
updateAvailable: comparison > 0 && !updated,
|
|
285
|
+
status: updated ? "updated" : status,
|
|
286
|
+
...updated ? { installedVersion: latest } : {}
|
|
287
|
+
}));
|
|
288
|
+
} else if (updated) io.write(`CLI \u5DF2\u66F4\u65B0\uFF1A${options.version} \u2192 ${latest}\u3002`);
|
|
289
|
+
else if (comparison > 0) io.write(`\u53D1\u73B0\u65B0\u7248\u672C\uFF1A${options.version} \u2192 ${latest}\u3002\u8FD0\u884C ${CLI_COMMAND_NAME2} update \u66F4\u65B0\u3002`);
|
|
290
|
+
else if (comparison < 0) io.write(`\u5F53\u524D\u7248\u672C ${options.version} \u9AD8\u4E8E npm dev ${latest}\uFF0C\u65E0\u9700\u66F4\u65B0\u3002`);
|
|
291
|
+
else io.write(`\u5F53\u524D\u5DF2\u662F\u6700\u65B0\u7248\uFF1A${options.version}\u3002`);
|
|
292
|
+
return { exitCode: 0 };
|
|
293
|
+
} catch (cause) {
|
|
294
|
+
return fail(cause instanceof Error ? cause.message : String(cause), 1);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// apps/cli/src/auto-update.ts
|
|
299
|
+
import { runNpm as executeNpm2, LANE_BUILD as LANE_BUILD2, CLI_AUTO_UPDATE_AVAILABLE as CLI_AUTO_UPDATE_AVAILABLE2, CLI_PACKAGE_NAME as CLI_PACKAGE_NAME2 } from "./platform.js";
|
|
300
|
+
import { spawn } from "node:child_process";
|
|
301
|
+
import { lstat as lstat2, mkdir as mkdir2, mkdtemp, readFile as readFile3, realpath as realpath2, rename, rm as rm2, writeFile as writeFile2 } from "node:fs/promises";
|
|
302
|
+
import { constants } from "node:os";
|
|
303
|
+
import { join as join4 } from "node:path";
|
|
304
|
+
var PACKAGE2 = CLI_PACKAGE_NAME2;
|
|
305
|
+
var REGISTRY2 = "https://registry.npmjs.org/";
|
|
306
|
+
var HANDOFF_ENV = "BUDDY_CLI_SIT_UPDATE_HANDOFF";
|
|
307
|
+
async function installedVersion(root) {
|
|
308
|
+
try {
|
|
309
|
+
const pkg = JSON.parse(await readFile3(join4(root, "package.json"), "utf8"));
|
|
310
|
+
assertUpdateManifest(pkg, pkg.version);
|
|
311
|
+
return typeof pkg.version === "string" ? pkg.version : void 0;
|
|
312
|
+
} catch {
|
|
313
|
+
return void 0;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
async function checkedEntry(root, version) {
|
|
317
|
+
if (await installedVersion(root) !== version) throw new Error(`CLI \u5B89\u88C5\u7ED3\u679C\u4E0E\u672C\u6B21\u8981\u6C42\u7684 ${version} \u4E0D\u4E00\u81F4\uFF0C\u8BF7\u91CD\u8BD5\u3002`);
|
|
318
|
+
const entry = join4(root, "dist", "bin", "buddy.js");
|
|
319
|
+
if (!(await lstat2(entry)).isFile()) throw new Error("\u65B0\u7248 CLI \u7F3A\u5C11\u6709\u6548\u7684\u6267\u884C\u5165\u53E3\u3002");
|
|
320
|
+
return entry;
|
|
321
|
+
}
|
|
322
|
+
async function prepareLatest(version, options) {
|
|
323
|
+
const cache = options.cacheDirectory ?? defaultUpdateCache();
|
|
324
|
+
const runNpm2 = options.runNpm ?? ((args, settings) => executeNpm2(args, { ...settings, maxBuffer: 4 * 1024 * 1024 }));
|
|
325
|
+
const installation = await globalInstallation(options.packageRoot, PACKAGE2);
|
|
326
|
+
if (installation) {
|
|
327
|
+
await installUpdate(version, options);
|
|
328
|
+
return checkedEntry(installation.root, version);
|
|
329
|
+
}
|
|
330
|
+
const destination = join4(cache, "releases", version);
|
|
331
|
+
return withUpdateLock(join4(cache, "locks", `release-${version}`), options.signal, async () => {
|
|
332
|
+
const root = join4(destination, "node_modules", PACKAGE2);
|
|
333
|
+
try {
|
|
334
|
+
return await checkedEntry(root, version);
|
|
335
|
+
} catch {
|
|
336
|
+
}
|
|
337
|
+
await mkdir2(join4(cache, "releases"), { recursive: true, mode: 448 });
|
|
338
|
+
const temporary = await mkdtemp(join4(cache, "releases", ".install-"));
|
|
339
|
+
try {
|
|
340
|
+
await writeFile2(join4(temporary, "package.json"), JSON.stringify({ private: true }));
|
|
341
|
+
await installCliWithNpm(
|
|
342
|
+
[
|
|
343
|
+
"install",
|
|
344
|
+
"--prefix",
|
|
345
|
+
temporary,
|
|
346
|
+
"--save-exact",
|
|
347
|
+
`${PACKAGE2}@${version}`,
|
|
348
|
+
`--registry=${REGISTRY2}`,
|
|
349
|
+
`--@my-life-buddies:registry=${REGISTRY2}`,
|
|
350
|
+
"--engine-strict",
|
|
351
|
+
"--no-audit",
|
|
352
|
+
"--no-fund"
|
|
353
|
+
],
|
|
354
|
+
{ cwd: temporary, signal: options.signal, timeout: 18e4 },
|
|
355
|
+
runNpm2
|
|
356
|
+
);
|
|
357
|
+
await checkedEntry(join4(temporary, "node_modules", PACKAGE2), version);
|
|
358
|
+
options.signal?.throwIfAborted();
|
|
359
|
+
await rm2(destination, { recursive: true, force: true });
|
|
360
|
+
await rename(temporary, destination);
|
|
361
|
+
return checkedEntry(root, version);
|
|
362
|
+
} finally {
|
|
363
|
+
await rm2(temporary, { recursive: true, force: true });
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
async function runUpdatedCli(entry, args, options) {
|
|
368
|
+
options.signal?.throwIfAborted();
|
|
369
|
+
return new Promise((resolve3, reject) => {
|
|
370
|
+
const child = spawn(process.execPath, [entry, ...args], { cwd: options.cwd, env: options.env, stdio: "inherit" });
|
|
371
|
+
let cancellationExitCode;
|
|
372
|
+
let killTimer;
|
|
373
|
+
const abort = () => {
|
|
374
|
+
const reason = options.signal?.reason?.message;
|
|
375
|
+
const signal = reason === "SIGINT" ? "SIGINT" : reason === "SIGHUP" ? "SIGHUP" : "SIGTERM";
|
|
376
|
+
cancellationExitCode ??= 128 + constants.signals[signal];
|
|
377
|
+
child.kill(signal);
|
|
378
|
+
killTimer ??= setTimeout(() => child.kill("SIGKILL"), 15e3);
|
|
379
|
+
killTimer.unref();
|
|
380
|
+
};
|
|
381
|
+
const cleanup = () => {
|
|
382
|
+
options.signal?.removeEventListener("abort", abort);
|
|
383
|
+
clearTimeout(killTimer);
|
|
384
|
+
};
|
|
385
|
+
child.once("error", (error) => {
|
|
386
|
+
cleanup();
|
|
387
|
+
reject(error);
|
|
388
|
+
});
|
|
389
|
+
child.once("exit", (code, signal) => {
|
|
390
|
+
cleanup();
|
|
391
|
+
resolve3({ exitCode: cancellationExitCode ?? code ?? 128 + (signal ? constants.signals[signal] : 1) });
|
|
392
|
+
});
|
|
393
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
394
|
+
if (options.signal?.aborted) abort();
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
async function runAutoUpdate(args, io, options) {
|
|
398
|
+
if (!CLI_AUTO_UPDATE_AVAILABLE2) return void 0;
|
|
399
|
+
if (args[0] === "dev" && args[1] === "stop") return void 0;
|
|
400
|
+
if (args[0] === "help" || args[0] === "update" || args[0] === "--version" || args[0] === "-v" || args.includes("--help") || args.includes("-h")) return void 0;
|
|
401
|
+
let installingUpdate = false;
|
|
402
|
+
try {
|
|
403
|
+
const env = { ...options.env ?? process.env };
|
|
404
|
+
const pin = env.BUDDY_CLI_PIN_VERSION;
|
|
405
|
+
if (pin !== void 0) {
|
|
406
|
+
if (!/^\d+\.\d+\.\d+-dev\.\d+$/.test(pin) || pin !== options.version) {
|
|
407
|
+
throw new Error(`CLI \u7248\u672C\u9501\u5B9A\u4E0D\u5339\u914D\uFF1A\u8981\u6C42 ${pin}\uFF0C\u5F53\u524D ${options.version}\u3002\u8BF7\u5B89\u88C5 ${PACKAGE2}@${pin} \u6216\u79FB\u9664 BUDDY_CLI_PIN_VERSION \u73AF\u5883\u53D8\u91CF\u3002`);
|
|
408
|
+
}
|
|
409
|
+
return void 0;
|
|
410
|
+
}
|
|
411
|
+
const handoff = env[HANDOFF_ENV];
|
|
412
|
+
if (handoff !== void 0) {
|
|
413
|
+
delete env[HANDOFF_ENV];
|
|
414
|
+
if (!options.env) delete process.env[HANDOFF_ENV];
|
|
415
|
+
const expected = JSON.parse(handoff);
|
|
416
|
+
const entry2 = await realpath2(join4(options.packageRoot, "dist", "bin", "buddy.js"));
|
|
417
|
+
if ((expected.package !== void 0 || LANE_BUILD2) && expected.package !== PACKAGE2 || expected.parent !== process.ppid || expected.version !== options.version || expected.entry !== entry2 || JSON.stringify(expected.args) !== JSON.stringify(args)) throw new Error("CLI \u81EA\u52A8\u66F4\u65B0\u63A5\u7EED\u6821\u9A8C\u5931\u8D25\uFF0C\u8BF7\u91CD\u65B0\u8FD0\u884C\u547D\u4EE4\u3002");
|
|
418
|
+
return void 0;
|
|
419
|
+
}
|
|
420
|
+
const latest = await latestVersion(options);
|
|
421
|
+
if (compareVersions(latest, options.version) <= 0) return void 0;
|
|
422
|
+
io.writeError(`CLI \u81EA\u52A8\u66F4\u65B0\uFF1A${options.version} \u2192 ${latest}\u3002\u5B8C\u6210\u540E\u7EE7\u7EED\u5F53\u524D\u547D\u4EE4\uFF0C\u8BF7\u52FF\u91CD\u590D\u6267\u884C\u3002`);
|
|
423
|
+
installingUpdate = true;
|
|
424
|
+
const entry = await realpath2(await prepareLatest(latest, options));
|
|
425
|
+
installingUpdate = false;
|
|
426
|
+
options.signal?.throwIfAborted();
|
|
427
|
+
env[HANDOFF_ENV] = JSON.stringify({ package: PACKAGE2, parent: process.pid, version: latest, entry, args });
|
|
428
|
+
return await (options.runCli ?? runUpdatedCli)(entry, args, { cwd: options.cwd ?? process.cwd(), env, signal: options.signal });
|
|
429
|
+
} catch (cause) {
|
|
430
|
+
const detail = cause instanceof Error ? cause.message : String(cause);
|
|
431
|
+
let error = `CLI \u81EA\u68C0\u6216\u66F4\u65B0\u5931\u8D25\uFF0C\u539F\u547D\u4EE4\u672A\u6267\u884C\uFF1A${detail}`;
|
|
432
|
+
if (options.signal?.aborted) error = "CLI \u81EA\u52A8\u66F4\u65B0\u5DF2\u53D6\u6D88\uFF0C\u539F\u547D\u4EE4\u672A\u6267\u884C\u3002";
|
|
433
|
+
else if (args[0] === "preview") {
|
|
434
|
+
error = cause?.code === "CLI_UPDATE_CHECK_TIMEOUT" ? "CLI \u66F4\u65B0\u68C0\u67E5\u8D85\u65F6\uFF0C\u672C\u6B21\u8FDE\u63A5\u5C1A\u672A\u5F00\u59CB\u3002\u8BF7\u628A\u7EC8\u7AEF\u62A5\u9519\u4EA4\u7ED9 Coding Agent\uFF0C\u68C0\u67E5 npm \u7F51\u7EDC\u6216\u4EE3\u7406\u914D\u7F6E\u3002" : `CLI \u66F4\u65B0\u672A\u5B8C\u6210\u3002\u8BF7\u8BA9 Coding Agent \u68C0\u67E5${installingUpdate ? "\u5B89\u88C5\u9519\u8BEF" : "\u4EE5\u4E0B\u9519\u8BEF"}\uFF0C\u5B8C\u6210\u540E\u91CD\u65B0\u8FD0\u884C\u8FDE\u63A5\u547D\u4EE4\u3002
|
|
435
|
+
${detail}`;
|
|
436
|
+
}
|
|
437
|
+
if (args.includes("--json")) io.write(JSON.stringify(args[0] === "diagnose" ? { ok: false, command: `diagnose.${args[1] ?? "help"}`, error: { code: "CLI_UPDATE_FAILED", message: error, retryable: true } } : { code: "CLI_UPDATE_FAILED", error }));
|
|
438
|
+
else io.writeError(error);
|
|
439
|
+
return { exitCode: 1 };
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// apps/cli/src/setup.ts
|
|
444
|
+
import { CLI_COMMAND_NAME as CLI_COMMAND_NAME3, CLI_PACKAGE_NAME as CLI_PACKAGE_NAME3, CLI_MAC_SETUP_AVAILABLE, CLI_WINDOWS_SETUP_AVAILABLE, assertBuildManifest as assertBuildManifest2 } from "./platform.js";
|
|
445
|
+
import { lstat as lstat4, readFile as readFile5, realpath as realpath5 } from "node:fs/promises";
|
|
446
|
+
import { join as join7 } from "node:path";
|
|
447
|
+
import { ensurePrivateDirectory, runNpm, windowsOperation, LANE_BUILD as LANE_BUILD3, CLI_UPDATE_GUIDANCE as CLI_UPDATE_GUIDANCE2 } from "./platform.js";
|
|
448
|
+
|
|
449
|
+
// apps/cli/src/setup-location.ts
|
|
450
|
+
import { randomUUID } from "node:crypto";
|
|
451
|
+
import { realpath as realpath3 } from "node:fs";
|
|
452
|
+
import { unlink, writeFile as writeFile3 } from "node:fs/promises";
|
|
453
|
+
import { join as join5, win32 } from "node:path";
|
|
454
|
+
import { promisify } from "node:util";
|
|
455
|
+
var nativeRealpath = promisify(realpath3.native);
|
|
456
|
+
var canonicalPath = (path) => win32.normalize(path.replace(/^\\\\\?\\/u, "")).toLowerCase();
|
|
457
|
+
async function assertSetupFileLocation(path, resolvePath = nativeRealpath) {
|
|
458
|
+
if (canonicalPath(await resolvePath(path)) !== canonicalPath(path)) {
|
|
459
|
+
throw new Error("Windows \u5C06\u5B89\u88C5\u6587\u4EF6\u91CD\u5B9A\u5411\u5230\u4E86\u5E94\u7528\u5BB9\u5668\u6216\u5176\u4ED6\u76EE\u5F55\uFF0C\u666E\u901A\u7EC8\u7AEF\u53EF\u80FD\u65E0\u6CD5\u4F7F\u7528\uFF1B\u5C1A\u672A\u4FEE\u6539 PATH\u3002\u8BF7\u4ECE Windows \u5F00\u59CB\u83DC\u5355\u6253\u5F00\u72EC\u7ACB\u7684 PowerShell \u6216 CMD\uFF0C\u518D\u8FD0\u884C\u5B98\u7F51\u5B89\u88C5\u547D\u4EE4\uFF0C\u4E0D\u8981\u5728\u5F53\u524D Coding Agent \u7684\u5185\u7F6E\u7EC8\u7AEF\u4E2D\u91CD\u8BD5\u3002");
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
async function assertSetupDirectoryLocation(prefix, resolvePath) {
|
|
463
|
+
const probe = join5(prefix, `.buddy-setup-path-${randomUUID()}.tmp`);
|
|
464
|
+
await writeFile3(probe, "", { flag: "wx", mode: 384 });
|
|
465
|
+
try {
|
|
466
|
+
await assertSetupFileLocation(probe, resolvePath);
|
|
467
|
+
} finally {
|
|
468
|
+
await unlink(probe);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// apps/cli/src/mac-user-path.ts
|
|
473
|
+
import { execFile } from "node:child_process";
|
|
474
|
+
import { randomUUID as randomUUID2 } from "node:crypto";
|
|
475
|
+
import { access, chmod, lstat as lstat3, readFile as readFile4, realpath as realpath4, rename as rename2, stat, unlink as unlink2, writeFile as writeFile4 } from "node:fs/promises";
|
|
476
|
+
import { constants as constants2 } from "node:fs";
|
|
477
|
+
import { userInfo } from "node:os";
|
|
478
|
+
import { basename, dirname as dirname3, isAbsolute, join as join6, normalize, resolve as resolve2 } from "node:path";
|
|
479
|
+
import { promisify as promisify2 } from "node:util";
|
|
480
|
+
var execute = promisify2(execFile);
|
|
481
|
+
var quoteShell = (value) => `'${value.replaceAll("'", "'\\''")}'`;
|
|
482
|
+
var safePath = (value) => isAbsolute(value) && !/[\u0000-\u001f\u007f]/u.test(value);
|
|
483
|
+
function macSetupEnvironment(env, shell = env.SHELL ?? userInfo().shell ?? "") {
|
|
484
|
+
const home = env.HOME;
|
|
485
|
+
if (!home || !safePath(home) || home.includes(":")) throw new Error("\u65E0\u6CD5\u786E\u8BA4 macOS \u7528\u6237\u76EE\u5F55 HOME\uFF0C\u5C1A\u672A\u4FEE\u6539\u7EC8\u7AEF\u914D\u7F6E\u3002");
|
|
486
|
+
const kind = basename(shell);
|
|
487
|
+
if (!safePath(shell) || kind !== "zsh" && kind !== "bash") throw new Error("setup \u7684 macOS PATH \u914D\u7F6E\u4EC5\u652F\u6301 zsh \u548C bash\uFF1B\u5F53\u524D shell \u672A\u83B7\u652F\u6301\uFF0C\u5C1A\u672A\u4FEE\u6539\u7EC8\u7AEF\u914D\u7F6E\u3002\u8BF7\u5728\u4F7F\u7528\u7684 zsh/bash \u7EC8\u7AEF\u91CD\u8BD5\u3002");
|
|
488
|
+
if (env.ZDOTDIR && !safePath(env.ZDOTDIR)) throw new Error("ZDOTDIR \u5FC5\u987B\u662F\u6709\u6548\u7684\u7EDD\u5BF9\u8DEF\u5F84\uFF0C\u5C1A\u672A\u4FEE\u6539\u7EC8\u7AEF\u914D\u7F6E\u3002");
|
|
489
|
+
return { home, shell, kind };
|
|
490
|
+
}
|
|
491
|
+
async function verifyMacExecutable(entry, options) {
|
|
492
|
+
await access(entry, constants2.X_OK);
|
|
493
|
+
const { stdout: stdout2 } = await execute(entry, ["--version", "--json"], { env: options.env, signal: options.signal, timeout: 15e3, maxBuffer: 1024 * 1024 });
|
|
494
|
+
const result = JSON.parse(stdout2);
|
|
495
|
+
if (result.version !== options.version || result.package !== options.packageName) throw new Error("CLI \u5B8C\u6574\u8DEF\u5F84\u6267\u884C\u7ED3\u679C\u4E0E\u8981\u6C42\u7684\u7248\u672C\u6216\u5305\u540D\u4E0D\u4E00\u81F4\uFF0C\u5C1A\u672A\u4FEE\u6539 PATH\u3002");
|
|
496
|
+
}
|
|
497
|
+
async function shellRun(shell, login, script, env, signal, trace = false) {
|
|
498
|
+
try {
|
|
499
|
+
return await execute(shell, [...trace ? ["-o", "SOURCETRACE"] : [], login ? "-lic" : "-ic", script], { env, cwd: env.HOME, signal, timeout: 15e3, maxBuffer: 1024 * 1024 });
|
|
500
|
+
} catch {
|
|
501
|
+
throw new Error(`\u65E0\u6CD5\u5B8C\u6210 ${basename(shell)} ${login ? "\u767B\u5F55" : "\u975E\u767B\u5F55"}\u4EA4\u4E92\u7EC8\u7AEF\u68C0\u67E5\uFF1B\u8BF7\u68C0\u67E5\u542F\u52A8\u914D\u7F6E\u662F\u5426\u62A5\u9519\u3001\u63D0\u524D\u9000\u51FA\u6216\u7B49\u5F85\u8F93\u5165\u3002`);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
function tracedFile(stderr2, name) {
|
|
505
|
+
return stderr2.split("\n").map((line) => /^\+(.+):\d+> <sourcetrace>$/u.exec(line)?.[1]).find((path) => path && basename(path) === name);
|
|
506
|
+
}
|
|
507
|
+
async function startupFiles(shell, kind, home, env, signal) {
|
|
508
|
+
if (kind === "bash") {
|
|
509
|
+
let login = join6(home, ".bash_profile");
|
|
510
|
+
for (const name of [".bash_profile", ".bash_login", ".profile"]) {
|
|
511
|
+
const path = join6(home, name);
|
|
512
|
+
try {
|
|
513
|
+
await access(path, constants2.R_OK);
|
|
514
|
+
login = path;
|
|
515
|
+
break;
|
|
516
|
+
} catch (error) {
|
|
517
|
+
if (error.code !== "ENOENT") throw error;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
return [join6(home, ".bashrc"), login];
|
|
521
|
+
}
|
|
522
|
+
const files = /* @__PURE__ */ new Set();
|
|
523
|
+
for (const login of [false, true]) {
|
|
524
|
+
const marker = `buddy-setup-${randomUUID2()}:`;
|
|
525
|
+
const script = `export ZDOTDIR; ${quoteShell(process.execPath)} -e ${quoteShell(`process.stdout.write(${JSON.stringify(marker)} + JSON.stringify({ home: process.env.HOME, zdotdir: process.env.ZDOTDIR }))`)}`;
|
|
526
|
+
const result = await shellRun(shell, login, script, env, signal, true);
|
|
527
|
+
const markerIndex = result.stdout.lastIndexOf(marker);
|
|
528
|
+
if (markerIndex < 0) throw new Error("\u65E0\u6CD5\u8BFB\u53D6 zsh \u7684\u5B9E\u9645\u542F\u52A8\u76EE\u5F55\uFF0C\u5C1A\u672A\u4FEE\u6539\u7EC8\u7AEF\u914D\u7F6E\u3002");
|
|
529
|
+
const actual = JSON.parse(result.stdout.slice(markerIndex + marker.length));
|
|
530
|
+
const directory = actual.zdotdir || actual.home;
|
|
531
|
+
if (typeof directory !== "string" || !safePath(directory)) throw new Error("zsh \u7684\u5B9E\u9645 ZDOTDIR \u65E0\u6548\uFF0C\u5C1A\u672A\u4FEE\u6539\u7EC8\u7AEF\u914D\u7F6E\u3002");
|
|
532
|
+
files.add(tracedFile(result.stderr, ".zshrc") ?? join6(directory, ".zshrc"));
|
|
533
|
+
if (login) files.add(tracedFile(result.stderr, ".zlogin") ?? join6(directory, ".zlogin"));
|
|
534
|
+
}
|
|
535
|
+
return [...files];
|
|
536
|
+
}
|
|
537
|
+
async function planEdit(path, block, start, end) {
|
|
538
|
+
if (!safePath(path)) throw new Error("\u7EC8\u7AEF\u914D\u7F6E\u8DEF\u5F84\u65E0\u6548\uFF0C\u5C1A\u672A\u4FEE\u6539 PATH\u3002");
|
|
539
|
+
let before, mode = 384;
|
|
540
|
+
const link = await lstat3(path).catch((error) => {
|
|
541
|
+
if (error.code === "ENOENT") return void 0;
|
|
542
|
+
throw error;
|
|
543
|
+
});
|
|
544
|
+
try {
|
|
545
|
+
path = await realpath4(path);
|
|
546
|
+
const info = await stat(path);
|
|
547
|
+
if (!info.isFile()) throw new Error("\u7EC8\u7AEF\u542F\u52A8\u914D\u7F6E\u4E0D\u662F\u666E\u901A\u6587\u4EF6\uFF0C\u5C1A\u672A\u4FEE\u6539 PATH\u3002");
|
|
548
|
+
mode = info.mode & 511;
|
|
549
|
+
before = await readFile4(path, "utf8");
|
|
550
|
+
} catch (error) {
|
|
551
|
+
if (error.code !== "ENOENT") throw error;
|
|
552
|
+
if (link?.isSymbolicLink()) throw new Error(`\u7EC8\u7AEF\u914D\u7F6E\u662F\u60AC\u7A7A\u7B26\u53F7\u94FE\u63A5\uFF0C\u5C1A\u672A\u4FEE\u6539\uFF1A${path}`);
|
|
553
|
+
}
|
|
554
|
+
const content = before ?? "";
|
|
555
|
+
const a = content.indexOf(start), b = content.indexOf(end);
|
|
556
|
+
if (a < 0 !== b < 0 || a >= 0 && (b < a || content.indexOf(start, a + start.length) >= 0 || content.indexOf(end, b + end.length) >= 0)) {
|
|
557
|
+
throw new Error(`\u7EC8\u7AEF\u914D\u7F6E\u4E2D\u7684 CLI PATH \u6807\u8BB0\u4E0D\u5B8C\u6574\uFF0C\u8BF7\u5148\u68C0\u67E5 ${path}\uFF1B\u5C1A\u672A\u4FEE\u6539\u914D\u7F6E\u3002`);
|
|
558
|
+
}
|
|
559
|
+
const after = a >= 0 ? content.slice(0, a) + block + content.slice(b + end.length) : content + (content && !content.endsWith("\n") ? "\n" : "") + block + "\n";
|
|
560
|
+
return { path, before, after, mode };
|
|
561
|
+
}
|
|
562
|
+
async function replace(edit, content) {
|
|
563
|
+
const temporary = join6(dirname3(edit.path), `.buddy-path-${randomUUID2()}.tmp`);
|
|
564
|
+
try {
|
|
565
|
+
await writeFile4(temporary, content, { flag: "wx", mode: edit.mode });
|
|
566
|
+
await chmod(temporary, edit.mode);
|
|
567
|
+
await rename2(temporary, edit.path);
|
|
568
|
+
} finally {
|
|
569
|
+
await unlink2(temporary).catch((error) => {
|
|
570
|
+
if (error.code !== "ENOENT") throw error;
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
async function configureMacUserPath(binDirectory, options) {
|
|
575
|
+
const env = { ...options.env ?? process.env };
|
|
576
|
+
const { shell, home, kind } = macSetupEnvironment(env, options.shell);
|
|
577
|
+
if (!safePath(binDirectory) || binDirectory.includes(":") || !/^[A-Za-z0-9_-]+$/u.test(options.command)) throw new Error("CLI PATH \u6216\u547D\u4EE4\u540D\u65E0\u6548\uFF0C\u5C1A\u672A\u4FEE\u6539\u7EC8\u7AEF\u914D\u7F6E\u3002");
|
|
578
|
+
const entry = join6(binDirectory, options.command);
|
|
579
|
+
await verifyMacExecutable(entry, options);
|
|
580
|
+
const expected = await realpath4(entry);
|
|
581
|
+
const actualBin = await realpath4(binDirectory);
|
|
582
|
+
const inherited = await Promise.all((env.PATH ?? "").split(":").map(async (path) => ({
|
|
583
|
+
path,
|
|
584
|
+
actual: await realpath4(resolve2(home, path || ".")).catch(() => normalize(resolve2(home, path || ".")))
|
|
585
|
+
})));
|
|
586
|
+
env.PATH = inherited.filter(({ path, actual }) => normalize(path) !== normalize(binDirectory) && actual !== actualBin).map(({ path }) => path).join(":");
|
|
587
|
+
const files = await startupFiles(shell, kind, home, env, options.signal);
|
|
588
|
+
const start = `# >>> ${options.command} PATH >>>`, end = `# <<< ${options.command} PATH <<<`;
|
|
589
|
+
const currentShellCommand = `export PATH=${quoteShell(binDirectory)}:"$PATH"`;
|
|
590
|
+
const block = `${start}
|
|
591
|
+
case ":$PATH:" in
|
|
592
|
+
*:${quoteShell(binDirectory)}:*) ;;
|
|
593
|
+
*) ${currentShellCommand} ;;
|
|
594
|
+
esac
|
|
595
|
+
${end}`;
|
|
596
|
+
const planned = await Promise.all([...new Set(files)].map((path) => planEdit(path, block, start, end)));
|
|
597
|
+
const edits = [...new Map(planned.map((edit) => [edit.path, edit])).values()];
|
|
598
|
+
const changed = [];
|
|
599
|
+
try {
|
|
600
|
+
for (const edit of edits) {
|
|
601
|
+
options.signal?.throwIfAborted();
|
|
602
|
+
if (edit.before === edit.after) continue;
|
|
603
|
+
const current = await readFile4(edit.path, "utf8").catch((error) => {
|
|
604
|
+
if (error.code === "ENOENT") return void 0;
|
|
605
|
+
throw error;
|
|
606
|
+
});
|
|
607
|
+
if (current !== edit.before) throw new Error(`\u7EC8\u7AEF\u914D\u7F6E\u5728\u5B89\u88C5\u671F\u95F4\u53D1\u751F\u6539\u53D8\uFF0C\u8BF7\u91CD\u8BD5\uFF1A${edit.path}`);
|
|
608
|
+
await replace(edit, edit.after);
|
|
609
|
+
changed.push(edit);
|
|
610
|
+
}
|
|
611
|
+
for (const login of [false, true]) {
|
|
612
|
+
const marker = `buddy-path-${randomUUID2()}:`;
|
|
613
|
+
const script = `builtin printf '%s\\n' ${quoteShell(marker)}; command -v ${options.command}; ${options.command} --version --json`;
|
|
614
|
+
const result = await shellRun(shell, login, script, env, options.signal);
|
|
615
|
+
const index = result.stdout.lastIndexOf(marker + "\n");
|
|
616
|
+
if (index < 0) throw new Error("\u65B0\u7EC8\u7AEF\u672A\u80FD\u5B8C\u6210 CLI \u547D\u4EE4\u53D1\u73B0\u68C0\u67E5\u3002");
|
|
617
|
+
const [resolved, ...versionLines] = result.stdout.slice(index + marker.length + 1).trim().split("\n");
|
|
618
|
+
if (!resolved || !isAbsolute(resolved) || await realpath4(resolved) !== expected) throw new Error("\u65B0\u7EC8\u7AEF\u672A\u89E3\u6790\u5230\u672C\u6B21\u5B89\u88C5\u7684 CLI\uFF0C\u672A\u786E\u8BA4 PATH \u914D\u7F6E\u6210\u529F\u3002");
|
|
619
|
+
const version = JSON.parse(versionLines.join("\n"));
|
|
620
|
+
if (version.version !== options.version || version.package !== options.packageName) throw new Error("\u65B0\u7EC8\u7AEF\u89E3\u6790\u5230\u5176\u4ED6\u7248\u672C\u6216\u6E20\u9053\u7684 CLI\uFF0C\u672A\u786E\u8BA4 PATH \u914D\u7F6E\u6210\u529F\u3002");
|
|
621
|
+
}
|
|
622
|
+
} catch (cause) {
|
|
623
|
+
const unrestored = [];
|
|
624
|
+
for (const edit of changed.reverse()) {
|
|
625
|
+
try {
|
|
626
|
+
if (await readFile4(edit.path, "utf8") !== edit.after) {
|
|
627
|
+
unrestored.push(edit.path);
|
|
628
|
+
continue;
|
|
629
|
+
}
|
|
630
|
+
if (edit.before === void 0) await unlink2(edit.path);
|
|
631
|
+
else await replace(edit, edit.before);
|
|
632
|
+
} catch {
|
|
633
|
+
unrestored.push(edit.path);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
throw new Error(`${cause instanceof Error ? cause.message : String(cause)}${unrestored.length ? ` \u90E8\u5206\u914D\u7F6E\u672A\u6062\u590D\uFF0C\u8BF7\u4FDD\u7559\u5E76\u68C0\u67E5\uFF1A${unrestored.join("\u3001")}` : " \u672C\u6B21\u7EC8\u7AEF\u914D\u7F6E\u6539\u52A8\u5DF2\u6062\u590D\uFF1B\u672A\u62A5\u544A\u5B89\u88C5\u5C31\u7EEA\u3002"}`);
|
|
637
|
+
}
|
|
638
|
+
return { shell, configuredFiles: edits.map((edit) => edit.path), currentShellCommand };
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
// apps/cli/src/setup.ts
|
|
642
|
+
var PACKAGE3 = CLI_PACKAGE_NAME3;
|
|
643
|
+
var REGISTRY3 = "https://registry.npmjs.org/";
|
|
644
|
+
var SETUP_UNAVAILABLE = LANE_BUILD3?.distribution === "local" ? CLI_UPDATE_GUIDANCE2 : `\u5F53\u524D\u5236\u54C1\u5DF2\u7981\u7528 setup\uFF1B\u8BF7\u6309\u539F\u5B89\u88C5\u65B9\u5F0F\u5B89\u88C5 ${PACKAGE3}@dev\u3002`;
|
|
645
|
+
async function runSetupCommand(args, io, options) {
|
|
646
|
+
if (args[0] !== "setup") return void 0;
|
|
647
|
+
const json = args.includes("--json");
|
|
648
|
+
const fail = (error, exitCode = 1) => {
|
|
649
|
+
if (json) io.write(JSON.stringify({ error }));
|
|
650
|
+
else io.writeError(error);
|
|
651
|
+
return { exitCode };
|
|
652
|
+
};
|
|
653
|
+
if (args.slice(1).some((arg) => !["--help", "-h", "--json"].includes(arg)) || new Set(args).size !== args.length) return fail("\u53C2\u6570\u65E0\u6548\uFF0C\u8BF7\u8FD0\u884C " + CLI_COMMAND_NAME3 + " setup --help \u67E5\u770B\u7528\u6CD5\u3002", 2);
|
|
654
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
655
|
+
const help = !CLI_WINDOWS_SETUP_AVAILABLE ? `${CLI_COMMAND_NAME3} setup\uFF1A${SETUP_UNAVAILABLE}` : `\u7528\u6CD5\uFF1A${CLI_COMMAND_NAME3} setup [--json]
|
|
656
|
+
${CLI_MAC_SETUP_AVAILABLE ? "macOS\uFF1A\u5B89\u88C5\u5230\u7528\u6237\u72EC\u7ACB\u76EE\u5F55\uFF1B\u5DF2\u5B89\u88C5\u51C6\u786E\u7248\u672C\u65F6\u53EA\u4FEE\u590D PATH\u3002\u6309\u5B9E\u9645 zsh/bash \u542F\u52A8\u914D\u7F6E\u6301\u4E45\u4FDD\u5B58\u8DEF\u5F84\uFF0C\u9A8C\u8BC1\u65B0\u767B\u5F55\u548C\u975E\u767B\u5F55\u4EA4\u4E92\u7EC8\u7AEF\u3002\u65B0\u7EC8\u7AEF\u81EA\u52A8\u751F\u6548\uFF1B\u5DF2\u6253\u5F00\u7684\u7EC8\u7AEF\u6216 Agent \u4F1A\u8BDD\u9700\u6267\u884C\u8F93\u51FA\u7684 export \u547D\u4EE4\uFF0C\u5B89\u88C5\u5668\u4E0D\u80FD\u4FEE\u6539\u7236 shell\u3002\u5176\u4ED6 shell \u6682\u4E0D\u652F\u6301\u3002\n" : ""}Windows\uFF1A\u5728\u5F53\u524D\u7528\u6237\u76EE\u5F55\u5B89\u88C5\u8FD0\u884C\u4E2D\u7684\u51C6\u786E ${PACKAGE3} \u7248\u672C\uFF0C\u5E76\u5C06\u72EC\u7ACB\u5B89\u88C5\u76EE\u5F55\u52A0\u5165\u7528\u6237 PATH\u3002\u68C0\u6D4B\u5230\u5E94\u7528\u5BB9\u5668\u91CD\u5B9A\u5411\u65F6\u505C\u6B62\uFF0C\u8BF7\u4ECE\u5F00\u59CB\u83DC\u5355\u6253\u5F00\u72EC\u7ACB PowerShell/CMD \u540E\u91CD\u8BD5\u3002\u65E0\u9700\u7BA1\u7406\u5458\u6743\u9650\uFF1B\u5B89\u88C5\u540E\u5B8C\u5168\u9000\u51FA\u7EC8\u7AEF\u6216 Coding Agent \u5E94\u7528\u540E\u91CD\u65B0\u6253\u5F00\u3002\u6B63\u5F0F\u7248\u3001SIT \u548C\u72EC\u7ACB\u5305\u4F7F\u7528\u4E0D\u540C\u76EE\u5F55\u3002${LANE_BUILD3 ? `\u66F4\u65B0\u65F6\u91CD\u65B0\u8FD0\u884C\u7F51\u9875\u63D0\u4F9B\u7684 ${PACKAGE3}@dev \u5B89\u88C5\u547D\u4EE4\u3002` : ""}`;
|
|
657
|
+
io.write(json ? JSON.stringify({ help }) : help);
|
|
658
|
+
return { exitCode: 0 };
|
|
659
|
+
}
|
|
660
|
+
try {
|
|
661
|
+
if (!CLI_WINDOWS_SETUP_AVAILABLE) throw new Error(SETUP_UNAVAILABLE);
|
|
662
|
+
const platform = options.platform ?? process.platform;
|
|
663
|
+
const mac = platform === "darwin" && CLI_MAC_SETUP_AVAILABLE;
|
|
664
|
+
if (platform !== "win32" && !mac) throw new Error("\u5F53\u524D\u5236\u54C1\u7684 setup \u4E0D\u652F\u6301\u6B64\u7CFB\u7EDF\uFF1B\u8BF7\u4F7F\u7528 macOS \u6216 Windows \u7684\u5BF9\u5E94\u5B89\u88C5\u5165\u53E3\u3002");
|
|
665
|
+
if (!/^\d+\.\d+\.\d+-dev\.\d+$/u.test(options.version) || LANE_BUILD3 && options.version !== LANE_BUILD3.sourceVersion) throw new Error("\u5B89\u88C5\u5668\u7248\u672C\u65E0\u6548\uFF0C\u5DF2\u505C\u6B62\u5B89\u88C5\u3002");
|
|
666
|
+
const env = options.env ?? process.env;
|
|
667
|
+
if (mac) {
|
|
668
|
+
const { home } = macSetupEnvironment(env);
|
|
669
|
+
const prefix2 = join7(home, ".local", "share", "buddy-cli", LANE_BUILD3?.id ?? "sit");
|
|
670
|
+
const binDirectory = join7(prefix2, "bin"), entry = join7(binDirectory, CLI_COMMAND_NAME3);
|
|
671
|
+
const packageRoot = join7(prefix2, "lib", "node_modules", PACKAGE3);
|
|
672
|
+
const validate = async () => {
|
|
673
|
+
const manifest = JSON.parse(await readFile5(join7(packageRoot, "package.json"), "utf8"));
|
|
674
|
+
if (manifest.name !== PACKAGE3 || manifest.version !== options.version || manifest.bin?.[CLI_COMMAND_NAME3] !== "dist/bin/buddy.js") throw new Error("\u5B89\u88C5\u7ED3\u679C\u4E0E\u8981\u6C42\u7684\u7248\u672C\u6216\u6E20\u9053\u4E0D\u4E00\u81F4\uFF0C\u5C1A\u672A\u4FEE\u6539 PATH\u3002");
|
|
675
|
+
assertBuildManifest2(manifest, "darwin");
|
|
676
|
+
if (await realpath5(entry) !== await realpath5(join7(packageRoot, "dist/bin/buddy.js"))) throw new Error("CLI \u547D\u4EE4\u5165\u53E3\u4E0D\u5C5E\u4E8E\u672C\u6B21\u5B89\u88C5\u76EE\u5F55\uFF0C\u5C1A\u672A\u4FEE\u6539 PATH\u3002");
|
|
677
|
+
await verifyMacExecutable(entry, { ...options, env, packageName: PACKAGE3 });
|
|
678
|
+
};
|
|
679
|
+
await (options.prepareDirectory ?? ensurePrivateDirectory)(prefix2);
|
|
680
|
+
let configured, reusedInstallation = false;
|
|
681
|
+
await withUpdateLock(globalUpdateLock(options.cacheDirectory ?? defaultUpdateCache(), prefix2), options.signal, async () => {
|
|
682
|
+
try {
|
|
683
|
+
await validate();
|
|
684
|
+
reusedInstallation = true;
|
|
685
|
+
} catch {
|
|
686
|
+
}
|
|
687
|
+
if (!reusedInstallation) {
|
|
688
|
+
if (!json) io.write(`\u6B63\u5728\u5B89\u88C5 ${CLI_COMMAND_NAME3} ${options.version}\u2026`);
|
|
689
|
+
await installCliWithNpm(
|
|
690
|
+
[
|
|
691
|
+
"install",
|
|
692
|
+
"--global",
|
|
693
|
+
"--prefix",
|
|
694
|
+
prefix2,
|
|
695
|
+
`${PACKAGE3}@${options.version}`,
|
|
696
|
+
`--registry=${REGISTRY3}`,
|
|
697
|
+
`--@my-life-buddies:registry=${REGISTRY3}`,
|
|
698
|
+
"--engine-strict",
|
|
699
|
+
"--no-audit",
|
|
700
|
+
"--no-fund"
|
|
701
|
+
],
|
|
702
|
+
{ cwd: prefix2, signal: options.signal, timeout: 18e4 },
|
|
703
|
+
options.runNpm ?? runNpm
|
|
704
|
+
);
|
|
705
|
+
await validate();
|
|
706
|
+
}
|
|
707
|
+
configured = await (options.configureMacUserPath ?? configureMacUserPath)(binDirectory, { env, command: CLI_COMMAND_NAME3, version: options.version, packageName: PACKAGE3, signal: options.signal });
|
|
708
|
+
});
|
|
709
|
+
const message2 = `\u5B89\u88C5\u4E0E PATH \u914D\u7F6E\u5DF2\u5B8C\u6210\uFF0C\u5DF2\u9A8C\u8BC1\u65B0\u767B\u5F55\u548C\u975E\u767B\u5F55\u4EA4\u4E92\u7EC8\u7AEF\u3002\u65B0\u5F00\u7EC8\u7AEF\u540E\u8FD0\u884C ${CLI_COMMAND_NAME3} --version\uFF1B\u5DF2\u6253\u5F00\u7684\u7528\u6237\u7EC8\u7AEF\u6216 Agent \u6267\u884C\u4F1A\u8BDD\u4E0D\u4F1A\u81EA\u52A8\u66F4\u65B0\uFF0C\u8BF7\u5728\u5404\u81EA\u4F1A\u8BDD\u6267\u884C\uFF1A
|
|
710
|
+
${configured.currentShellCommand}`;
|
|
711
|
+
io.write(json ? JSON.stringify({ version: options.version, prefix: prefix2, command: CLI_COMMAND_NAME3, reusedInstallation, ...configured, message: message2 }) : message2);
|
|
712
|
+
return { exitCode: 0 };
|
|
713
|
+
}
|
|
714
|
+
const local = env[Object.keys(env).find((key) => key.toLowerCase() === "localappdata") ?? "LOCALAPPDATA"];
|
|
715
|
+
if (!local || !/^[A-Za-z]:[\\/]/u.test(local) || local.startsWith("\\\\") || /[;\u0000-\u001f]/u.test(local)) throw new Error("\u65E0\u6CD5\u5B9A\u4F4D\u5F53\u524D\u7528\u6237\u7684\u672C\u5730\u5E94\u7528\u76EE\u5F55\uFF0C\u8BF7\u68C0\u67E5 LOCALAPPDATA\u3002");
|
|
716
|
+
const prefix = join7(local, "buddy-cli", LANE_BUILD3?.id ?? "sit");
|
|
717
|
+
await (options.prepareDirectory ?? ensurePrivateDirectory)(prefix);
|
|
718
|
+
await assertSetupDirectoryLocation(prefix, options.resolveInstallPath);
|
|
719
|
+
await withUpdateLock(globalUpdateLock(options.cacheDirectory ?? defaultUpdateCache(), prefix), options.signal, async () => {
|
|
720
|
+
if (!json) io.write(`\u6B63\u5728\u5B89\u88C5 ${CLI_COMMAND_NAME3} ${options.version}\u2026`);
|
|
721
|
+
await installCliWithNpm(
|
|
722
|
+
[
|
|
723
|
+
"install",
|
|
724
|
+
"--global",
|
|
725
|
+
"--prefix",
|
|
726
|
+
prefix,
|
|
727
|
+
`${PACKAGE3}@${options.version}`,
|
|
728
|
+
`--registry=${REGISTRY3}`,
|
|
729
|
+
`--@my-life-buddies:registry=${REGISTRY3}`,
|
|
730
|
+
"--engine-strict",
|
|
731
|
+
"--no-audit",
|
|
732
|
+
"--no-fund"
|
|
733
|
+
],
|
|
734
|
+
{ cwd: prefix, signal: options.signal, timeout: 18e4 },
|
|
735
|
+
options.runNpm ?? runNpm
|
|
736
|
+
);
|
|
737
|
+
const manifest = JSON.parse(await readFile5(join7(prefix, "node_modules", PACKAGE3, "package.json"), "utf8"));
|
|
738
|
+
if (manifest.name !== PACKAGE3 || manifest.version !== options.version || manifest.bin?.[CLI_COMMAND_NAME3] !== "dist/bin/buddy.js") throw new Error("\u5B89\u88C5\u7ED3\u679C\u4E0E\u8981\u6C42\u7684\u7248\u672C\u6216\u6E20\u9053\u4E0D\u4E00\u81F4\uFF0C\u5C1A\u672A\u4FEE\u6539 PATH\u3002");
|
|
739
|
+
assertBuildManifest2(manifest, "win32");
|
|
740
|
+
await readFile5(join7(prefix, `${CLI_COMMAND_NAME3}.cmd`), "utf8");
|
|
741
|
+
const ps1 = await lstat4(join7(prefix, `${CLI_COMMAND_NAME3}.ps1`)).catch((error) => {
|
|
742
|
+
if (error.code === "ENOENT") return void 0;
|
|
743
|
+
throw error;
|
|
744
|
+
});
|
|
745
|
+
if (ps1) throw new Error("Windows \u547D\u4EE4\u5165\u53E3\u672A\u5B8C\u6210\u914D\u7F6E\uFF0C\u5C1A\u672A\u4FEE\u6539 PATH\u3002\u8BF7\u786E\u8BA4 npm \u5B89\u88C5\u811A\u672C\u672A\u88AB\u7981\u7528\u540E\u91CD\u8BD5\uFF1B\u65E0\u9700\u4FEE\u6539 PowerShell \u6267\u884C\u7B56\u7565\u3002");
|
|
746
|
+
await assertSetupFileLocation(join7(prefix, `${CLI_COMMAND_NAME3}.cmd`), options.resolveInstallPath);
|
|
747
|
+
await assertSetupFileLocation(join7(prefix, "node_modules", PACKAGE3, "package.json"), options.resolveInstallPath);
|
|
748
|
+
await (options.configureUserPath ?? (async (path) => {
|
|
749
|
+
await windowsOperation({ operation: "user-path", path });
|
|
750
|
+
}))(prefix);
|
|
751
|
+
});
|
|
752
|
+
const message = "\u5B89\u88C5\u5B8C\u6210\u3002\u8BF7\u5B8C\u5168\u9000\u51FA\u7EC8\u7AEF\u6216 Coding Agent \u5E94\u7528\u540E\u91CD\u65B0\u6253\u5F00\uFF0C\u7136\u540E\u8FD0\u884C " + CLI_COMMAND_NAME3 + " --version\u3002";
|
|
753
|
+
io.write(json ? JSON.stringify({ version: options.version, prefix, command: CLI_COMMAND_NAME3, message }) : message);
|
|
754
|
+
return { exitCode: 0 };
|
|
755
|
+
} catch (cause) {
|
|
756
|
+
return fail(cause instanceof Error ? cause.message : String(cause));
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
// apps/cli/src/termination.ts
|
|
761
|
+
function bindTerminationSignals(controller) {
|
|
762
|
+
const handlers = /* @__PURE__ */ new Map();
|
|
763
|
+
for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
|
|
764
|
+
const handler = () => {
|
|
765
|
+
if (!controller.signal.aborted) controller.abort(new Error(signal));
|
|
766
|
+
};
|
|
767
|
+
handlers.set(signal, handler);
|
|
768
|
+
process.on(signal, handler);
|
|
769
|
+
}
|
|
770
|
+
return () => {
|
|
771
|
+
for (const [signal, handler] of handlers) process.off(signal, handler);
|
|
772
|
+
};
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
// apps/cli/src/bin/buddy.ts
|
|
776
|
+
import { assertBuildManifest as assertBuildManifest3, LANE_BUILD as LANE_BUILD4 } from "./platform.js";
|
|
777
|
+
var terminal = {
|
|
778
|
+
write(message) {
|
|
779
|
+
stdout.write(`${message}
|
|
780
|
+
`);
|
|
781
|
+
},
|
|
782
|
+
writeError(message) {
|
|
783
|
+
stderr.write(`${message}
|
|
784
|
+
`);
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
var lifecycle = new AbortController();
|
|
788
|
+
var unbindTermination = bindTerminationSignals(lifecycle);
|
|
789
|
+
var onOutputError = (error) => {
|
|
790
|
+
if (error.code === "EPIPE" || error.code === "EIO") lifecycle.abort(new Error("SIGHUP"));
|
|
791
|
+
else throw error;
|
|
792
|
+
};
|
|
793
|
+
stdout.on("error", onOutputError);
|
|
794
|
+
stderr.on("error", onOutputError);
|
|
795
|
+
try {
|
|
796
|
+
assertSupportedDeveloperPlatform();
|
|
797
|
+
const args = process.argv.slice(2);
|
|
798
|
+
const require2 = createRequire(import.meta.url);
|
|
799
|
+
const manifestPath = require2.resolve(`${CLI_PACKAGE_NAME4}/package.json`);
|
|
800
|
+
const manifest = require2(manifestPath);
|
|
801
|
+
assertBuildManifest3(manifest);
|
|
802
|
+
if (LANE_BUILD4 && !args.includes("--json")) {
|
|
803
|
+
terminal.writeError(`[${LANE_BUILD4.distribution === "npm" ? "\u6CF3\u9053 npm \u6D4B\u8BD5" : "\u672C\u5730\u6CF3\u9053\u6D4B\u8BD5"} ${LANE_BUILD4.id}] ${LANE_BUILD4.origin} \xB7 ${LANE_BUILD4.validationScope} \xB7 ${LANE_BUILD4.distribution === "npm" ? "\u81EA\u52A8\u66F4\u65B0\u72EC\u7ACB npm \u5305" : "\u7981\u6B62\u516C\u5171 npm \u53D1\u5E03/\u66F4\u65B0"}`);
|
|
804
|
+
}
|
|
805
|
+
const options = { version: manifest.version, packageRoot: dirname4(manifestPath), signal: lifecycle.signal };
|
|
806
|
+
const maintenance = await runSetupCommand(args, terminal, options) ?? await runSelfUpdateCommand(args, terminal, options) ?? await runAutoUpdate(args, terminal, options);
|
|
807
|
+
const result = maintenance ?? await (await import("./command-runner.js")).runCommand(args, lifecycle.signal);
|
|
808
|
+
process.exitCode = result.exitCode;
|
|
809
|
+
} catch (cause) {
|
|
810
|
+
const args = process.argv.slice(2);
|
|
811
|
+
if (args[0] === "diagnose" && args.includes("--json")) terminal.write(JSON.stringify({
|
|
812
|
+
ok: false,
|
|
813
|
+
command: `diagnose.${args[1] ?? "help"}`,
|
|
814
|
+
error: { code: "CLI_START_FAILED", message: cause instanceof Error ? cause.message : "CLI \u672A\u80FD\u542F\u52A8\uFF0C\u539F\u6392\u67E5\u5C1A\u672A\u6267\u884C\u3002", retryable: false }
|
|
815
|
+
}));
|
|
816
|
+
else terminal.writeError(`${CLI_COMMAND_NAME4}: ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
817
|
+
process.exitCode = 1;
|
|
818
|
+
} finally {
|
|
819
|
+
unbindTermination();
|
|
820
|
+
}
|
|
821
|
+
//# sourceMappingURL=buddy.js.map
|