|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { sortUniqueStrings } from "@openclaw/normalization-core/string-normalization"; |
| 4 | +import { writeJson } from "../../src/infra/json-files.ts"; |
| 5 | +import { |
| 6 | + collectPackageDistInventory, |
| 7 | + PACKAGE_DIST_INVENTORY_RELATIVE_PATH, |
| 8 | +} from "../../src/infra/package-dist-inventory.ts"; |
| 9 | + |
| 10 | +export { LOCAL_BUILD_METADATA_DIST_PATHS } from "./local-build-metadata-paths.mjs"; |
| 11 | +export { PACKAGE_DIST_INVENTORY_RELATIVE_PATH }; |
| 12 | + |
| 13 | +const INSTALL_STAGE_DEBRIS_DIR_PATTERN = /^\.openclaw-install-stage(?:-[^/]+)?$/iu; |
| 14 | + |
| 15 | +function normalizeRelativePath(value: string): string { |
| 16 | + return value.replace(/\\/g, "/"); |
| 17 | +} |
| 18 | + |
| 19 | +function isInstallStageDirName(value: string): boolean { |
| 20 | + return INSTALL_STAGE_DEBRIS_DIR_PATTERN.test(value); |
| 21 | +} |
| 22 | + |
| 23 | +export function isLegacyPluginDependencyInstallStagePath(relativePath: string): boolean { |
| 24 | + const parts = normalizeRelativePath(relativePath).split("/"); |
| 25 | + return ( |
| 26 | + parts.length >= 4 && |
| 27 | + parts[0]?.toLowerCase() === "dist" && |
| 28 | + parts[1]?.toLowerCase() === "extensions" && |
| 29 | + Boolean(parts[2]) && |
| 30 | + isInstallStageDirName(parts[3] ?? "") |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +async function collectLegacyPluginDependencyStagingDebrisPaths( |
| 35 | + packageRoot: string, |
| 36 | +): Promise<string[]> { |
| 37 | + const distDirs: string[] = []; |
| 38 | + try { |
| 39 | + const packageRootEntries = await fs.readdir(packageRoot, { withFileTypes: true }); |
| 40 | + for (const entry of packageRootEntries) { |
| 41 | + if (entry.isDirectory() && entry.name.toLowerCase() === "dist") { |
| 42 | + distDirs.push(path.join(packageRoot, entry.name)); |
| 43 | + } |
| 44 | + } |
| 45 | + } catch (error) { |
| 46 | + if ((error as NodeJS.ErrnoException).code === "ENOENT") { |
| 47 | + return []; |
| 48 | + } |
| 49 | + throw error; |
| 50 | + } |
| 51 | + |
| 52 | + const debris: string[] = []; |
| 53 | + for (const distDir of distDirs) { |
| 54 | + let distEntries: import("node:fs").Dirent[]; |
| 55 | + try { |
| 56 | + distEntries = await fs.readdir(distDir, { withFileTypes: true }); |
| 57 | + } catch (error) { |
| 58 | + if ((error as NodeJS.ErrnoException).code === "ENOENT") { |
| 59 | + continue; |
| 60 | + } |
| 61 | + throw error; |
| 62 | + } |
| 63 | + |
| 64 | + for (const distEntry of distEntries) { |
| 65 | + if (!distEntry.isDirectory() || distEntry.name.toLowerCase() !== "extensions") { |
| 66 | + continue; |
| 67 | + } |
| 68 | + const extensionsDir = path.join(distDir, distEntry.name); |
| 69 | + let extensionEntries: import("node:fs").Dirent[]; |
| 70 | + try { |
| 71 | + extensionEntries = await fs.readdir(extensionsDir, { withFileTypes: true }); |
| 72 | + } catch (error) { |
| 73 | + if ((error as NodeJS.ErrnoException).code === "ENOENT") { |
| 74 | + continue; |
| 75 | + } |
| 76 | + throw error; |
| 77 | + } |
| 78 | + |
| 79 | + for (const extensionEntry of extensionEntries) { |
| 80 | + if (!extensionEntry.isDirectory()) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + const extensionPath = path.join(extensionsDir, extensionEntry.name); |
| 84 | + let stagingEntries: import("node:fs").Dirent[]; |
| 85 | + try { |
| 86 | + stagingEntries = await fs.readdir(extensionPath, { withFileTypes: true }); |
| 87 | + } catch (error) { |
| 88 | + if ((error as NodeJS.ErrnoException).code === "ENOENT") { |
| 89 | + continue; |
| 90 | + } |
| 91 | + throw error; |
| 92 | + } |
| 93 | + for (const stagingEntry of stagingEntries) { |
| 94 | + if (!isInstallStageDirName(stagingEntry.name)) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + debris.push( |
| 98 | + normalizeRelativePath( |
| 99 | + path.relative(packageRoot, path.join(extensionPath, stagingEntry.name)), |
| 100 | + ), |
| 101 | + ); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return debris.toSorted((left, right) => left.localeCompare(right)); |
| 107 | +} |
| 108 | + |
| 109 | +async function assertNoLegacyPluginDependencyStagingDebris(packageRoot: string): Promise<void> { |
| 110 | + const debris = await collectLegacyPluginDependencyStagingDebrisPaths(packageRoot); |
| 111 | + if (debris.length === 0) { |
| 112 | + return; |
| 113 | + } |
| 114 | + throw new Error( |
| 115 | + `unexpected legacy plugin dependency staging debris in package dist: ${debris.join(", ")}`, |
| 116 | + ); |
| 117 | +} |
| 118 | + |
| 119 | +export async function writePackageDistInventory(packageRoot: string): Promise<string[]> { |
| 120 | + await assertNoLegacyPluginDependencyStagingDebris(packageRoot); |
| 121 | + const inventory = sortUniqueStrings(await collectPackageDistInventory(packageRoot)); |
| 122 | + const inventoryPath = path.join(packageRoot, PACKAGE_DIST_INVENTORY_RELATIVE_PATH); |
| 123 | + await writeJson(inventoryPath, inventory, { trailingNewline: true }); |
| 124 | + return inventory; |
| 125 | +} |
0 commit comments