|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import { readFileSync } from "node:fs"; |
| 3 | + |
| 4 | +import { parseArgs, readPackage, repoPath } from "./_shared.mjs"; |
| 5 | + |
| 6 | +const args = parseArgs(); |
| 7 | +const manifestPath = args.manifest ?? ".release-manifest.json"; |
| 8 | +const npmTag = args.tag; |
| 9 | + |
| 10 | +const manifest = JSON.parse(readFileSync(repoPath(manifestPath), "utf8")); |
| 11 | +const packages = orderPackagesForPublish(manifest.packages ?? []); |
| 12 | + |
| 13 | +if (packages.length === 0) { |
| 14 | + console.log(`No packages to publish from ${manifestPath}.`); |
| 15 | + process.exit(0); |
| 16 | +} |
| 17 | + |
| 18 | +for (const pkg of packages) { |
| 19 | + const packageDir = repoPath(pkg.dir); |
| 20 | + const packageJson = readPackage(pkg.dir); |
| 21 | + const publishArgs = ["publish"]; |
| 22 | + |
| 23 | + if (packageJson.publishConfig?.access) { |
| 24 | + publishArgs.push("--access", packageJson.publishConfig.access); |
| 25 | + } |
| 26 | + |
| 27 | + if (npmTag) { |
| 28 | + publishArgs.push("--tag", npmTag); |
| 29 | + } |
| 30 | + |
| 31 | + console.log( |
| 32 | + `Publishing ${packageJson.name}@${packageJson.version} from ${pkg.dir} with npm`, |
| 33 | + ); |
| 34 | + |
| 35 | + execFileSync("npm", publishArgs, { |
| 36 | + cwd: packageDir, |
| 37 | + stdio: "inherit", |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +function orderPackagesForPublish(packages) { |
| 42 | + const packageMap = new Map( |
| 43 | + packages.map((pkg) => [ |
| 44 | + pkg.name, |
| 45 | + { ...pkg, manifest: readPackage(pkg.dir) }, |
| 46 | + ]), |
| 47 | + ); |
| 48 | + const visiting = new Set(); |
| 49 | + const visited = new Set(); |
| 50 | + const ordered = []; |
| 51 | + |
| 52 | + for (const pkg of packageMap.values()) { |
| 53 | + visit(pkg); |
| 54 | + } |
| 55 | + |
| 56 | + return ordered.map(({ manifest: _manifest, ...pkg }) => pkg); |
| 57 | + |
| 58 | + function visit(pkg) { |
| 59 | + if (visited.has(pkg.name)) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if (visiting.has(pkg.name)) { |
| 64 | + throw new Error( |
| 65 | + `Detected a publish dependency cycle involving ${pkg.name}`, |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + visiting.add(pkg.name); |
| 70 | + |
| 71 | + for (const dependencyName of getWorkspaceReleaseDependencies( |
| 72 | + pkg.manifest, |
| 73 | + )) { |
| 74 | + const dependency = packageMap.get(dependencyName); |
| 75 | + if (dependency) { |
| 76 | + visit(dependency); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + visiting.delete(pkg.name); |
| 81 | + visited.add(pkg.name); |
| 82 | + ordered.push(pkg); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +function getWorkspaceReleaseDependencies(manifest) { |
| 87 | + const dependencyNames = new Set(); |
| 88 | + |
| 89 | + for (const field of [ |
| 90 | + "dependencies", |
| 91 | + "optionalDependencies", |
| 92 | + "peerDependencies", |
| 93 | + "devDependencies", |
| 94 | + ]) { |
| 95 | + for (const dependencyName of Object.keys(manifest[field] ?? {})) { |
| 96 | + dependencyNames.add(dependencyName); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + dependencyNames.delete(manifest.name); |
| 101 | + return dependencyNames; |
| 102 | +} |
0 commit comments