forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.ts
More file actions
209 lines (188 loc) · 6.31 KB
/
entry.ts
File metadata and controls
209 lines (188 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env node
import { spawn } from "node:child_process";
import { enableCompileCache } from "node:module";
import process from "node:process";
import { fileURLToPath } from "node:url";
import { isRootHelpInvocation, isRootVersionInvocation } from "./cli/argv.js";
import { parseCliContainerArgs, resolveCliContainerTarget } from "./cli/container-target.js";
import { applyCliProfileEnv, parseCliProfileArgs } from "./cli/profile.js";
import { normalizeWindowsArgv } from "./cli/windows-argv.js";
import { buildCliRespawnPlan } from "./entry.respawn.js";
import { isTruthyEnvValue, normalizeEnv } from "./infra/env.js";
import { isMainModule } from "./infra/is-main.js";
import { ensureOpenClawExecMarkerOnProcess } from "./infra/openclaw-exec-env.js";
import { installProcessWarningFilter } from "./infra/warning-filter.js";
import { attachChildProcessBridge } from "./process/child-process-bridge.js";
const ENTRY_WRAPPER_PAIRS = [
{ wrapperBasename: "openclaw.mjs", entryBasename: "entry.js" },
{ wrapperBasename: "openclaw.js", entryBasename: "entry.js" },
] as const;
function shouldForceReadOnlyAuthStore(argv: string[]): boolean {
const tokens = argv.slice(2).filter((token) => token.length > 0 && !token.startsWith("-"));
for (let index = 0; index < tokens.length - 1; index += 1) {
if (tokens[index] === "secrets" && tokens[index + 1] === "audit") {
return true;
}
}
return false;
}
// Guard: only run entry-point logic when this file is the main module.
// The bundler may import entry.js as a shared dependency when dist/index.js
// is the actual entry point; without this guard the top-level code below
// would call runCli a second time, starting a duplicate gateway that fails
// on the lock / port and crashes the process.
if (
!isMainModule({
currentFile: fileURLToPath(import.meta.url),
wrapperEntryPairs: [...ENTRY_WRAPPER_PAIRS],
})
) {
// Imported as a dependency — skip all entry-point side effects.
} else {
const { installGaxiosFetchCompat } = await import("./infra/gaxios-fetch-compat.js");
await installGaxiosFetchCompat();
process.title = "openclaw";
ensureOpenClawExecMarkerOnProcess();
installProcessWarningFilter();
normalizeEnv();
if (!isTruthyEnvValue(process.env.NODE_DISABLE_COMPILE_CACHE)) {
try {
enableCompileCache();
} catch {
// Best-effort only; never block startup.
}
}
if (shouldForceReadOnlyAuthStore(process.argv)) {
process.env.OPENCLAW_AUTH_STORE_READONLY = "1";
}
if (process.argv.includes("--no-color")) {
process.env.NO_COLOR = "1";
process.env.FORCE_COLOR = "0";
}
function ensureCliRespawnReady(): boolean {
const plan = buildCliRespawnPlan();
if (!plan) {
return false;
}
const child = spawn(process.execPath, plan.argv, {
stdio: "inherit",
env: plan.env,
});
attachChildProcessBridge(child);
child.once("exit", (code, signal) => {
if (signal) {
process.exitCode = 1;
return;
}
process.exit(code ?? 1);
});
child.once("error", (error) => {
console.error(
"[openclaw] Failed to respawn CLI:",
error instanceof Error ? (error.stack ?? error.message) : error,
);
process.exit(1);
});
// Parent must not continue running the CLI.
return true;
}
function tryHandleRootVersionFastPath(argv: string[]): boolean {
if (resolveCliContainerTarget(argv)) {
return false;
}
if (!isRootVersionInvocation(argv)) {
return false;
}
Promise.all([import("./version.js"), import("./infra/git-commit.js")])
.then(([{ VERSION }, { resolveCommitHash }]) => {
const commit = resolveCommitHash({ moduleUrl: import.meta.url });
console.log(commit ? `OpenClaw ${VERSION} (${commit})` : `OpenClaw ${VERSION}`);
process.exit(0);
})
.catch((error) => {
console.error(
"[openclaw] Failed to resolve version:",
error instanceof Error ? (error.stack ?? error.message) : error,
);
process.exitCode = 1;
});
return true;
}
process.argv = normalizeWindowsArgv(process.argv);
if (!ensureCliRespawnReady()) {
const parsedContainer = parseCliContainerArgs(process.argv);
if (!parsedContainer.ok) {
console.error(`[openclaw] ${parsedContainer.error}`);
process.exit(2);
}
const parsed = parseCliProfileArgs(parsedContainer.argv);
if (!parsed.ok) {
// Keep it simple; Commander will handle rich help/errors after we strip flags.
console.error(`[openclaw] ${parsed.error}`);
process.exit(2);
}
const containerTargetName = resolveCliContainerTarget(process.argv);
if (containerTargetName && parsed.profile) {
console.error("[openclaw] --container cannot be combined with --profile/--dev");
process.exit(2);
}
if (parsed.profile) {
applyCliProfileEnv({ profile: parsed.profile });
// Keep Commander and ad-hoc argv checks consistent.
process.argv = parsed.argv;
}
if (!tryHandleRootVersionFastPath(process.argv)) {
runMainOrRootHelp(process.argv);
}
}
}
export function tryHandleRootHelpFastPath(
argv: string[],
deps: {
outputRootHelp?: () => void | Promise<void>;
onError?: (error: unknown) => void;
env?: NodeJS.ProcessEnv;
} = {},
): boolean {
if (resolveCliContainerTarget(argv, deps.env)) {
return false;
}
if (!isRootHelpInvocation(argv)) {
return false;
}
const handleError =
deps.onError ??
((error: unknown) => {
console.error(
"[openclaw] Failed to display help:",
error instanceof Error ? (error.stack ?? error.message) : error,
);
process.exitCode = 1;
});
if (deps.outputRootHelp) {
Promise.resolve()
.then(() => deps.outputRootHelp?.())
.catch(handleError);
return true;
}
import("./cli/program/root-help.js")
.then(({ outputRootHelp }) => {
return outputRootHelp();
})
.catch(handleError);
return true;
}
function runMainOrRootHelp(argv: string[]): void {
if (tryHandleRootHelpFastPath(argv)) {
return;
}
import("./cli/run-main.js")
.then(({ runCli }) => runCli(argv))
.catch((error) => {
console.error(
"[openclaw] Failed to start CLI:",
error instanceof Error ? (error.stack ?? error.message) : error,
);
process.exitCode = 1;
});
}