|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { execFileSync } from "node:child_process"; |
| 4 | +import { readFileSync } from "node:fs"; |
| 5 | +import path from "node:path"; |
| 6 | +import { fileURLToPath } from "node:url"; |
| 7 | +import { extractCurrentPackageChangelog } from "./package-changelog.mjs"; |
| 8 | +import { validateBundledPackageDependencyAlignment } from "./package-source-dependencies.mjs"; |
| 9 | + |
| 10 | +const FULL_GIT_COMMIT_RE = /^[0-9a-f]{40}$/u; |
| 11 | +const ROOT_MANIFEST_PATH = "package.json"; |
| 12 | +const AI_MANIFEST_PATH = "packages/ai/package.json"; |
| 13 | +const CHANGELOG_PATH = "CHANGELOG.md"; |
| 14 | + |
| 15 | +function parseArgs(argv) { |
| 16 | + const options = { |
| 17 | + allowUnreleasedChangelog: false, |
| 18 | + ref: "", |
| 19 | + sourceDir: "", |
| 20 | + }; |
| 21 | + for (let index = 0; index < argv.length; index += 1) { |
| 22 | + const arg = argv[index]; |
| 23 | + if (arg === "--allow-unreleased-changelog") { |
| 24 | + options.allowUnreleasedChangelog = true; |
| 25 | + continue; |
| 26 | + } |
| 27 | + if (arg === "--ref") { |
| 28 | + options.ref = argv[index + 1] ?? ""; |
| 29 | + index += 1; |
| 30 | + continue; |
| 31 | + } |
| 32 | + if (arg === "--source-dir") { |
| 33 | + options.sourceDir = argv[index + 1] ?? ""; |
| 34 | + index += 1; |
| 35 | + continue; |
| 36 | + } |
| 37 | + throw new Error(`Unknown package source preflight option: ${arg}`); |
| 38 | + } |
| 39 | + if (options.ref && options.sourceDir) { |
| 40 | + throw new Error("Use exactly one of --ref or --source-dir."); |
| 41 | + } |
| 42 | + if (options.ref && !FULL_GIT_COMMIT_RE.test(options.ref)) { |
| 43 | + throw new Error(`--ref must be a full lowercase commit SHA; got ${options.ref}`); |
| 44 | + } |
| 45 | + if (!options.ref && !options.sourceDir) { |
| 46 | + throw new Error("Use exactly one of --ref or --source-dir."); |
| 47 | + } |
| 48 | + return options; |
| 49 | +} |
| 50 | + |
| 51 | +function parseManifest(content, manifestPath) { |
| 52 | + try { |
| 53 | + return JSON.parse(content); |
| 54 | + } catch (error) { |
| 55 | + throw new Error(`Failed to parse ${manifestPath}.`, { cause: error }); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +export function validatePackageSource({ |
| 60 | + aiManifestContent, |
| 61 | + allowUnreleasedChangelog = false, |
| 62 | + changelogContent, |
| 63 | + rootManifestContent, |
| 64 | +}) { |
| 65 | + const rootManifest = parseManifest(rootManifestContent, ROOT_MANIFEST_PATH); |
| 66 | + if (typeof rootManifest.version !== "string") { |
| 67 | + throw new Error(`${ROOT_MANIFEST_PATH} version must be a string.`); |
| 68 | + } |
| 69 | + extractCurrentPackageChangelog(changelogContent, rootManifest.version, { |
| 70 | + allowUnreleased: allowUnreleasedChangelog, |
| 71 | + }); |
| 72 | + |
| 73 | + const rootDependencies = rootManifest.dependencies ?? {}; |
| 74 | + const aiDependency = rootDependencies["@openclaw/ai"]; |
| 75 | + if (aiManifestContent === null) { |
| 76 | + if (aiDependency === undefined) { |
| 77 | + return rootManifest.version; |
| 78 | + } |
| 79 | + throw new Error(`${ROOT_MANIFEST_PATH} declares @openclaw/ai without ${AI_MANIFEST_PATH}.`); |
| 80 | + } |
| 81 | + |
| 82 | + const aiManifest = parseManifest(aiManifestContent, AI_MANIFEST_PATH); |
| 83 | + if (aiDependency !== "workspace:*") { |
| 84 | + throw new Error( |
| 85 | + `${ROOT_MANIFEST_PATH} must depend on @openclaw/ai via workspace:*; found ${JSON.stringify(aiDependency)}.`, |
| 86 | + ); |
| 87 | + } |
| 88 | + if (aiManifest.version !== rootManifest.version) { |
| 89 | + throw new Error( |
| 90 | + `${AI_MANIFEST_PATH} version must match ${ROOT_MANIFEST_PATH}: expected ${rootManifest.version}, found ${String(aiManifest.version ?? "<missing>")}.`, |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + validateBundledPackageDependencyAlignment({ |
| 95 | + bundledDependencies: aiManifest.dependencies, |
| 96 | + bundledPackageLabel: AI_MANIFEST_PATH, |
| 97 | + rootDependencies, |
| 98 | + rootPackageLabel: ROOT_MANIFEST_PATH, |
| 99 | + }); |
| 100 | + return rootManifest.version; |
| 101 | +} |
| 102 | + |
| 103 | +function readGitFile(ref, file, { optional = false } = {}) { |
| 104 | + try { |
| 105 | + return execFileSync("git", ["show", `${ref}:${file}`], { |
| 106 | + encoding: "utf8", |
| 107 | + maxBuffer: 16 * 1024 * 1024, |
| 108 | + timeout: 30_000, |
| 109 | + }); |
| 110 | + } catch (error) { |
| 111 | + if (optional && error?.status === 128) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + throw new Error(`Unable to read ${file} from ${ref}.`, { cause: error }); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +export function validatePackageSourceRef(ref, options = {}) { |
| 119 | + return validatePackageSource({ |
| 120 | + aiManifestContent: readGitFile(ref, AI_MANIFEST_PATH, { optional: true }), |
| 121 | + allowUnreleasedChangelog: options.allowUnreleasedChangelog, |
| 122 | + changelogContent: readGitFile(ref, CHANGELOG_PATH), |
| 123 | + rootManifestContent: readGitFile(ref, ROOT_MANIFEST_PATH), |
| 124 | + }); |
| 125 | +} |
| 126 | + |
| 127 | +function readSourceFile(sourceDir, file, { optional = false } = {}) { |
| 128 | + try { |
| 129 | + return readFileSync(path.join(sourceDir, file), "utf8"); |
| 130 | + } catch (error) { |
| 131 | + if (optional && error?.code === "ENOENT") { |
| 132 | + return null; |
| 133 | + } |
| 134 | + throw new Error(`Unable to read ${file} from ${sourceDir}.`, { cause: error }); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +export function validatePackageSourceDir(sourceDir, options = {}) { |
| 139 | + const resolvedSourceDir = path.resolve(sourceDir); |
| 140 | + return validatePackageSource({ |
| 141 | + aiManifestContent: readSourceFile(resolvedSourceDir, AI_MANIFEST_PATH, { optional: true }), |
| 142 | + allowUnreleasedChangelog: options.allowUnreleasedChangelog, |
| 143 | + changelogContent: readSourceFile(resolvedSourceDir, CHANGELOG_PATH), |
| 144 | + rootManifestContent: readSourceFile(resolvedSourceDir, ROOT_MANIFEST_PATH), |
| 145 | + }); |
| 146 | +} |
| 147 | + |
| 148 | +function main(argv = process.argv.slice(2)) { |
| 149 | + const options = parseArgs(argv); |
| 150 | + const version = options.ref |
| 151 | + ? validatePackageSourceRef(options.ref, options) |
| 152 | + : validatePackageSourceDir(options.sourceDir, options); |
| 153 | + console.log(`package-source-preflight: source manifests and changelog are valid (${version}).`); |
| 154 | +} |
| 155 | + |
| 156 | +if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { |
| 157 | + try { |
| 158 | + main(); |
| 159 | + } catch (error) { |
| 160 | + console.error(error instanceof Error ? error.message : String(error)); |
| 161 | + process.exitCode = 1; |
| 162 | + } |
| 163 | +} |
0 commit comments