#!/usr/bin/env node import { existsSync, readdirSync, readFileSync, statSync } from "node:fs"; import { join, posix } from "node:path"; const DOCKERFILES = ["Dockerfile", "apps/upload/Dockerfile"]; const WORKSPACE_YAML = "pnpm-workspace.yaml"; function parseImporterGlobs(yaml) { const globs = []; let inPackages = false; for (const line of yaml.split("\n")) { if (/^packages:\s*$/.test(line)) { inPackages = true; continue; } if (!inPackages) continue; const item = line.match(/^\s+-\s+(.+?)\s*(?:#.*)?$/); if (item) { globs.push(item[1].replace(/^['"]|['"]$/g, "")); continue; } if (line.trim() !== "" && !line.startsWith("#")) break; } return globs; } function resolveImporters(globs) { const importers = new Set(); const excluded = new Set(); for (const glob of globs) { if (glob.startsWith("!")) { excluded.add(glob.slice(1)); continue; } if (glob.endsWith("/*")) { const base = glob.slice(0, -2); if (!existsSync(base)) { console.error(`✗ ${WORKSPACE_YAML} glob "${glob}" matches no directory`); process.exitCode = 1; continue; } for (const entry of readdirSync(base).sort()) { const dir = posix.join(base, entry); if (statSync(dir).isDirectory() && existsSync(join(dir, "package.json"))) { importers.add(dir); } } continue; } if (existsSync(join(glob, "package.json"))) importers.add(glob); } for (const entry of excluded) importers.delete(entry); return importers; } function copiedImporterDirs(dockerfile) { const copied = new Set(); for (const line of readFileSync(dockerfile, "utf8").split("\n")) { const match = line.match(/^COPY\s+(?:--\S+\s+)*(\S+)\s+\S+\s*$/); if (!match) continue; const source = match[1]; if (!source.endsWith("/package.json")) continue; const dir = source.slice(0, -"/package.json".length); if (dir.startsWith("/") || dir.includes("*")) continue; copied.add(dir); } return copied; } let failing = false; const importers = resolveImporters(parseImporterGlobs(readFileSync(WORKSPACE_YAML, "utf8"))); for (const dockerfile of DOCKERFILES) { if (!existsSync(dockerfile)) { console.error(`✗ ${dockerfile} missing`); failing = true; continue; } const copied = copiedImporterDirs(dockerfile); const missing = [...importers].filter((dir) => !copied.has(dir)); const extra = [...copied].filter((dir) => !importers.has(dir)); for (const dir of missing) { console.error( `✗ ${dockerfile}: no "COPY ${dir}/package.json" — the frozen install fails at image build (importer of ${WORKSPACE_YAML})`, ); failing = true; } for (const dir of extra) { console.log( `- ${dockerfile}: COPY for "${dir}" is not a workspace importer (harmless — the incremental pass re-links workspaces)`, ); } } if (failing) { console.error( `\nAdd the missing COPY lines above both install passes, keeping the lists in sync with ${WORKSPACE_YAML}.`, ); process.exitCode = 1; } else { console.log( `dockerfile importer guard holds — ${DOCKERFILES.join(" + ")} copy all ${importers.size} lockfile importers`, ); }