hagiscript-sdk 0.2.9-dev.1780142424.1.dedbfb7

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.
Files changed (86) hide show
  1. package/README.md +25 -0
  2. package/dist/.tsbuildinfo +1 -0
  3. package/dist/package.json +38 -0
  4. package/dist/src/index.d.ts +24 -0
  5. package/dist/src/index.js +27 -0
  6. package/dist/src/index.js.map +1 -0
  7. package/dist/src/runtime/command-launch.d.ts +41 -0
  8. package/dist/src/runtime/command-launch.js +100 -0
  9. package/dist/src/runtime/command-launch.js.map +1 -0
  10. package/dist/src/runtime/component-service-manager.d.ts +71 -0
  11. package/dist/src/runtime/component-service-manager.js +367 -0
  12. package/dist/src/runtime/component-service-manager.js.map +1 -0
  13. package/dist/src/runtime/dotnet-installer.d.ts +39 -0
  14. package/dist/src/runtime/dotnet-installer.js +282 -0
  15. package/dist/src/runtime/dotnet-installer.js.map +1 -0
  16. package/dist/src/runtime/download-cache.d.ts +7 -0
  17. package/dist/src/runtime/download-cache.js +110 -0
  18. package/dist/src/runtime/download-cache.js.map +1 -0
  19. package/dist/src/runtime/manifest-manager.d.ts +57 -0
  20. package/dist/src/runtime/manifest-manager.js +188 -0
  21. package/dist/src/runtime/manifest-manager.js.map +1 -0
  22. package/dist/src/runtime/node-download.d.ts +13 -0
  23. package/dist/src/runtime/node-download.js +78 -0
  24. package/dist/src/runtime/node-download.js.map +1 -0
  25. package/dist/src/runtime/node-extract.d.ts +18 -0
  26. package/dist/src/runtime/node-extract.js +170 -0
  27. package/dist/src/runtime/node-extract.js.map +1 -0
  28. package/dist/src/runtime/node-installer.d.ts +43 -0
  29. package/dist/src/runtime/node-installer.js +114 -0
  30. package/dist/src/runtime/node-installer.js.map +1 -0
  31. package/dist/src/runtime/node-platform.d.ts +15 -0
  32. package/dist/src/runtime/node-platform.js +51 -0
  33. package/dist/src/runtime/node-platform.js.map +1 -0
  34. package/dist/src/runtime/node-release.d.ts +28 -0
  35. package/dist/src/runtime/node-release.js +149 -0
  36. package/dist/src/runtime/node-release.js.map +1 -0
  37. package/dist/src/runtime/node-verify.d.ts +30 -0
  38. package/dist/src/runtime/node-verify.js +102 -0
  39. package/dist/src/runtime/node-verify.js.map +1 -0
  40. package/dist/src/runtime/npm-global.d.ts +28 -0
  41. package/dist/src/runtime/npm-global.js +77 -0
  42. package/dist/src/runtime/npm-global.js.map +1 -0
  43. package/dist/src/runtime/npm-sync.d.ts +164 -0
  44. package/dist/src/runtime/npm-sync.js +603 -0
  45. package/dist/src/runtime/npm-sync.js.map +1 -0
  46. package/dist/src/runtime/pm2-manager.d.ts +102 -0
  47. package/dist/src/runtime/pm2-manager.js +715 -0
  48. package/dist/src/runtime/pm2-manager.js.map +1 -0
  49. package/dist/src/runtime/runtime-executor.d.ts +58 -0
  50. package/dist/src/runtime/runtime-executor.js +291 -0
  51. package/dist/src/runtime/runtime-executor.js.map +1 -0
  52. package/dist/src/runtime/runtime-manager.d.ts +92 -0
  53. package/dist/src/runtime/runtime-manager.js +798 -0
  54. package/dist/src/runtime/runtime-manager.js.map +1 -0
  55. package/dist/src/runtime/runtime-manifest.d.ts +106 -0
  56. package/dist/src/runtime/runtime-manifest.js +450 -0
  57. package/dist/src/runtime/runtime-manifest.js.map +1 -0
  58. package/dist/src/runtime/runtime-paths.d.ts +45 -0
  59. package/dist/src/runtime/runtime-paths.js +138 -0
  60. package/dist/src/runtime/runtime-paths.js.map +1 -0
  61. package/dist/src/runtime/runtime-state.d.ts +45 -0
  62. package/dist/src/runtime/runtime-state.js +82 -0
  63. package/dist/src/runtime/runtime-state.js.map +1 -0
  64. package/dist/src/runtime/server-config.d.ts +21 -0
  65. package/dist/src/runtime/server-config.js +169 -0
  66. package/dist/src/runtime/server-config.js.map +1 -0
  67. package/dist/src/runtime/server-manager.d.ts +107 -0
  68. package/dist/src/runtime/server-manager.js +946 -0
  69. package/dist/src/runtime/server-manager.js.map +1 -0
  70. package/dist/src/runtime/server-version-state.d.ts +43 -0
  71. package/dist/src/runtime/server-version-state.js +156 -0
  72. package/dist/src/runtime/server-version-state.js.map +1 -0
  73. package/dist/src/runtime/seven-zip-extract.d.ts +15 -0
  74. package/dist/src/runtime/seven-zip-extract.js +104 -0
  75. package/dist/src/runtime/seven-zip-extract.js.map +1 -0
  76. package/dist/src/runtime/tool-sync-catalog.config.json +59 -0
  77. package/dist/src/runtime/tool-sync-catalog.d.ts +48 -0
  78. package/dist/src/runtime/tool-sync-catalog.js +189 -0
  79. package/dist/src/runtime/tool-sync-catalog.js.map +1 -0
  80. package/dist/src/runtime/zip-extract.d.ts +6 -0
  81. package/dist/src/runtime/zip-extract.js +90 -0
  82. package/dist/src/runtime/zip-extract.js.map +1 -0
  83. package/dist/src/version.d.ts +7 -0
  84. package/dist/src/version.js +23 -0
  85. package/dist/src/version.js.map +1 -0
  86. package/package.json +38 -0
@@ -0,0 +1,282 @@
1
+ import { access, chmod, mkdtemp, rm, writeFile } from "node:fs/promises";
2
+ import { dirname, join, resolve } from "node:path";
3
+ import { copyDirectoryFromCache, isDownloadCacheEnabled, resolveDownloadCacheDirectory, storeDirectoryInCache } from "./download-cache.js";
4
+ import { assertTargetIsEmptyOrMissing, moveExtractedRootToTarget } from "./node-extract.js";
5
+ import { CommandExecutionError, runCommand } from "./command-launch.js";
6
+ const managedDotnetRuntimeNames = [
7
+ "Microsoft.NETCore.App",
8
+ "Microsoft.AspNetCore.App"
9
+ ];
10
+ const windowsPowerShellCommands = ["pwsh", "powershell.exe", "powershell"];
11
+ export async function installManagedDotnetRuntime(options) {
12
+ const targetDirectory = resolve(options.targetDirectory);
13
+ const version = normalizeRequiredVersion(options.version);
14
+ const runner = options.runner ?? runCommand;
15
+ const platform = options.platform ?? process.platform;
16
+ const architecture = mapDotnetArchitecture(options.architecture ?? process.arch);
17
+ const stagingRoot = await mkdtemp(join(dirname(targetDirectory), ".hagiscript-dotnet-"));
18
+ const stagedRuntimeDirectory = join(stagingRoot, "runtime");
19
+ const installerScriptPath = join(stagingRoot, platform === "win32" ? "dotnet-install.ps1" : "dotnet-install.sh");
20
+ const cacheRuntimeDirectory = isDownloadCacheEnabled(options.downloadCacheEnabled)
21
+ ? join(resolveDownloadCacheDirectory(options.downloadCacheDirectory), "dotnet", version, `${platform}-${architecture}`)
22
+ : undefined;
23
+ await assertTargetIsEmptyOrMissing(targetDirectory);
24
+ try {
25
+ if (cacheRuntimeDirectory) {
26
+ const restoredFromCache = await copyDirectoryFromCache(cacheRuntimeDirectory, stagedRuntimeDirectory);
27
+ if (restoredFromCache) {
28
+ const cachedVerification = await verifyManagedDotnetRuntime({
29
+ targetDirectory: stagedRuntimeDirectory,
30
+ version,
31
+ runner,
32
+ platform,
33
+ timeoutMs: options.timeoutMs
34
+ });
35
+ if (cachedVerification.valid) {
36
+ await moveExtractedRootToTarget(stagedRuntimeDirectory, targetDirectory);
37
+ return {
38
+ ...cachedVerification,
39
+ valid: true,
40
+ targetDirectory
41
+ };
42
+ }
43
+ await rm(cacheRuntimeDirectory, { recursive: true, force: true }).catch(() => undefined);
44
+ await rm(stagedRuntimeDirectory, { recursive: true, force: true }).catch(() => undefined);
45
+ }
46
+ }
47
+ await downloadDotnetInstallScript(installerScriptPath, {
48
+ platform,
49
+ scriptBaseUrl: options.scriptBaseUrl,
50
+ fetchImpl: options.fetchImpl
51
+ });
52
+ for (const runtimeKind of ["dotnet", "aspnetcore"]) {
53
+ const invocation = buildDotnetInstallInvocation({
54
+ installerScriptPath,
55
+ installDirectory: stagedRuntimeDirectory,
56
+ version,
57
+ runtime: runtimeKind,
58
+ platform,
59
+ architecture,
60
+ verbose: options.verbose
61
+ });
62
+ await runDotnetInstallInvocation(runner, invocation, {
63
+ timeoutMs: options.timeoutMs,
64
+ env: {
65
+ ...process.env,
66
+ DOTNET_CLI_TELEMETRY_OPTOUT: "1",
67
+ DOTNET_NOLOGO: "1",
68
+ DOTNET_SKIP_FIRST_TIME_EXPERIENCE: "1"
69
+ }
70
+ });
71
+ }
72
+ const verification = await verifyManagedDotnetRuntime({
73
+ targetDirectory: stagedRuntimeDirectory,
74
+ version,
75
+ runner,
76
+ platform,
77
+ timeoutMs: options.timeoutMs
78
+ });
79
+ if (!verification.valid) {
80
+ throw new Error(`Installed .NET runtime failed verification: ${verification.failureReason ?? "unknown failure"}`);
81
+ }
82
+ if (cacheRuntimeDirectory) {
83
+ await storeDirectoryInCache(stagedRuntimeDirectory, cacheRuntimeDirectory);
84
+ }
85
+ await moveExtractedRootToTarget(stagedRuntimeDirectory, targetDirectory);
86
+ return {
87
+ ...verification,
88
+ valid: true,
89
+ targetDirectory
90
+ };
91
+ }
92
+ finally {
93
+ await rm(stagingRoot, { recursive: true, force: true }).catch(() => undefined);
94
+ }
95
+ }
96
+ export async function verifyManagedDotnetRuntime(options) {
97
+ const targetDirectory = resolve(options.targetDirectory);
98
+ const version = normalizeRequiredVersion(options.version);
99
+ const platform = options.platform ?? process.platform;
100
+ const runner = options.runner ?? runCommand;
101
+ const dotnetPath = getManagedDotnetExecutablePath(targetDirectory, platform);
102
+ try {
103
+ await access(dotnetPath);
104
+ }
105
+ catch (error) {
106
+ return {
107
+ valid: false,
108
+ targetDirectory,
109
+ dotnetPath,
110
+ installedRuntimes: {},
111
+ requiredVersion: version,
112
+ failureReason: error instanceof Error
113
+ ? `Managed dotnet executable is missing: ${error.message}`
114
+ : "Managed dotnet executable is missing."
115
+ };
116
+ }
117
+ try {
118
+ const [infoResult, listRuntimesResult] = await Promise.all([
119
+ runner(dotnetPath, ["--info"], {
120
+ timeoutMs: options.timeoutMs
121
+ }),
122
+ runner(dotnetPath, ["--list-runtimes"], {
123
+ timeoutMs: options.timeoutMs
124
+ })
125
+ ]);
126
+ const installedRuntimes = parseInstalledDotnetRuntimes(listRuntimesResult.stdout);
127
+ for (const runtimeName of managedDotnetRuntimeNames) {
128
+ if (!installedRuntimes[runtimeName]?.includes(version)) {
129
+ return {
130
+ valid: false,
131
+ targetDirectory,
132
+ dotnetPath,
133
+ installedRuntimes,
134
+ requiredVersion: version,
135
+ infoOutput: infoResult.stdout,
136
+ listRuntimesOutput: listRuntimesResult.stdout,
137
+ failureReason: `Missing required runtime ${runtimeName} ${version}.`
138
+ };
139
+ }
140
+ }
141
+ return {
142
+ valid: true,
143
+ targetDirectory,
144
+ dotnetPath,
145
+ installedRuntimes,
146
+ requiredVersion: version,
147
+ infoOutput: infoResult.stdout,
148
+ listRuntimesOutput: listRuntimesResult.stdout
149
+ };
150
+ }
151
+ catch (error) {
152
+ return {
153
+ valid: false,
154
+ targetDirectory,
155
+ dotnetPath,
156
+ installedRuntimes: {},
157
+ requiredVersion: version,
158
+ failureReason: error instanceof Error ? error.message : "Failed to execute managed dotnet verification."
159
+ };
160
+ }
161
+ }
162
+ export function getManagedDotnetExecutablePath(targetDirectory, platform = process.platform) {
163
+ return join(resolve(targetDirectory), platform === "win32" ? "dotnet.exe" : "dotnet");
164
+ }
165
+ export function parseInstalledDotnetRuntimes(output) {
166
+ const runtimes = {};
167
+ for (const line of output.split(/\r?\n/u)) {
168
+ const match = /^(?<name>\S+)\s+(?<version>\S+)\s+\[[^\]]+\]$/u.exec(line.trim());
169
+ if (!match?.groups?.name || !match.groups.version) {
170
+ continue;
171
+ }
172
+ const versions = (runtimes[match.groups.name] ??= []);
173
+ versions.push(match.groups.version);
174
+ }
175
+ return runtimes;
176
+ }
177
+ export function mapDotnetArchitecture(architecture) {
178
+ switch (architecture) {
179
+ case "x64":
180
+ return "x64";
181
+ case "arm64":
182
+ case "aarch64":
183
+ return "arm64";
184
+ default:
185
+ throw new Error(`Unsupported .NET runtime architecture: ${architecture}`);
186
+ }
187
+ }
188
+ function normalizeRequiredVersion(version) {
189
+ const normalized = version.trim();
190
+ if (!normalized) {
191
+ throw new Error("Managed .NET runtime version must be a non-empty string.");
192
+ }
193
+ return normalized;
194
+ }
195
+ async function downloadDotnetInstallScript(destinationPath, options) {
196
+ const fetchImpl = options.fetchImpl ?? globalThis.fetch;
197
+ if (typeof fetchImpl !== "function") {
198
+ throw new Error("Global fetch is unavailable for downloading the .NET installer.");
199
+ }
200
+ const url = buildDotnetInstallScriptUrl(options.platform, options.scriptBaseUrl);
201
+ const response = await fetchImpl(url);
202
+ if (!response.ok) {
203
+ throw new Error(`Failed to download .NET installer ${url}: HTTP ${response.status}`);
204
+ }
205
+ const scriptContents = await response.text();
206
+ await writeFile(destinationPath, scriptContents, "utf8");
207
+ if (options.platform !== "win32") {
208
+ await chmod(destinationPath, 0o755);
209
+ }
210
+ }
211
+ export function buildDotnetInstallScriptUrl(platform, scriptBaseUrl = "https://dot.net/v1") {
212
+ const normalizedBaseUrl = scriptBaseUrl.replace(/\/+$/u, "");
213
+ return `${normalizedBaseUrl}/${platform === "win32" ? "dotnet-install.ps1" : "dotnet-install.sh"}`;
214
+ }
215
+ function buildDotnetInstallInvocation(options) {
216
+ if (options.platform === "win32") {
217
+ return {
218
+ commands: windowsPowerShellCommands,
219
+ args: [
220
+ "-NoLogo",
221
+ "-NoProfile",
222
+ "-NonInteractive",
223
+ "-ExecutionPolicy",
224
+ "Bypass",
225
+ "-File",
226
+ options.installerScriptPath,
227
+ "-InstallDir",
228
+ options.installDirectory,
229
+ "-Version",
230
+ options.version,
231
+ "-Runtime",
232
+ options.runtime,
233
+ "-Architecture",
234
+ options.architecture,
235
+ "-NoPath",
236
+ ...(options.verbose ? ["-Verbose"] : [])
237
+ ]
238
+ };
239
+ }
240
+ return {
241
+ commands: ["bash"],
242
+ args: [
243
+ options.installerScriptPath,
244
+ "--install-dir",
245
+ options.installDirectory,
246
+ "--version",
247
+ options.version,
248
+ "--runtime",
249
+ options.runtime,
250
+ "--architecture",
251
+ options.architecture,
252
+ "--no-path",
253
+ ...(options.verbose ? ["--verbose"] : [])
254
+ ]
255
+ };
256
+ }
257
+ async function runDotnetInstallInvocation(runner, invocation, options) {
258
+ const failures = [];
259
+ for (const command of invocation.commands) {
260
+ try {
261
+ await runner(command, invocation.args, options);
262
+ return;
263
+ }
264
+ catch (error) {
265
+ failures.push(formatDotnetInstallFailure(command, invocation.args, error));
266
+ }
267
+ }
268
+ throw new Error(`Failed to execute .NET installer. Attempts:\n${failures.map((failure) => `- ${failure}`).join("\n")}`);
269
+ }
270
+ function formatDotnetInstallFailure(command, args, error) {
271
+ const commandLine = `${command} ${args.join(" ")}`;
272
+ if (error instanceof CommandExecutionError) {
273
+ const details = [
274
+ error.context.shortMessage || error.message,
275
+ error.context.stderr.trim() ? `stderr: ${error.context.stderr.trim()}` : "",
276
+ error.context.stdout.trim() ? `stdout: ${error.context.stdout.trim()}` : ""
277
+ ].filter(Boolean);
278
+ return `${commandLine} => ${details.join(" | ")}`;
279
+ }
280
+ return `${commandLine} => ${error instanceof Error ? error.message : String(error)}`;
281
+ }
282
+ //# sourceMappingURL=dotnet-installer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dotnet-installer.js","sourceRoot":"","sources":["../../../../src/runtime/dotnet-installer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACzE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EACL,sBAAsB,EACtB,sBAAsB,EACtB,6BAA6B,EAC7B,qBAAqB,EACtB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,4BAA4B,EAC5B,yBAAyB,EAC1B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,qBAAqB,EACrB,UAAU,EAGX,MAAM,qBAAqB,CAAC;AA+B7B,MAAM,yBAAyB,GAAG;IAChC,uBAAuB;IACvB,0BAA0B;CAClB,CAAC;AACX,MAAM,yBAAyB,GAAG,CAAC,MAAM,EAAE,gBAAgB,EAAE,YAAY,CAAU,CAAC;AAEpF,MAAM,CAAC,KAAK,UAAU,2BAA2B,CAC/C,OAA2C;IAE3C,MAAM,eAAe,GAAG,OAAO,CAAC,OAAO,CAAC,e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AED,OAAO,GAAG,WAAW,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;AACvF,CAAC"}
@@ -0,0 +1,7 @@
1
+ export declare function isDownloadCacheEnabled(enabled: boolean | undefined): boolean;
2
+ export declare function getDefaultDownloadCacheDirectory(): string;
3
+ export declare function resolveDownloadCacheDirectory(directory?: string): string;
4
+ export declare function copyFileFromCache(cachePath: string, destinationPath: string): Promise<number | undefined>;
5
+ export declare function copyDirectoryFromCache(cachePath: string, destinationPath: string): Promise<boolean>;
6
+ export declare function storeFileInCache(sourcePath: string, cachePath: string): Promise<void>;
7
+ export declare function storeDirectoryInCache(sourcePath: string, cachePath: string): Promise<void>;
@@ -0,0 +1,110 @@
1
+ import { copyFile, cp, mkdir, rename, rm, stat } from "node:fs/promises";
2
+ import { dirname, join, resolve } from "node:path";
3
+ import { homedir } from "node:os";
4
+ export function isDownloadCacheEnabled(enabled) {
5
+ return enabled ?? true;
6
+ }
7
+ export function getDefaultDownloadCacheDirectory() {
8
+ return join(homedir(), ".hagiscript", "download-cache");
9
+ }
10
+ export function resolveDownloadCacheDirectory(directory) {
11
+ const normalized = directory?.trim();
12
+ return resolve(normalized && normalized.length > 0
13
+ ? normalized
14
+ : getDefaultDownloadCacheDirectory());
15
+ }
16
+ export async function copyFileFromCache(cachePath, destinationPath) {
17
+ try {
18
+ const cachedFile = await stat(cachePath);
19
+ await mkdir(dirname(destinationPath), { recursive: true });
20
+ await copyFile(cachePath, destinationPath);
21
+ return cachedFile.size;
22
+ }
23
+ catch (error) {
24
+ if (isMissingPathError(error)) {
25
+ return undefined;
26
+ }
27
+ throw error;
28
+ }
29
+ }
30
+ export async function copyDirectoryFromCache(cachePath, destinationPath) {
31
+ try {
32
+ await stat(cachePath);
33
+ await mkdir(dirname(destinationPath), { recursive: true });
34
+ await cp(cachePath, destinationPath, {
35
+ recursive: true,
36
+ force: false,
37
+ errorOnExist: true
38
+ });
39
+ return true;
40
+ }
41
+ catch (error) {
42
+ if (isMissingPathError(error)) {
43
+ return false;
44
+ }
45
+ throw error;
46
+ }
47
+ }
48
+ export async function storeFileInCache(sourcePath, cachePath) {
49
+ try {
50
+ await stat(cachePath);
51
+ return;
52
+ }
53
+ catch (error) {
54
+ if (!isMissingPathError(error)) {
55
+ throw error;
56
+ }
57
+ }
58
+ const temporaryPath = getTemporaryCachePath(cachePath);
59
+ await mkdir(dirname(cachePath), { recursive: true });
60
+ await copyFile(sourcePath, temporaryPath);
61
+ await moveIntoCache(temporaryPath, cachePath);
62
+ }
63
+ export async function storeDirectoryInCache(sourcePath, cachePath) {
64
+ try {
65
+ await stat(cachePath);
66
+ return;
67
+ }
68
+ catch (error) {
69
+ if (!isMissingPathError(error)) {
70
+ throw error;
71
+ }
72
+ }
73
+ const temporaryPath = getTemporaryCachePath(cachePath);
74
+ await mkdir(dirname(cachePath), { recursive: true });
75
+ await cp(sourcePath, temporaryPath, {
76
+ recursive: true,
77
+ force: false,
78
+ errorOnExist: true
79
+ });
80
+ await moveIntoCache(temporaryPath, cachePath);
81
+ }
82
+ function getTemporaryCachePath(cachePath) {
83
+ return `${cachePath}.tmp-${process.pid}-${Date.now()}-${Math.random()
84
+ .toString(16)
85
+ .slice(2)}`;
86
+ }
87
+ async function moveIntoCache(temporaryPath, cachePath) {
88
+ try {
89
+ await rename(temporaryPath, cachePath);
90
+ }
91
+ catch (error) {
92
+ if (!isAlreadyCachedError(error)) {
93
+ throw error;
94
+ }
95
+ }
96
+ finally {
97
+ await rm(temporaryPath, { recursive: true, force: true }).catch(() => undefined);
98
+ }
99
+ }
100
+ function isMissingPathError(error) {
101
+ return (error instanceof Error &&
102
+ "code" in error &&
103
+ (error.code === "ENOENT" || error.code === "ENOTDIR"));
104
+ }
105
+ function isAlreadyCachedError(error) {
106
+ return (error instanceof Error &&
107
+ "code" in error &&
108
+ (error.code === "EEXIST" || error.code === "ENOTEMPTY"));
109
+ }
110
+ //# sourceMappingURL=download-cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"download-cache.js","sourceRoot":"","sources":["../../../../src/runtime/download-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACzE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,MAAM,UAAU,sBAAsB,CAAC,OAA4B;IACjE,OAAO,OAAO,IAAI,IAAI,CAAC;AACzB,CAAC;AAED,MAAM,UAAU,gCAAgC;IAC9C,OAAO,IAAI,CAAC,OAAO,EAAE,EAAE,aAAa,EAAE,gBAAgB,CAAC,CAAC;AAC1D,CAAC;AAED,MAAM,UAAU,6BAA6B,CAAC,SAAkB;IAC9D,MAAM,UAAU,GAAG,SAAS,EAAE,IAAI,EAAE,CAAC;IACrC,OAAO,OAAO,CACZ,UAAU,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC;QACjC,CAAC,CAAC,UAAU;QACZ,CAAC,CAAC,gCAAgC,EAAE,CACvC,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,SAAiB,EACjB,eAAuB;IAEvB,IAAI,CAAC;QACH,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,CAAC;QACzC,MAAM,KAAK,CAAC,OAAO,CAAC,eAAe,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAC3D,MAAM,QAAQ,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;QAC3C,OAAO,UAAU,CAAC,IAAI,CAAC;IACzB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,kBAAkB,CAAC,KAAK,CAAC,EAAE,CAAC;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S,CAChB,CAAC;IACJ,CAAC;AACH,CAAC;AAED,SAAS,kBAAkB,CAAC,KAAc;IACxC,OAAO,CACL,KAAK,YAAY,KAAK;QACtB,MAAM,IAAI,KAAK;QACf,CAAC,KAAK,CAAC,IAAI,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CACtD,CAAC;AACJ,CAAC;AAED,SAAS,oBAAoB,CAAC,KAAc;IAC1C,OAAO,CACL,KAAK,YAAY,KAAK;QACtB,MAAM,IAAI,KAAK;QACf,CAAC,KAAK,CAAC,IAAI,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,KAAK,WAAW,CAAC,CACxD,CAAC;AACJ,CAAC"}
@@ -0,0 +1,57 @@
1
+ import { type LoadedRuntimeManifest } from "./runtime-manifest.js";
2
+ export interface RuntimeManifestPathUpdates {
3
+ runtimeRoot?: string;
4
+ runtimeHome?: string;
5
+ runtimeDataRoot?: string;
6
+ serverProgramRoot?: string;
7
+ serverDataRoot?: string;
8
+ }
9
+ export interface RuntimeManifestNpmPackageUpdate {
10
+ packageName: string;
11
+ version: string;
12
+ target?: string;
13
+ }
14
+ export interface RuntimeManifestMutationOptions {
15
+ manifestPath: string;
16
+ pathUpdates?: RuntimeManifestPathUpdates;
17
+ npmPackageUpdates?: readonly RuntimeManifestNpmPackageUpdate[];
18
+ serverActiveVersion?: string;
19
+ }
20
+ export interface InitRuntimeManifestOptions extends RuntimeManifestMutationOptions {
21
+ force?: boolean;
22
+ }
23
+ export interface RuntimeManifestMutationResult {
24
+ manifestPath: string;
25
+ manifest: LoadedRuntimeManifest;
26
+ changedFields: string[];
27
+ }
28
+ export interface RuntimeManifestSummary {
29
+ manifestPath: string;
30
+ runtime: {
31
+ name: string;
32
+ version: string;
33
+ hagicodeInstance?: string;
34
+ };
35
+ paths: RuntimeManifestPathUpdates & {
36
+ runtimeRoot: string;
37
+ runtimeHome: string;
38
+ runtimeDataRoot: string;
39
+ serverProgramRoot?: string;
40
+ serverDataRoot?: string;
41
+ npmPrefix?: string;
42
+ };
43
+ serverActiveVersion?: string;
44
+ components: string[];
45
+ npmPackages: Array<{
46
+ packageName: string;
47
+ version?: string;
48
+ target?: string;
49
+ }>;
50
+ }
51
+ export declare class RuntimeManifestMutationError extends Error {
52
+ constructor(message: string);
53
+ }
54
+ export declare function initRuntimeManifest(options?: InitRuntimeManifestOptions): Promise<RuntimeManifestMutationResult>;
55
+ export declare function updateRuntimeManifest(options: RuntimeManifestMutationOptions): Promise<RuntimeManifestMutationResult>;
56
+ export declare function readRuntimeManifestSummary(manifestPath?: string): Promise<RuntimeManifestSummary>;
57
+ export declare function renderRuntimeManifestSummaryText(summary: RuntimeManifestSummary): string;
@@ -0,0 +1,188 @@
1
+ import { access, mkdir, readFile, writeFile } from "node:fs/promises";
2
+ import { dirname, resolve } from "node:path";
3
+ import { parse, stringify } from "yaml";
4
+ import { getDefaultRuntimeManifestPath, loadRuntimeManifest } from "./runtime-manifest.js";
5
+ const DEFAULT_INIT_MANIFEST_PATH = "hagiscript.manifest.yaml";
6
+ export class RuntimeManifestMutationError extends Error {
7
+ constructor(message) {
8
+ super(message);
9
+ this.name = "RuntimeManifestMutationError";
10
+ }
11
+ }
12
+ export async function initRuntimeManifest(options = { manifestPath: DEFAULT_INIT_MANIFEST_PATH }) {
13
+ const manifestPath = resolve(options.manifestPath || DEFAULT_INIT_MANIFEST_PATH);
14
+ if (!(options.force ?? false) && (await fileExists(manifestPath))) {
15
+ throw new RuntimeManifestMutationError(`Manifest already exists: ${manifestPath}. Use --force to overwrite it.`);
16
+ }
17
+ const defaultManifestPath = getDefaultRuntimeManifestPath();
18
+ const manifestObject = await readRuntimeManifestObject(defaultManifestPath);
19
+ const changedFields = applyRuntimeManifestUpdates(manifestObject, {
20
+ pathUpdates: options.pathUpdates,
21
+ npmPackageUpdates: options.npmPackageUpdates,
22
+ serverActiveVersion: options.serverActiveVersion
23
+ });
24
+ await writeRuntimeManifestObject(manifestPath, manifestObject);
25
+ const manifest = await loadRuntimeManifest({ manifestPath });
26
+ return {
27
+ manifestPath,
28
+ manifest,
29
+ changedFields
30
+ };
31
+ }
32
+ export async function updateRuntimeManifest(options) {
33
+ const manifestPath = resolve(options.manifestPath);
34
+ const manifestObject = await readRuntimeManifestObject(manifestPath);
35
+ const changedFields = applyRuntimeManifestUpdates(manifestObject, options);
36
+ if (changedFields.length === 0) {
37
+ throw new RuntimeManifestMutationError("No manifest changes requested.");
38
+ }
39
+ await writeRuntimeManifestObject(manifestPath, manifestObject);
40
+ const manifest = await loadRuntimeManifest({ manifestPath });
41
+ return {
42
+ manifestPath,
43
+ manifest,
44
+ changedFields
45
+ };
46
+ }
47
+ export async function readRuntimeManifestSummary(manifestPath) {
48
+ const manifest = await loadRuntimeManifest({ manifestPath });
49
+ const serverComponent = manifest.componentMap.get("server");
50
+ const packages = readManifestNpmPackages(manifest.npmSync);
51
+ return {
52
+ manifestPath: manifest.manifestPath,
53
+ runtime: {
54
+ name: manifest.runtime.name,
55
+ version: manifest.runtime.version,
56
+ hagicodeInstance: manifest.runtime.hagicodeInstance
57
+ },
58
+ paths: {
59
+ runtimeRoot: manifest.paths.runtimeRoot,
60
+ runtimeHome: manifest.paths.runtimeHome,
61
+ runtimeDataRoot: manifest.paths.runtimeDataRoot,
62
+ serverProgramRoot: manifest.paths.serverProgramRoot,
63
+ serverDataRoot: manifest.paths.serverDataRoot,
64
+ npmPrefix: manifest.paths.npmPrefix
65
+ },
66
+ serverActiveVersion: serverComponent?.releasedService?.activeVersion,
67
+ components: manifest.components.map((component) => component.name),
68
+ npmPackages: packages
69
+ };
70
+ }
71
+ export function renderRuntimeManifestSummaryText(summary) {
72
+ return [
73
+ "Manifest.",
74
+ `Path: ${summary.manifestPath}`,
75
+ `Runtime: ${summary.runtime.name}@${summary.runtime.version}`,
76
+ `Instance: ${summary.runtime.hagicodeInstance ?? "(none)"}`,
77
+ "Paths:",
78
+ ` runtimeRoot: ${summary.paths.runtimeRoot}`,
79
+ ` runtimeHome: ${summary.paths.runtimeHome}`,
80
+ ` runtimeDataRoot: ${summary.paths.runtimeDataRoot}`,
81
+ ` serverProgramRoot: ${summary.paths.serverProgramRoot ?? "(default)"}`,
82
+ ` serverDataRoot: ${summary.paths.serverDataRoot ?? "(default)"}`,
83
+ ` npmPrefix: ${summary.paths.npmPrefix ?? "(default)"}`,
84
+ `Server active version: ${summary.serverActiveVersion ?? "(none)"}`,
85
+ `Components: ${summary.components.join(", ") || "(none)"}`,
86
+ ...(summary.npmPackages.length === 0
87
+ ? ["Managed npm packages: (none)"]
88
+ : [
89
+ "Managed npm packages:",
90
+ ...summary.npmPackages.map((entry) => ` - ${entry.packageName} version=${entry.version ?? "(unspecified)"} target=${entry.target ?? entry.version ?? "(unspecified)"}`)
91
+ ])
92
+ ].join("\n");
93
+ }
94
+ async function readRuntimeManifestObject(manifestPath) {
95
+ let parsed;
96
+ try {
97
+ parsed = parse(await readFile(manifestPath, "utf8"));
98
+ }
99
+ catch (error) {
100
+ const message = error instanceof Error ? error.message : String(error);
101
+ throw new RuntimeManifestMutationError(`Failed to read manifest ${manifestPath}: ${message}`);
102
+ }
103
+ if (!isRecord(parsed)) {
104
+ throw new RuntimeManifestMutationError(`Manifest ${manifestPath} must be a YAML object.`);
105
+ }
106
+ return parsed;
107
+ }
108
+ async function writeRuntimeManifestObject(manifestPath, manifestObject) {
109
+ await mkdir(dirname(manifestPath), { recursive: true });
110
+ await writeFile(manifestPath, stringify(manifestObject), "utf8");
111
+ }
112
+ function applyRuntimeManifestUpdates(manifestObject, options) {
113
+ const changedFields = [];
114
+ const pathsObject = ensureObject(manifestObject, "paths");
115
+ for (const [key, value] of Object.entries(options.pathUpdates ?? {})) {
116
+ if (!value) {
117
+ continue;
118
+ }
119
+ pathsObject[key] = value;
120
+ changedFields.push(`paths.${key}`);
121
+ }
122
+ for (const update of options.npmPackageUpdates ?? []) {
123
+ const npmSyncObject = ensureObject(manifestObject, "npmSync");
124
+ const packagesObject = ensureObject(npmSyncObject, "packages");
125
+ const packageObject = ensureObject(packagesObject, update.packageName);
126
+ packageObject.version = update.version;
127
+ packageObject.target = update.target ?? update.version;
128
+ changedFields.push(`npmSync.packages.${update.packageName}`);
129
+ }
130
+ if (options.serverActiveVersion) {
131
+ const serverComponent = findNamedComponent(manifestObject, "server");
132
+ const releasedServiceObject = ensureObject(serverComponent, "releasedService");
133
+ releasedServiceObject.activeVersion = options.serverActiveVersion;
134
+ changedFields.push("components.server.releasedService.activeVersion");
135
+ }
136
+ return changedFields;
137
+ }
138
+ function findNamedComponent(manifestObject, componentName) {
139
+ const components = manifestObject.components;
140
+ if (!Array.isArray(components)) {
141
+ throw new RuntimeManifestMutationError("Manifest components must be an array.");
142
+ }
143
+ for (const component of components) {
144
+ if (isRecord(component) && component.name === componentName) {
145
+ return component;
146
+ }
147
+ }
148
+ throw new RuntimeManifestMutationError(`Manifest does not define a ${componentName} component.`);
149
+ }
150
+ function ensureObject(owner, key) {
151
+ const current = owner[key];
152
+ if (isRecord(current)) {
153
+ return current;
154
+ }
155
+ const next = {};
156
+ owner[key] = next;
157
+ return next;
158
+ }
159
+ function readManifestNpmPackages(npmSync) {
160
+ const packages = npmSync?.packages;
161
+ if (!isRecord(packages)) {
162
+ return [];
163
+ }
164
+ return Object.entries(packages)
165
+ .flatMap(([packageName, value]) => isRecord(value)
166
+ ? [
167
+ {
168
+ packageName,
169
+ version: typeof value.version === "string" ? value.version : undefined,
170
+ target: typeof value.target === "string" ? value.target : undefined
171
+ }
172
+ ]
173
+ : [])
174
+ .sort((left, right) => left.packageName.localeCompare(right.packageName));
175
+ }
176
+ async function fileExists(filePath) {
177
+ try {
178
+ await access(filePath);
179
+ return true;
180
+ }
181
+ catch {
182
+ return false;
183
+ }
184
+ }
185
+ function isRecord(value) {
186
+ return typeof value === "object" && value !== null && !Array.isArray(value);
187
+ }
188
+ //# sourceMappingURL=manifest-manager.js.map