





















@@ -3,6 +3,8 @@ import { randomUUID } from "node:crypto";
33import fs from "node:fs/promises";
44import os from "node:os";
55import path from "node:path";
6+import { pathToFileURL } from "node:url";
7+import { resolvePnpmRunner } from "./pnpm-runner.mjs";
6879type RunResult = {
810code: number | null;
@@ -11,6 +13,47 @@ type RunResult = {
1113stderr: string;
1214};
131516+type PnpmCommand = {
17+args: string[];
18+command: string;
19+env?: NodeJS.ProcessEnv;
20+shell: boolean;
21+windowsVerbatimArguments?: boolean;
22+};
23+24+type ResolvePnpmCommandOptions = {
25+comSpec?: string;
26+env?: NodeJS.ProcessEnv;
27+execPath?: string;
28+npmExecPath?: string;
29+platform?: NodeJS.Platform;
30+};
31+32+function resolveEnvValue(env: NodeJS.ProcessEnv, name: string): string | undefined {
33+const key = Object.keys(env).find((candidate) => candidate.toLowerCase() === name.toLowerCase());
34+return key === undefined ? undefined : env[key];
35+}
36+37+export function resolveZaiFallbackPnpmCommand(
38+args: string[],
39+options: ResolvePnpmCommandOptions = {},
40+): PnpmCommand {
41+const env = options.env ?? process.env;
42+const command = resolvePnpmRunner({
43+comSpec: options.comSpec ?? resolveEnvValue(env, "ComSpec"),
44+npmExecPath: options.npmExecPath ?? env.npm_execpath,
45+nodeExecPath: options.execPath ?? process.execPath,
46+platform: options.platform,
47+pnpmArgs: args,
48+});
49+if (command.env === undefined) {
50+const invocation = { ...command };
51+delete invocation.env;
52+return invocation;
53+}
54+return command;
55+}
56+1457function pickAnthropicEnv(): { type: "oauth" | "api"; value: string } | null {
1558const oauth = process.env.ANTHROPIC_OAUTH_TOKEN?.trim();
1659if (oauth) {
@@ -33,9 +76,12 @@ async function runCommand(
3376env: NodeJS.ProcessEnv,
3477): Promise<RunResult> {
3578return await new Promise((resolve, reject) => {
36-const child = spawn("pnpm", args, {
37- env,
79+const command = resolveZaiFallbackPnpmCommand(args, { env });
80+const child = spawn(command.command, command.args, {
81+env: command.env ?? env,
82+shell: command.shell,
3883stdio: ["ignore", "pipe", "pipe"],
84+windowsVerbatimArguments: command.windowsVerbatimArguments,
3985});
4086let stdout = "";
4187let stderr = "";
@@ -157,7 +203,14 @@ async function main() {
157203process.exit(run2.code ?? 1);
158204}
159205160-main().catch((err) => {
161-console.error(err);
162-process.exit(1);
163-});
206+function isCliEntrypoint() {
207+const entrypoint = process.argv[1];
208+return Boolean(entrypoint && import.meta.url === pathToFileURL(path.resolve(entrypoint)).href);
209+}
210+211+if (isCliEntrypoint()) {
212+await main().catch((err) => {
213+console.error(err);
214+process.exit(1);
215+});
216+}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。