|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import yaml from "js-yaml"; |
| 4 | + |
| 5 | +const rootDir = path.resolve(import.meta.dirname, ".."); |
| 6 | + |
| 7 | +// Read the root package.json version |
| 8 | +const rootPackagePath = path.join(rootDir, "package.json"); |
| 9 | +const rootPackage = JSON.parse(fs.readFileSync(rootPackagePath, "utf-8")) as { version: string }; |
| 10 | +const { version } = rootPackage; |
| 11 | + |
| 12 | +console.log(`Syncing version: ${version}`); |
| 13 | + |
| 14 | +// Parse pnpm-workspace.yaml to get workspace globs |
| 15 | +const workspaceYamlPath = path.join(rootDir, "pnpm-workspace.yaml"); |
| 16 | +const workspaceConfig = yaml.load(fs.readFileSync(workspaceYamlPath, "utf-8")) as { packages?: string[] }; |
| 17 | +const globs: string[] = workspaceConfig.packages ?? []; |
| 18 | + |
| 19 | +// Ensure a resolved path is within the project root to prevent path traversal |
| 20 | +function safePath(unsafePath: string): string | undefined { |
| 21 | + const resolved = path.resolve(rootDir, unsafePath); |
| 22 | + if (!resolved.startsWith(rootDir + path.sep) && resolved !== rootDir) { |
| 23 | + console.warn(` Skipping path outside project root: ${unsafePath}`); |
| 24 | + return undefined; |
| 25 | + } |
| 26 | + |
| 27 | + return resolved; |
| 28 | +} |
| 29 | + |
| 30 | +// Resolve globs to actual package directories |
| 31 | +const packageDirs: string[] = []; |
| 32 | +for (const glob of globs) { |
| 33 | + if (glob.endsWith("/*")) { |
| 34 | + // Wildcard glob — enumerate subdirectories |
| 35 | + const parentDir = safePath(glob.replace("/*", "")); |
| 36 | + if (parentDir && fs.existsSync(parentDir)) { |
| 37 | + for (const entry of fs.readdirSync(parentDir, { withFileTypes: true })) { |
| 38 | + if (entry.isDirectory()) { |
| 39 | + const dir = path.join(parentDir, entry.name); |
| 40 | + if (safePath(path.relative(rootDir, dir))) { |
| 41 | + packageDirs.push(dir); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } else { |
| 47 | + // Exact path |
| 48 | + const dir = safePath(glob); |
| 49 | + if (dir && fs.existsSync(dir)) { |
| 50 | + packageDirs.push(dir); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Update each package.json |
| 56 | +let updated = 0; |
| 57 | +let skipped = 0; |
| 58 | +for (const dir of packageDirs) { |
| 59 | + const pkgPath = path.join(dir, "package.json"); |
| 60 | + if (!fs.existsSync(pkgPath)) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + const raw = fs.readFileSync(pkgPath, "utf-8"); |
| 65 | + const pkg = JSON.parse(raw) as { name: string; version: string }; |
| 66 | + |
| 67 | + if (pkg.version === version) { |
| 68 | + skipped++; |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + const oldVersion = pkg.version; |
| 73 | + pkg.version = version; |
| 74 | + |
| 75 | + // Preserve original formatting (detect indent) |
| 76 | + const indent = raw.match(/^(\s+)"/m)?.[1] ?? "\t"; |
| 77 | + fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, indent) + "\n"); |
| 78 | + |
| 79 | + console.log(` ${pkg.name}: ${oldVersion} → ${version}`); |
| 80 | + updated++; |
| 81 | +} |
| 82 | + |
| 83 | +console.log(`\nDone: ${updated} updated, ${skipped} skipped (version ${version})`); |
0 commit comments