|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { readdirSync, readFileSync } from "node:fs"; |
| 4 | +import { join, relative } from "node:path"; |
| 5 | +import { spawnSync } from "node:child_process"; |
| 6 | +import { fileURLToPath } from "node:url"; |
| 7 | + |
| 8 | +const __dirname = fileURLToPath(new URL(".", import.meta.url)); |
| 9 | +const repoRoot = join(__dirname, ".."); |
| 10 | +const packagesRoot = join(repoRoot, "packages"); |
| 11 | + |
| 12 | +const sourceTag = process.argv[2] || "alpha"; |
| 13 | +const targetTag = process.argv[3] || "next"; |
| 14 | + |
| 15 | +function walkPackageJsonFiles(dir, results = []) { |
| 16 | + for (const entry of readdirSync(dir, { withFileTypes: true })) { |
| 17 | + if ( |
| 18 | + entry.name === "node_modules" || |
| 19 | + entry.name === "dist" || |
| 20 | + entry.name === "target" || |
| 21 | + entry.name === ".turbo" |
| 22 | + ) { |
| 23 | + continue; |
| 24 | + } |
| 25 | + |
| 26 | + const fullPath = join(dir, entry.name); |
| 27 | + |
| 28 | + if (entry.isDirectory()) { |
| 29 | + walkPackageJsonFiles(fullPath, results); |
| 30 | + continue; |
| 31 | + } |
| 32 | + |
| 33 | + if (entry.isFile() && entry.name === "package.json") { |
| 34 | + results.push(fullPath); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return results; |
| 39 | +} |
| 40 | + |
| 41 | +function runNpm(args, { allowFailure = false } = {}) { |
| 42 | + const result = spawnSync("npm", args, { |
| 43 | + cwd: repoRoot, |
| 44 | + encoding: "utf8", |
| 45 | + }); |
| 46 | + |
| 47 | + if (result.status === 0) { |
| 48 | + return result.stdout.trim(); |
| 49 | + } |
| 50 | + |
| 51 | + if (allowFailure) { |
| 52 | + return ""; |
| 53 | + } |
| 54 | + |
| 55 | + const stderr = result.stderr.trim(); |
| 56 | + throw new Error(stderr || `npm ${args.join(" ")} failed`); |
| 57 | +} |
| 58 | + |
| 59 | +function collectManagedPackages() { |
| 60 | + const names = new Map(); |
| 61 | + |
| 62 | + for (const packageJsonPath of walkPackageJsonFiles(packagesRoot)) { |
| 63 | + const raw = readFileSync(packageJsonPath, "utf8"); |
| 64 | + const pkg = JSON.parse(raw); |
| 65 | + |
| 66 | + if (pkg.private === true || typeof pkg.name !== "string") { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + if (!pkg.name.startsWith("@elizaos/")) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + names.set(pkg.name, relative(repoRoot, packageJsonPath)); |
| 75 | + } |
| 76 | + |
| 77 | + return [...names.entries()].sort(([a], [b]) => a.localeCompare(b)); |
| 78 | +} |
| 79 | + |
| 80 | +const packages = collectManagedPackages(); |
| 81 | +let updated = 0; |
| 82 | + |
| 83 | +console.log( |
| 84 | + `Syncing dist-tags for ${packages.length} package(s): ${sourceTag} -> ${targetTag}`, |
| 85 | +); |
| 86 | + |
| 87 | +for (const [packageName, packageJsonPath] of packages) { |
| 88 | + const rawTags = runNpm(["view", packageName, "dist-tags", "--json"], { |
| 89 | + allowFailure: true, |
| 90 | + }); |
| 91 | + |
| 92 | + if (!rawTags) { |
| 93 | + console.log(`- skip ${packageName} (${packageJsonPath}): no dist-tags found`); |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + let tags; |
| 98 | + try { |
| 99 | + tags = JSON.parse(rawTags); |
| 100 | + } catch (_error) { |
| 101 | + console.log(`- skip ${packageName} (${packageJsonPath}): invalid dist-tags`); |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + const sourceVersion = tags[sourceTag]; |
| 106 | + if (!sourceVersion) { |
| 107 | + console.log(`- skip ${packageName} (${packageJsonPath}): no ${sourceTag} tag`); |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + if (tags[targetTag] === sourceVersion) { |
| 112 | + console.log(`- ok ${packageName}: ${targetTag} already ${sourceVersion}`); |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + runNpm(["dist-tag", "add", `${packageName}@${sourceVersion}`, targetTag]); |
| 117 | + updated += 1; |
| 118 | + console.log( |
| 119 | + `- set ${packageName}: ${targetTag} -> ${sourceVersion} (from ${sourceTag})`, |
| 120 | + ); |
| 121 | +} |
| 122 | + |
| 123 | +console.log(`Done. Updated ${updated} package(s).`); |
0 commit comments