|
| 1 | +import { spawn } from "node:child_process"; |
| 2 | +import { readFile, writeFile } from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | +import { computeFingerprint } from "./postinstall-cache.mjs"; |
| 6 | +import { exists } from "./utils.mjs"; |
| 7 | + |
| 8 | +const runtimeDir = path.dirname(fileURLToPath(import.meta.url)); |
| 9 | +const nodeModulesDir = path.join(runtimeDir, "node_modules"); |
| 10 | +const cacheFileName = ".postinstall-cache.json"; |
| 11 | +const cacheFilePath = path.join(runtimeDir, cacheFileName); |
| 12 | +const lockfilePath = path.join(runtimeDir, "package-lock.json"); |
| 13 | + |
| 14 | +async function readCachedFingerprint() { |
| 15 | + if (!(await exists(cacheFilePath))) { |
| 16 | + return null; |
| 17 | + } |
| 18 | + |
| 19 | + try { |
| 20 | + const content = await readFile(cacheFilePath, "utf8"); |
| 21 | + const parsed = JSON.parse(content); |
| 22 | + return typeof parsed.fingerprint === "string" ? parsed.fingerprint : null; |
| 23 | + } catch { |
| 24 | + return null; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +async function run(command, args) { |
| 29 | + await new Promise((resolve, reject) => { |
| 30 | + const child = spawn(command, args, { |
| 31 | + cwd: runtimeDir, |
| 32 | + stdio: "inherit", |
| 33 | + }); |
| 34 | + |
| 35 | + child.on("error", reject); |
| 36 | + child.on("exit", (code) => { |
| 37 | + if (code === 0) { |
| 38 | + resolve(); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + reject( |
| 43 | + new Error( |
| 44 | + `${command} ${args.join(" ")} exited with code ${code ?? "unknown"}`, |
| 45 | + ), |
| 46 | + ); |
| 47 | + }); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +async function installRuntime() { |
| 52 | + const npmCommand = process.platform === "win32" ? "npm.cmd" : "npm"; |
| 53 | + |
| 54 | + if (await exists(lockfilePath)) { |
| 55 | + try { |
| 56 | + await run(npmCommand, ["ci", "--no-audit", "--no-fund"]); |
| 57 | + return; |
| 58 | + } catch (error) { |
| 59 | + console.warn( |
| 60 | + "openclaw-runtime npm ci failed, falling back to npm install --prefer-offline.", |
| 61 | + ); |
| 62 | + console.warn(error instanceof Error ? error.message : String(error)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + await run(npmCommand, [ |
| 67 | + "install", |
| 68 | + "--no-audit", |
| 69 | + "--no-fund", |
| 70 | + "--prefer-offline", |
| 71 | + ]); |
| 72 | +} |
| 73 | + |
| 74 | +try { |
| 75 | + const fingerprint = await computeFingerprint(runtimeDir); |
| 76 | + const cachedFingerprint = await readCachedFingerprint(); |
| 77 | + const hasNodeModules = await exists(nodeModulesDir); |
| 78 | + |
| 79 | + if (hasNodeModules && cachedFingerprint === fingerprint) { |
| 80 | + console.log("openclaw-runtime unchanged, skipping install:pruned."); |
| 81 | + process.exit(0); |
| 82 | + } |
| 83 | + |
| 84 | + if (!hasNodeModules) { |
| 85 | + console.log( |
| 86 | + "openclaw-runtime node_modules missing, running install:pruned.", |
| 87 | + ); |
| 88 | + } else if (cachedFingerprint === null) { |
| 89 | + console.log("openclaw-runtime cache missing, running install:pruned."); |
| 90 | + } else { |
| 91 | + console.log("openclaw-runtime inputs changed, running install:pruned."); |
| 92 | + } |
| 93 | + |
| 94 | + await installRuntime(); |
| 95 | + await run(process.execPath, ["./prune-runtime.mjs"]); |
| 96 | + |
| 97 | + await writeFile( |
| 98 | + cacheFilePath, |
| 99 | + `${JSON.stringify( |
| 100 | + { |
| 101 | + fingerprint, |
| 102 | + updatedAt: new Date().toISOString(), |
| 103 | + }, |
| 104 | + null, |
| 105 | + 2, |
| 106 | + )}\n`, |
| 107 | + "utf8", |
| 108 | + ); |
| 109 | + |
| 110 | + console.log("openclaw-runtime cache updated."); |
| 111 | +} catch (error) { |
| 112 | + console.error("openclaw-runtime postinstall failed."); |
| 113 | + throw error; |
| 114 | +} |
0 commit comments