|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { spawnSync } from "node:child_process"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | + |
| 6 | +export interface WorkspacePackage { |
| 7 | + name: string; |
| 8 | + dir: string; |
| 9 | + scripts: string[]; |
| 10 | +} |
| 11 | + |
| 12 | +interface CheckWorkspaceArgs { |
| 13 | + includeBuild: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +function readPackageJson(packageJsonPath: string): { scripts?: Record<string, string> } | null { |
| 17 | + try { |
| 18 | + return JSON.parse(fs.readFileSync(packageJsonPath, "utf8")) as { scripts?: Record<string, string> }; |
| 19 | + } catch { |
| 20 | + return null; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function collectWorkspacePackageDirs(repoRoot: string): string[] { |
| 25 | + const dirs: string[] = []; |
| 26 | + |
| 27 | + const rootApp = path.join(repoRoot, "app"); |
| 28 | + if (fs.existsSync(path.join(rootApp, "package.json"))) { |
| 29 | + dirs.push(rootApp); |
| 30 | + } |
| 31 | + |
| 32 | + for (const section of ["apps", "packages"]) { |
| 33 | + const sectionDir = path.join(repoRoot, section); |
| 34 | + if (!fs.existsSync(sectionDir)) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + const entries = fs.readdirSync(sectionDir, { withFileTypes: true }); |
| 39 | + for (const entry of entries) { |
| 40 | + if (!entry.isDirectory()) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + const packageDir = path.join(sectionDir, entry.name); |
| 45 | + if (fs.existsSync(path.join(packageDir, "package.json"))) { |
| 46 | + dirs.push(packageDir); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return dirs.sort(); |
| 52 | +} |
| 53 | + |
| 54 | +export function discoverWorkspacePackages(repoRoot: string): WorkspacePackage[] { |
| 55 | + const dirs = collectWorkspacePackageDirs(repoRoot); |
| 56 | + |
| 57 | + return dirs |
| 58 | + .map((dir) => { |
| 59 | + const pkg = readPackageJson(path.join(dir, "package.json")); |
| 60 | + if (!pkg) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + const scripts = Object.keys(pkg.scripts ?? {}); |
| 65 | + return { |
| 66 | + name: path.relative(repoRoot, dir), |
| 67 | + dir, |
| 68 | + scripts |
| 69 | + }; |
| 70 | + }) |
| 71 | + .filter((pkg): pkg is WorkspacePackage => pkg !== null); |
| 72 | +} |
| 73 | + |
| 74 | +export function parseCheckWorkspaceArgs(argv: string[]): CheckWorkspaceArgs { |
| 75 | + return { |
| 76 | + includeBuild: argv.includes("--include-build") |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +function runScript(pkg: WorkspacePackage, script: string): number { |
| 81 | + const result = spawnSync("pnpm", ["--dir", pkg.dir, "run", script], { |
| 82 | + stdio: "inherit" |
| 83 | + }); |
| 84 | + return result.status ?? 1; |
| 85 | +} |
| 86 | + |
| 87 | +export function runCheckWorkspace(repoRoot: string, args: CheckWorkspaceArgs): number { |
| 88 | + const packages = discoverWorkspacePackages(repoRoot); |
| 89 | + if (packages.length === 0) { |
| 90 | + console.log("check:workspace: no JS workspace packages found under app/, apps/*, or packages/*"); |
| 91 | + return 0; |
| 92 | + } |
| 93 | + |
| 94 | + const scriptOrder = ["lint", "typecheck", "test", ...(args.includeBuild ? ["build"] : [])]; |
| 95 | + let failures = 0; |
| 96 | + |
| 97 | + for (const pkg of packages) { |
| 98 | + console.log(`check:workspace: ${pkg.name}`); |
| 99 | + for (const script of scriptOrder) { |
| 100 | + if (!pkg.scripts.includes(script)) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + const code = runScript(pkg, script); |
| 105 | + if (code !== 0) { |
| 106 | + console.error(`check:workspace: FAIL ${pkg.name} script '${script}' exited with ${code}`); |
| 107 | + failures += 1; |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (failures > 0) { |
| 114 | + return 1; |
| 115 | + } |
| 116 | + |
| 117 | + console.log("check:workspace: PASS"); |
| 118 | + return 0; |
| 119 | +} |
| 120 | + |
| 121 | +const isMain = process.argv[1] && fileURLToPath(import.meta.url) === path.resolve(process.argv[1]); |
| 122 | + |
| 123 | +if (isMain) { |
| 124 | + const args = parseCheckWorkspaceArgs(process.argv.slice(2)); |
| 125 | + const code = runCheckWorkspace(process.cwd(), args); |
| 126 | + process.exit(code); |
| 127 | +} |
| 128 | + |
0 commit comments