@hagicode/hagiscript 0.1.6 → 0.1.8
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 +144 -201
- package/bin/runtime +16 -0
- package/dist/cli.js +4 -0
- package/dist/cli.js.map +1 -1
- package/dist/commands/pm2-commands.d.ts +2 -0
- package/dist/commands/pm2-commands.js +54 -0
- package/dist/commands/pm2-commands.js.map +1 -0
- package/dist/commands/runtime-commands.d.ts +2 -0
- package/dist/commands/runtime-commands.js +136 -0
- package/dist/commands/runtime-commands.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/runtime/command-launch.js +1 -0
- package/dist/runtime/command-launch.js.map +1 -1
- package/dist/runtime/pm2-manager.d.ts +54 -0
- package/dist/runtime/pm2-manager.js +258 -0
- package/dist/runtime/pm2-manager.js.map +1 -0
- package/dist/runtime/runtime-executor.d.ts +45 -0
- package/dist/runtime/runtime-executor.js +153 -0
- package/dist/runtime/runtime-executor.js.map +1 -0
- package/dist/runtime/runtime-manager.d.ts +79 -0
- package/dist/runtime/runtime-manager.js +651 -0
- package/dist/runtime/runtime-manager.js.map +1 -0
- package/dist/runtime/runtime-manifest.d.ts +77 -0
- package/dist/runtime/runtime-manifest.js +277 -0
- package/dist/runtime/runtime-manifest.js.map +1 -0
- package/dist/runtime/runtime-paths.d.ts +31 -0
- package/dist/runtime/runtime-paths.js +77 -0
- package/dist/runtime/runtime-paths.js.map +1 -0
- package/dist/runtime/runtime-state.d.ts +45 -0
- package/dist/runtime/runtime-state.js +82 -0
- package/dist/runtime/runtime-state.js.map +1 -0
- package/package.json +9 -5
- package/runtime/lib/runtime-script-lib.mjs +531 -0
- package/runtime/manifest.yaml +136 -0
- package/runtime/scripts/configure-code-server.mjs +14 -0
- package/runtime/scripts/configure-omniroute.mjs +16 -0
- package/runtime/scripts/install-code-server.mjs +53 -0
- package/runtime/scripts/install-dotnet.mjs +24 -0
- package/runtime/scripts/install-node.mjs +4 -0
- package/runtime/scripts/install-npm-packages.mjs +4 -0
- package/runtime/scripts/install-omniroute.mjs +60 -0
- package/runtime/scripts/remove-npm-packages.mjs +4 -0
- package/runtime/scripts/update-npm-packages.mjs +4 -0
- package/runtime/scripts/verify-dotnet.mjs +10 -0
- package/runtime/scripts/verify-node.mjs +4 -0
- package/runtime/templates/code-server-config.yaml +5 -0
- package/runtime/templates/omniroute-config.yaml +4 -0
- package/README_cn.md +0 -266
|
@@ -0,0 +1,651 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, join, relative } from "node:path";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { installNodeRuntime, resolveManagedNodeRuntime } from "./node-installer.js";
|
|
6
|
+
import { verifyNodeRuntime } from "./node-verify.js";
|
|
7
|
+
import { syncNpmGlobals } from "./npm-sync.js";
|
|
8
|
+
import { executeRuntimeScript, writeRuntimeLog } from "./runtime-executor.js";
|
|
9
|
+
import { loadRuntimeManifest } from "./runtime-manifest.js";
|
|
10
|
+
import { getComponentConfigDirectory, getComponentLogsDirectory, getComponentManagedRoot, getComponentPm2Home, getComponentRuntimeDataHome, isPathInsideRuntimeRoot, resolveRuntimePaths } from "./runtime-paths.js";
|
|
11
|
+
import { mergeRuntimeState, readRuntimeState, writeRuntimeState } from "./runtime-state.js";
|
|
12
|
+
export class RuntimeLifecycleError extends Error {
|
|
13
|
+
constructor(message, options) {
|
|
14
|
+
super(message, options);
|
|
15
|
+
this.name = "RuntimeLifecycleError";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export async function installRuntime(options = {}) {
|
|
19
|
+
return runRuntimeLifecycle("install", options);
|
|
20
|
+
}
|
|
21
|
+
export async function removeRuntime(options = {}) {
|
|
22
|
+
return runRuntimeLifecycle("remove", options);
|
|
23
|
+
}
|
|
24
|
+
export async function updateRuntime(options = {}) {
|
|
25
|
+
return runRuntimeLifecycle("update", options);
|
|
26
|
+
}
|
|
27
|
+
export async function runRuntimeLifecycle(phase, options = {}) {
|
|
28
|
+
const manifest = await loadRuntimeManifest({ manifestPath: options.manifestPath });
|
|
29
|
+
const paths = resolveRuntimePaths(manifest, { runtimeRoot: options.runtimeRoot });
|
|
30
|
+
const existingState = await readRuntimeState(paths.stateFile);
|
|
31
|
+
const state = mergeRuntimeState(manifest, paths, existingState);
|
|
32
|
+
const { plan, skipped } = planRuntimeLifecycle(phase, manifest, state, options);
|
|
33
|
+
const now = options.now ?? (() => new Date());
|
|
34
|
+
const logger = options.logger ?? (() => undefined);
|
|
35
|
+
if (options.dryRun || (phase === "update" && options.checkOnly)) {
|
|
36
|
+
for (const action of plan) {
|
|
37
|
+
logger(`Plan: ${phase} ${action.componentName} (${action.strategy}${action.reason ? `: ${action.reason}` : ""})`);
|
|
38
|
+
}
|
|
39
|
+
for (const item of skipped) {
|
|
40
|
+
logger(`Skip: ${item.componentName} (${item.reason})`);
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
manifest,
|
|
44
|
+
paths,
|
|
45
|
+
state,
|
|
46
|
+
plan,
|
|
47
|
+
skipped,
|
|
48
|
+
changedComponents: []
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
await ensureManagedDirectories(paths);
|
|
52
|
+
const logFilePath = join(paths.logs, `${phase}-${now().toISOString().replaceAll(":", "-")}.log`);
|
|
53
|
+
const operationState = {
|
|
54
|
+
phase,
|
|
55
|
+
status: "success",
|
|
56
|
+
selectedComponents: plan.map((item) => item.componentName),
|
|
57
|
+
completedComponents: [],
|
|
58
|
+
startedAt: now().toISOString(),
|
|
59
|
+
finishedAt: now().toISOString(),
|
|
60
|
+
logFile: logFilePath
|
|
61
|
+
};
|
|
62
|
+
const changedComponents = [];
|
|
63
|
+
await writeRuntimeLog(logFilePath, `Runtime ${phase} starting with managed root ${paths.root}`);
|
|
64
|
+
try {
|
|
65
|
+
for (const action of plan) {
|
|
66
|
+
const component = manifest.componentMap.get(action.componentName);
|
|
67
|
+
if (!component) {
|
|
68
|
+
throw new RuntimeLifecycleError(`Unknown runtime component ${action.componentName}`);
|
|
69
|
+
}
|
|
70
|
+
logger(`Running ${phase}: ${component.name}`);
|
|
71
|
+
try {
|
|
72
|
+
const componentState = await executeRuntimeAction(action, component, manifest, paths, options, logFilePath);
|
|
73
|
+
state.components[component.name] = componentState;
|
|
74
|
+
changedComponents.push(component.name);
|
|
75
|
+
operationState.completedComponents.push(component.name);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
state.components[component.name] = {
|
|
79
|
+
name: component.name,
|
|
80
|
+
type: component.type,
|
|
81
|
+
status: "failed",
|
|
82
|
+
version: state.components[component.name]?.version ?? null,
|
|
83
|
+
managedProgramPaths: [getComponentManagedRoot(paths, component.name)],
|
|
84
|
+
managedDataPaths: [
|
|
85
|
+
getComponentRuntimeDataHome(paths, component.name, component.runtimeDataDir),
|
|
86
|
+
getComponentConfigDirectory(paths, component.name, component.runtimeDataDir),
|
|
87
|
+
getComponentLogsDirectory(paths, component.name, component.runtimeDataDir),
|
|
88
|
+
...(component.pm2
|
|
89
|
+
? [
|
|
90
|
+
getComponentPm2Home(paths, component.name, component.runtimeDataDir, component.pm2.pm2Home)
|
|
91
|
+
]
|
|
92
|
+
: [])
|
|
93
|
+
],
|
|
94
|
+
managedPaths: [
|
|
95
|
+
getComponentManagedRoot(paths, component.name),
|
|
96
|
+
getComponentRuntimeDataHome(paths, component.name, component.runtimeDataDir),
|
|
97
|
+
getComponentConfigDirectory(paths, component.name, component.runtimeDataDir),
|
|
98
|
+
getComponentLogsDirectory(paths, component.name, component.runtimeDataDir),
|
|
99
|
+
...(component.pm2
|
|
100
|
+
? [
|
|
101
|
+
getComponentPm2Home(paths, component.name, component.runtimeDataDir, component.pm2.pm2Home)
|
|
102
|
+
]
|
|
103
|
+
: [])
|
|
104
|
+
],
|
|
105
|
+
lastAction: phase,
|
|
106
|
+
lastUpdatedAt: now().toISOString(),
|
|
107
|
+
logFile: logFilePath,
|
|
108
|
+
details: {
|
|
109
|
+
error: error instanceof Error ? error.message : String(error)
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
throw new RuntimeLifecycleError(`${phase} failed for component ${component.name}: ${error instanceof Error ? error.message : String(error)}`, error instanceof Error ? { cause: error } : undefined);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
operationState.finishedAt = now().toISOString();
|
|
116
|
+
state.lastOperation = operationState;
|
|
117
|
+
await writeRuntimeState(paths.stateFile, state);
|
|
118
|
+
return {
|
|
119
|
+
manifest,
|
|
120
|
+
paths,
|
|
121
|
+
state,
|
|
122
|
+
plan,
|
|
123
|
+
skipped,
|
|
124
|
+
changedComponents,
|
|
125
|
+
logFilePath
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
operationState.status = "failed";
|
|
130
|
+
operationState.finishedAt = now().toISOString();
|
|
131
|
+
operationState.message = error instanceof Error ? error.message : String(error);
|
|
132
|
+
state.lastOperation = operationState;
|
|
133
|
+
await writeRuntimeLog(logFilePath, `Failure: ${operationState.message}`);
|
|
134
|
+
await writeRuntimeState(paths.stateFile, state);
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
export async function queryRuntimeState(options) {
|
|
139
|
+
const manifest = await loadRuntimeManifest({ manifestPath: options.manifestPath });
|
|
140
|
+
const paths = resolveRuntimePaths(manifest, { runtimeRoot: options.runtimeRoot });
|
|
141
|
+
const state = mergeRuntimeState(manifest, paths, await readRuntimeState(paths.stateFile));
|
|
142
|
+
const components = manifest.components.map((component) => {
|
|
143
|
+
const entry = state.components[component.name];
|
|
144
|
+
const fallbackProgramPaths = [getComponentManagedRoot(paths, component.name)];
|
|
145
|
+
const runtimeDataHome = component.name === "npm-packages"
|
|
146
|
+
? null
|
|
147
|
+
: getComponentRuntimeDataHome(paths, component.name, component.runtimeDataDir);
|
|
148
|
+
const pm2Home = component.pm2
|
|
149
|
+
? getComponentPm2Home(paths, component.name, component.runtimeDataDir, component.pm2.pm2Home)
|
|
150
|
+
: null;
|
|
151
|
+
const fallbackDataPaths = component.name === "npm-packages" || !runtimeDataHome
|
|
152
|
+
? []
|
|
153
|
+
: [
|
|
154
|
+
runtimeDataHome,
|
|
155
|
+
getComponentConfigDirectory(paths, component.name, component.runtimeDataDir),
|
|
156
|
+
getComponentLogsDirectory(paths, component.name, component.runtimeDataDir),
|
|
157
|
+
...(pm2Home ? [pm2Home] : [])
|
|
158
|
+
];
|
|
159
|
+
const programPaths = entry?.managedProgramPaths ?? fallbackProgramPaths;
|
|
160
|
+
const externalDataPaths = entry?.managedDataPaths ?? fallbackDataPaths;
|
|
161
|
+
return {
|
|
162
|
+
name: component.name,
|
|
163
|
+
type: component.type,
|
|
164
|
+
status: entry?.status ?? "not-installed",
|
|
165
|
+
version: entry?.version ?? null,
|
|
166
|
+
runtimeDataHome,
|
|
167
|
+
pm2Home,
|
|
168
|
+
programPaths,
|
|
169
|
+
externalDataPaths,
|
|
170
|
+
managedPaths: entry?.managedPaths ?? [...programPaths, ...externalDataPaths]
|
|
171
|
+
};
|
|
172
|
+
});
|
|
173
|
+
const programRoots = [
|
|
174
|
+
paths.runtimeHome,
|
|
175
|
+
paths.bin,
|
|
176
|
+
paths.componentsRoot,
|
|
177
|
+
paths.npmPrefix
|
|
178
|
+
];
|
|
179
|
+
const externalDataRoots = [
|
|
180
|
+
paths.runtimeDataRoot,
|
|
181
|
+
paths.config,
|
|
182
|
+
paths.logs,
|
|
183
|
+
paths.data,
|
|
184
|
+
paths.componentDataRoot
|
|
185
|
+
];
|
|
186
|
+
return {
|
|
187
|
+
runtime: state.runtime,
|
|
188
|
+
managedRoot: state.managedRoot,
|
|
189
|
+
managedPaths: state.managedPaths,
|
|
190
|
+
layout: {
|
|
191
|
+
separated: paths.runtimeHome !== paths.runtimeDataRoot,
|
|
192
|
+
runtimeHome: paths.runtimeHome,
|
|
193
|
+
runtimeDataRoot: paths.runtimeDataRoot,
|
|
194
|
+
programRoots,
|
|
195
|
+
externalDataRoots
|
|
196
|
+
},
|
|
197
|
+
ready: components.every((component) => component.status === "installed"),
|
|
198
|
+
components,
|
|
199
|
+
lastOperation: state.lastOperation
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
export function renderRuntimeStateText(report) {
|
|
203
|
+
const lines = [
|
|
204
|
+
`Runtime: ${report.runtime.name} ${report.runtime.version}`,
|
|
205
|
+
`Manifest: ${report.runtime.manifestPath}`,
|
|
206
|
+
`Managed root: ${report.managedRoot}`,
|
|
207
|
+
`Runtime home: ${report.layout.runtimeHome}`,
|
|
208
|
+
`Runtime data root: ${report.layout.runtimeDataRoot}`,
|
|
209
|
+
`Bin: ${report.managedPaths.bin}`,
|
|
210
|
+
`State file: ${report.managedPaths.stateFile}`,
|
|
211
|
+
`Program roots: ${report.layout.programRoots.join(", ")}`,
|
|
212
|
+
`External data roots: ${report.layout.externalDataRoots.join(", ")}`,
|
|
213
|
+
`Separated layout: ${report.layout.separated ? "yes" : "no"}`,
|
|
214
|
+
`Ready: ${report.ready ? "yes" : "no"}`
|
|
215
|
+
];
|
|
216
|
+
for (const component of report.components) {
|
|
217
|
+
lines.push(`- ${component.name}: ${component.status} version=${component.version ?? "n/a"} program=${component.programPaths.join("|") || "n/a"} data=${component.externalDataPaths.join("|") || "n/a"}`);
|
|
218
|
+
if (component.runtimeDataHome) {
|
|
219
|
+
lines.push(` runtime-data-home=${component.runtimeDataHome}`);
|
|
220
|
+
}
|
|
221
|
+
if (component.pm2Home) {
|
|
222
|
+
lines.push(` pm2-home=${component.pm2Home}`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
if (report.lastOperation) {
|
|
226
|
+
lines.push(`Last operation: ${report.lastOperation.phase} ${report.lastOperation.status} (${report.lastOperation.completedComponents.length}/${report.lastOperation.selectedComponents.length})`);
|
|
227
|
+
}
|
|
228
|
+
return lines.join("\n");
|
|
229
|
+
}
|
|
230
|
+
export function planRuntimeLifecycle(phase, manifest, state, options = {}) {
|
|
231
|
+
const requestedSet = selectRequestedComponents(manifest, options.components);
|
|
232
|
+
const orderedComponentNames = orderedPhaseComponents(manifest, phase).filter((name) => requestedSet.has(name));
|
|
233
|
+
const plan = [];
|
|
234
|
+
const skipped = [];
|
|
235
|
+
for (const componentName of orderedComponentNames) {
|
|
236
|
+
const component = manifest.componentMap.get(componentName);
|
|
237
|
+
if (!component) {
|
|
238
|
+
throw new RuntimeLifecycleError(`Unknown runtime component ${componentName}`);
|
|
239
|
+
}
|
|
240
|
+
if (phase === "update" && !options.force) {
|
|
241
|
+
const currentState = state.components[component.name];
|
|
242
|
+
if (currentState?.status === "installed" && !componentNeedsUpdate(component, currentState)) {
|
|
243
|
+
skipped.push({
|
|
244
|
+
componentName: component.name,
|
|
245
|
+
reason: "already up to date"
|
|
246
|
+
});
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
plan.push(resolveRuntimeAction(component, phase));
|
|
251
|
+
}
|
|
252
|
+
return { plan, skipped };
|
|
253
|
+
}
|
|
254
|
+
async function executeRuntimeAction(action, component, manifest, paths, options, logFilePath) {
|
|
255
|
+
const componentRoot = getComponentManagedRoot(paths, component.name);
|
|
256
|
+
const componentConfigDir = getComponentConfigDirectory(paths, component.name, component.runtimeDataDir);
|
|
257
|
+
switch (component.name) {
|
|
258
|
+
case "node":
|
|
259
|
+
return executeNodeComponent(action.phase, component, paths, logFilePath);
|
|
260
|
+
case "npm-packages":
|
|
261
|
+
return executeNpmPackagesComponent(action.phase, component, manifest, paths, options, logFilePath);
|
|
262
|
+
default:
|
|
263
|
+
return executeScriptComponent(action, component, manifest, paths, componentRoot, componentConfigDir, options, logFilePath);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
async function executeNodeComponent(phase, component, paths, logFilePath) {
|
|
267
|
+
await mkdir(dirname(paths.nodeRuntime), { recursive: true });
|
|
268
|
+
if (phase === "remove") {
|
|
269
|
+
await rm(paths.nodeRuntime, { recursive: true, force: true });
|
|
270
|
+
await removeWrapper(paths.bin, "node");
|
|
271
|
+
await removeWrapper(paths.bin, "npm");
|
|
272
|
+
await writeRuntimeLog(logFilePath, "Removed managed node runtime directories");
|
|
273
|
+
return {
|
|
274
|
+
name: component.name,
|
|
275
|
+
type: component.type,
|
|
276
|
+
status: "removed",
|
|
277
|
+
version: null,
|
|
278
|
+
managedProgramPaths: [paths.nodeRuntime],
|
|
279
|
+
managedDataPaths: [],
|
|
280
|
+
managedPaths: [paths.nodeRuntime],
|
|
281
|
+
lastAction: phase,
|
|
282
|
+
lastUpdatedAt: new Date().toISOString(),
|
|
283
|
+
logFile: logFilePath
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
const desiredVersion = component.version ?? component.channelVersion;
|
|
287
|
+
const currentVerification = await verifyNodeRuntime(paths.nodeRuntime);
|
|
288
|
+
const normalizedCurrentVersion = normalizeVersion(currentVerification.nodeVersion);
|
|
289
|
+
const normalizedDesiredVersion = normalizeVersion(desiredVersion);
|
|
290
|
+
if (!currentVerification.valid) {
|
|
291
|
+
await rm(paths.nodeRuntime, { recursive: true, force: true });
|
|
292
|
+
}
|
|
293
|
+
if (phase === "update" &&
|
|
294
|
+
currentVerification.valid &&
|
|
295
|
+
normalizedDesiredVersion &&
|
|
296
|
+
normalizedCurrentVersion &&
|
|
297
|
+
normalizedDesiredVersion !== normalizedCurrentVersion) {
|
|
298
|
+
await rm(paths.nodeRuntime, { recursive: true, force: true });
|
|
299
|
+
}
|
|
300
|
+
const resolvedRuntime = await resolveNodeRuntimeForPhase(phase, paths.nodeRuntime, desiredVersion, currentVerification);
|
|
301
|
+
const wrappers = await materializeNodeWrappers(paths.bin, {
|
|
302
|
+
nodePath: resolvedRuntime.nodePath,
|
|
303
|
+
npmPath: resolvedRuntime.npmPath
|
|
304
|
+
});
|
|
305
|
+
await writeRuntimeLog(logFilePath, `Node runtime ready: ${resolvedRuntime.targetDirectory} (${resolvedRuntime.nodeVersion})`);
|
|
306
|
+
return {
|
|
307
|
+
name: component.name,
|
|
308
|
+
type: component.type,
|
|
309
|
+
status: "installed",
|
|
310
|
+
version: normalizeVersion(resolvedRuntime.nodeVersion),
|
|
311
|
+
managedProgramPaths: [paths.nodeRuntime, ...wrappers],
|
|
312
|
+
managedDataPaths: [],
|
|
313
|
+
managedPaths: [paths.nodeRuntime, ...wrappers],
|
|
314
|
+
lastAction: phase,
|
|
315
|
+
lastUpdatedAt: new Date().toISOString(),
|
|
316
|
+
logFile: logFilePath
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
async function executeNpmPackagesComponent(phase, component, manifest, paths, options, logFilePath) {
|
|
320
|
+
if (phase === "remove") {
|
|
321
|
+
await rm(paths.npmPrefix, { recursive: true, force: true });
|
|
322
|
+
await writeRuntimeLog(logFilePath, `Removed managed npm prefix ${paths.npmPrefix}`);
|
|
323
|
+
return {
|
|
324
|
+
name: component.name,
|
|
325
|
+
type: component.type,
|
|
326
|
+
status: "removed",
|
|
327
|
+
version: null,
|
|
328
|
+
managedProgramPaths: [paths.npmPrefix],
|
|
329
|
+
managedDataPaths: [],
|
|
330
|
+
managedPaths: [paths.npmPrefix],
|
|
331
|
+
lastAction: phase,
|
|
332
|
+
lastUpdatedAt: new Date().toISOString(),
|
|
333
|
+
logFile: logFilePath
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
await resolveManagedNodeRuntime({
|
|
337
|
+
targetDirectory: paths.nodeRuntime,
|
|
338
|
+
versionSelector: manifest.componentMap.get("node")?.version ?? "22"
|
|
339
|
+
});
|
|
340
|
+
const scratchDirectory = await mkdtemp(join(tmpdir(), "hagiscript-runtime-npm-"));
|
|
341
|
+
const manifestPath = join(scratchDirectory, "npm-sync-manifest.json");
|
|
342
|
+
const npmManifest = {
|
|
343
|
+
packages: Object.fromEntries(component.packageCatalog.map((entry) => [
|
|
344
|
+
entry.packageName,
|
|
345
|
+
toNpmSyncManifestEntry(entry.packageName, entry.installSpec)
|
|
346
|
+
]))
|
|
347
|
+
};
|
|
348
|
+
try {
|
|
349
|
+
await writeFile(manifestPath, `${JSON.stringify(npmManifest, null, 2)}\n`, "utf8");
|
|
350
|
+
const summary = await syncNpmGlobals({
|
|
351
|
+
runtimePath: paths.nodeRuntime,
|
|
352
|
+
manifestPath,
|
|
353
|
+
force: options.force,
|
|
354
|
+
npmOptions: {
|
|
355
|
+
prefix: paths.npmPrefix
|
|
356
|
+
},
|
|
357
|
+
onLog: (event) => appendNpmSyncLog(logFilePath, event)
|
|
358
|
+
});
|
|
359
|
+
await writeRuntimeLog(logFilePath, `Managed npm prefix ready: ${paths.npmPrefix} changed=${summary.changedCount}`);
|
|
360
|
+
}
|
|
361
|
+
finally {
|
|
362
|
+
await rm(scratchDirectory, { recursive: true, force: true });
|
|
363
|
+
}
|
|
364
|
+
return {
|
|
365
|
+
name: component.name,
|
|
366
|
+
type: component.type,
|
|
367
|
+
status: "installed",
|
|
368
|
+
version: computePackageCatalogFingerprint(component),
|
|
369
|
+
managedProgramPaths: [paths.npmPrefix],
|
|
370
|
+
managedDataPaths: [],
|
|
371
|
+
managedPaths: [paths.npmPrefix],
|
|
372
|
+
lastAction: phase,
|
|
373
|
+
lastUpdatedAt: new Date().toISOString(),
|
|
374
|
+
logFile: logFilePath,
|
|
375
|
+
details: {
|
|
376
|
+
packageCount: component.packageCatalog.length
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
async function executeScriptComponent(action, component, manifest, paths, componentRoot, componentConfigDir, options, logFilePath) {
|
|
381
|
+
await mkdir(componentRoot, { recursive: true });
|
|
382
|
+
await mkdir(componentConfigDir, { recursive: true });
|
|
383
|
+
const scriptPath = resolveScriptForAction(action, component);
|
|
384
|
+
if (scriptPath) {
|
|
385
|
+
await executeRuntimeScript(scriptPath, {
|
|
386
|
+
component,
|
|
387
|
+
phase: action.phase,
|
|
388
|
+
manifest,
|
|
389
|
+
paths,
|
|
390
|
+
componentRoot,
|
|
391
|
+
componentConfigDir,
|
|
392
|
+
logFilePath,
|
|
393
|
+
purge: options.purge,
|
|
394
|
+
verbose: options.verbose
|
|
395
|
+
});
|
|
396
|
+
if (action.phase !== "remove" && component.scripts.configure) {
|
|
397
|
+
await executeRuntimeScript(component.scripts.configure, {
|
|
398
|
+
component,
|
|
399
|
+
phase: action.phase,
|
|
400
|
+
manifest,
|
|
401
|
+
paths,
|
|
402
|
+
componentRoot,
|
|
403
|
+
componentConfigDir,
|
|
404
|
+
logFilePath,
|
|
405
|
+
purge: options.purge,
|
|
406
|
+
verbose: options.verbose
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
if (action.phase !== "remove" && component.scripts.verify) {
|
|
410
|
+
await executeRuntimeScript(component.scripts.verify, {
|
|
411
|
+
component,
|
|
412
|
+
phase: action.phase,
|
|
413
|
+
manifest,
|
|
414
|
+
paths,
|
|
415
|
+
componentRoot,
|
|
416
|
+
componentConfigDir,
|
|
417
|
+
logFilePath,
|
|
418
|
+
purge: options.purge,
|
|
419
|
+
verbose: options.verbose
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
const componentDataHome = getComponentRuntimeDataHome(paths, component.name, component.runtimeDataDir);
|
|
425
|
+
await cleanupManagedComponent([paths.root, paths.runtimeHome, paths.runtimeDataRoot], componentRoot, ...(options.purge ? [componentDataHome] : []));
|
|
426
|
+
}
|
|
427
|
+
const isRemoval = action.phase === "remove";
|
|
428
|
+
const managedProgramPaths = [componentRoot];
|
|
429
|
+
const componentDataHome = getComponentRuntimeDataHome(paths, component.name, component.runtimeDataDir);
|
|
430
|
+
const componentLogsDir = getComponentLogsDirectory(paths, component.name, component.runtimeDataDir);
|
|
431
|
+
const componentPm2Home = component.pm2
|
|
432
|
+
? getComponentPm2Home(paths, component.name, component.runtimeDataDir, component.pm2.pm2Home)
|
|
433
|
+
: null;
|
|
434
|
+
const managedDataPaths = options.purge || !isRemoval
|
|
435
|
+
? [
|
|
436
|
+
componentDataHome,
|
|
437
|
+
componentConfigDir,
|
|
438
|
+
componentLogsDir,
|
|
439
|
+
...(componentPm2Home ? [componentPm2Home] : [])
|
|
440
|
+
]
|
|
441
|
+
: [];
|
|
442
|
+
return {
|
|
443
|
+
name: component.name,
|
|
444
|
+
type: component.type,
|
|
445
|
+
status: isRemoval ? "removed" : "installed",
|
|
446
|
+
version: isRemoval ? null : component.version ?? component.channelVersion ?? null,
|
|
447
|
+
managedProgramPaths,
|
|
448
|
+
managedDataPaths,
|
|
449
|
+
managedPaths: [...managedProgramPaths, ...managedDataPaths],
|
|
450
|
+
lastAction: action.phase,
|
|
451
|
+
lastUpdatedAt: new Date().toISOString(),
|
|
452
|
+
logFile: logFilePath
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
function selectRequestedComponents(manifest, requestedComponents) {
|
|
456
|
+
if (!requestedComponents || requestedComponents.length === 0) {
|
|
457
|
+
return new Set(manifest.components.map((component) => component.name));
|
|
458
|
+
}
|
|
459
|
+
const requestedSet = new Set();
|
|
460
|
+
for (const value of requestedComponents) {
|
|
461
|
+
if (!manifest.componentMap.has(value)) {
|
|
462
|
+
throw new RuntimeLifecycleError(`Unknown runtime component: ${value}`);
|
|
463
|
+
}
|
|
464
|
+
requestedSet.add(value);
|
|
465
|
+
}
|
|
466
|
+
return requestedSet;
|
|
467
|
+
}
|
|
468
|
+
function orderedPhaseComponents(manifest, phase) {
|
|
469
|
+
const definition = manifest.phases[phase];
|
|
470
|
+
const ordered = [...definition.order];
|
|
471
|
+
return definition.reverse ? ordered.reverse() : ordered;
|
|
472
|
+
}
|
|
473
|
+
function resolveRuntimeAction(component, phase) {
|
|
474
|
+
if (component.name === "node" || component.name === "npm-packages") {
|
|
475
|
+
return {
|
|
476
|
+
componentName: component.name,
|
|
477
|
+
phase,
|
|
478
|
+
strategy: "builtin"
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
if (phase === "install") {
|
|
482
|
+
return {
|
|
483
|
+
componentName: component.name,
|
|
484
|
+
phase,
|
|
485
|
+
strategy: "script",
|
|
486
|
+
scriptPath: component.scripts.install
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
if (phase === "update") {
|
|
490
|
+
if (component.scripts.update) {
|
|
491
|
+
return {
|
|
492
|
+
componentName: component.name,
|
|
493
|
+
phase,
|
|
494
|
+
strategy: "script",
|
|
495
|
+
scriptPath: component.scripts.update
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
return {
|
|
499
|
+
componentName: component.name,
|
|
500
|
+
phase,
|
|
501
|
+
strategy: "fallback-install",
|
|
502
|
+
scriptPath: component.scripts.install,
|
|
503
|
+
reason: "update hook missing; reusing install hook"
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
if (component.scripts.remove) {
|
|
507
|
+
return {
|
|
508
|
+
componentName: component.name,
|
|
509
|
+
phase,
|
|
510
|
+
strategy: "script",
|
|
511
|
+
scriptPath: component.scripts.remove
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
return {
|
|
515
|
+
componentName: component.name,
|
|
516
|
+
phase,
|
|
517
|
+
strategy: "fallback-cleanup",
|
|
518
|
+
reason: "remove hook missing; cleaning managed paths only"
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
function componentNeedsUpdate(component, state) {
|
|
522
|
+
if (state.status !== "installed") {
|
|
523
|
+
return true;
|
|
524
|
+
}
|
|
525
|
+
if (component.name === "npm-packages") {
|
|
526
|
+
return state.version !== computePackageCatalogFingerprint(component);
|
|
527
|
+
}
|
|
528
|
+
const desiredVersion = component.version ?? component.channelVersion ?? null;
|
|
529
|
+
return desiredVersion !== state.version;
|
|
530
|
+
}
|
|
531
|
+
function resolveScriptForAction(action, component) {
|
|
532
|
+
switch (action.strategy) {
|
|
533
|
+
case "script":
|
|
534
|
+
case "fallback-install":
|
|
535
|
+
return action.scriptPath ?? component.scripts.install;
|
|
536
|
+
case "builtin":
|
|
537
|
+
case "fallback-cleanup":
|
|
538
|
+
return null;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
function toNpmSyncManifestEntry(packageName, installSpec) {
|
|
542
|
+
const trimmed = installSpec.trim();
|
|
543
|
+
const scopedPrefix = `${packageName}@`;
|
|
544
|
+
if (trimmed === packageName) {
|
|
545
|
+
return {
|
|
546
|
+
version: "*"
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
if (trimmed.startsWith(scopedPrefix)) {
|
|
550
|
+
const selector = trimmed.slice(scopedPrefix.length).trim();
|
|
551
|
+
return {
|
|
552
|
+
version: selector || "*",
|
|
553
|
+
target: selector || undefined
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
return {
|
|
557
|
+
version: "*",
|
|
558
|
+
target: trimmed
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
function computePackageCatalogFingerprint(component) {
|
|
562
|
+
return createHash("sha256")
|
|
563
|
+
.update(JSON.stringify(component.packageCatalog))
|
|
564
|
+
.digest("hex")
|
|
565
|
+
.slice(0, 12);
|
|
566
|
+
}
|
|
567
|
+
async function ensureManagedDirectories(paths) {
|
|
568
|
+
await Promise.all([
|
|
569
|
+
mkdir(paths.root, { recursive: true }),
|
|
570
|
+
mkdir(paths.runtimeHome, { recursive: true }),
|
|
571
|
+
mkdir(paths.runtimeDataRoot, { recursive: true }),
|
|
572
|
+
mkdir(paths.bin, { recursive: true }),
|
|
573
|
+
mkdir(paths.config, { recursive: true }),
|
|
574
|
+
mkdir(paths.logs, { recursive: true }),
|
|
575
|
+
mkdir(paths.data, { recursive: true }),
|
|
576
|
+
mkdir(paths.componentsRoot, { recursive: true }),
|
|
577
|
+
mkdir(paths.componentDataRoot, { recursive: true }),
|
|
578
|
+
mkdir(paths.vendoredRoot, { recursive: true })
|
|
579
|
+
]);
|
|
580
|
+
}
|
|
581
|
+
async function appendNpmSyncLog(logFilePath, event) {
|
|
582
|
+
await writeRuntimeLog(logFilePath, `npm-sync:${event.type} ${JSON.stringify(event)}`);
|
|
583
|
+
}
|
|
584
|
+
async function cleanupManagedComponent(allowedRoots, ...paths) {
|
|
585
|
+
for (const pathValue of paths) {
|
|
586
|
+
if (!allowedRoots.some((rootPath) => isPathInsideRuntimeRoot(rootPath, pathValue))) {
|
|
587
|
+
throw new RuntimeLifecycleError(`Refusing to clean path outside managed runtime root: ${pathValue}`);
|
|
588
|
+
}
|
|
589
|
+
await rm(pathValue, { recursive: true, force: true });
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
async function materializeNodeWrappers(binDirectory, executables) {
|
|
593
|
+
await mkdir(binDirectory, { recursive: true });
|
|
594
|
+
return Promise.all([
|
|
595
|
+
writeExecutableWrapper(binDirectory, "node", executables.nodePath),
|
|
596
|
+
writeExecutableWrapper(binDirectory, "npm", executables.npmPath)
|
|
597
|
+
]);
|
|
598
|
+
}
|
|
599
|
+
async function writeExecutableWrapper(binDirectory, commandName, targetPath) {
|
|
600
|
+
const wrapperPath = process.platform === "win32"
|
|
601
|
+
? join(binDirectory, `${commandName}.cmd`)
|
|
602
|
+
: join(binDirectory, commandName);
|
|
603
|
+
const relativeTarget = relative(binDirectory, targetPath);
|
|
604
|
+
await writeFile(wrapperPath, process.platform === "win32"
|
|
605
|
+
? `@echo off\r\n"%~dp0\\${relativeTarget.replaceAll("/", "\\")}" %*\r\n`
|
|
606
|
+
: `#!/usr/bin/env sh\nexec "$(dirname "$0")/${relativeTarget.replaceAll("\\", "/")}" "$@"\n`, "utf8");
|
|
607
|
+
return wrapperPath;
|
|
608
|
+
}
|
|
609
|
+
async function removeWrapper(binDirectory, commandName) {
|
|
610
|
+
const wrapperPath = process.platform === "win32"
|
|
611
|
+
? join(binDirectory, `${commandName}.cmd`)
|
|
612
|
+
: join(binDirectory, commandName);
|
|
613
|
+
await rm(wrapperPath, { force: true });
|
|
614
|
+
}
|
|
615
|
+
function normalizeVersion(value) {
|
|
616
|
+
if (!value) {
|
|
617
|
+
return null;
|
|
618
|
+
}
|
|
619
|
+
return value.replace(/^v/u, "");
|
|
620
|
+
}
|
|
621
|
+
async function resolveNodeRuntimeForPhase(phase, targetDirectory, desiredVersion, currentVerification) {
|
|
622
|
+
if (phase === "update" &&
|
|
623
|
+
currentVerification.valid &&
|
|
624
|
+
normalizeVersion(currentVerification.nodeVersion) === normalizeVersion(desiredVersion)) {
|
|
625
|
+
return {
|
|
626
|
+
targetDirectory: currentVerification.targetDirectory,
|
|
627
|
+
nodePath: currentVerification.nodePath ?? "",
|
|
628
|
+
npmPath: currentVerification.npmPath ?? "",
|
|
629
|
+
nodeVersion: currentVerification.nodeVersion ?? "",
|
|
630
|
+
npmVersion: currentVerification.npmVersion ?? ""
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
if (phase === "update") {
|
|
634
|
+
const installed = await installNodeRuntime({
|
|
635
|
+
targetDirectory,
|
|
636
|
+
versionSelector: desiredVersion
|
|
637
|
+
});
|
|
638
|
+
return {
|
|
639
|
+
targetDirectory: installed.targetDirectory,
|
|
640
|
+
nodePath: installed.nodePath,
|
|
641
|
+
npmPath: installed.npmPath,
|
|
642
|
+
nodeVersion: installed.version,
|
|
643
|
+
npmVersion: installed.npmVersion
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
return resolveManagedNodeRuntime({
|
|
647
|
+
targetDirectory,
|
|
648
|
+
versionSelector: desiredVersion
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
//# sourceMappingURL=runtime-manager.js.map
|