|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Check which packages need publishing to NPM |
| 5 | + * |
| 6 | + * Scans all packages in the monorepo and checks if their current versions |
| 7 | + * exist on NPM. Fails if no packages need publishing (all versions already exist). |
| 8 | + * |
| 9 | + * Exit codes: |
| 10 | + * 0 - Success, packages need publishing |
| 11 | + * 1 - Error, no packages need publishing or script failed |
| 12 | + */ |
| 13 | + |
| 14 | +import { execSync, type ExecException } from 'child_process'; |
| 15 | +import { readFileSync } from 'fs'; |
| 16 | +import { join } from 'path'; |
| 17 | + |
| 18 | +interface PackageJson { |
| 19 | + name: string; |
| 20 | + version: string; |
| 21 | + private?: boolean; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Check if a specific package version exists on NPM |
| 26 | + */ |
| 27 | +function checkPackageOnNpm(packageName: string, version: string): boolean { |
| 28 | + try { |
| 29 | + execSync(`npm view ${packageName}@${version} version`, { |
| 30 | + stdio: 'pipe', |
| 31 | + encoding: 'utf8', |
| 32 | + }); |
| 33 | + return true; // Package exists on NPM |
| 34 | + } catch (error) { |
| 35 | + // npm view exits with code 1 when package doesn't exist (404) |
| 36 | + // Check if this is a "not found" error vs a real error (network, etc) |
| 37 | + const execError = error as ExecException; |
| 38 | + const stderr = execError.stderr?.toString() || ''; |
| 39 | + if (stderr.includes('404') || stderr.includes('Not Found')) { |
| 40 | + return false; // Package doesn't exist on NPM |
| 41 | + } |
| 42 | + // For other errors (network issues, npm down, etc), throw so we don't incorrectly |
| 43 | + // report that packages need publishing when we can't actually check NPM |
| 44 | + throw error; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Get all workspace packages using yarn workspaces list |
| 50 | + */ |
| 51 | +function getWorkspacePackages(): string[] { |
| 52 | + try { |
| 53 | + const output = execSync('yarn workspaces list --json', { |
| 54 | + encoding: 'utf8', |
| 55 | + stdio: ['pipe', 'pipe', 'pipe'], |
| 56 | + }); |
| 57 | + |
| 58 | + const workspaces: string[] = []; |
| 59 | + // Each line is a JSON object |
| 60 | + for (const line of output.trim().split('\n')) { |
| 61 | + const workspace = JSON.parse(line); |
| 62 | + // Skip the root workspace (location is '.') |
| 63 | + if (workspace.location && workspace.location !== '.') { |
| 64 | + workspaces.push(join(process.cwd(), workspace.location, 'package.json')); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return workspaces; |
| 69 | + } catch (error) { |
| 70 | + console.error('❌ ERROR: Failed to get yarn workspaces'); |
| 71 | + console.error((error as Error).message); |
| 72 | + process.exit(1); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Main function that checks all packages in the monorepo |
| 78 | + */ |
| 79 | +function main(dryRun = false): void { |
| 80 | + console.log('🔍 Checking which packages need publishing...\n'); |
| 81 | + |
| 82 | + const packagesToPublish: string[] = []; |
| 83 | + const packagesAlreadyPublished: string[] = []; |
| 84 | + const packagesSkipped: string[] = []; |
| 85 | + |
| 86 | + const packageJsonPaths = getWorkspacePackages(); |
| 87 | + |
| 88 | + for (const packageJsonPath of packageJsonPaths) { |
| 89 | + |
| 90 | + let packageJson: PackageJson; |
| 91 | + try { |
| 92 | + const content = readFileSync(packageJsonPath, 'utf8'); |
| 93 | + packageJson = JSON.parse(content); |
| 94 | + } catch (error) { |
| 95 | + console.error(`⚠️ Failed to read ${packageJsonPath}:`, (error as Error).message); |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + const { name, version, private: isPrivate } = packageJson; |
| 100 | + |
| 101 | + if (!name || !version) { |
| 102 | + console.log(`⏭️ Skipping ${packageJsonPath}: missing name or version`); |
| 103 | + packagesSkipped.push(packageJsonPath); |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + if (isPrivate) { |
| 108 | + console.log(`⏭️ Skipping private package: ${name}@${version}`); |
| 109 | + packagesSkipped.push(`${name}@${version}`); |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + const existsOnNpm = checkPackageOnNpm(name, version); |
| 114 | + |
| 115 | + if (existsOnNpm) { |
| 116 | + console.log(`✅ Already published: ${name}@${version}`); |
| 117 | + packagesAlreadyPublished.push(`${name}@${version}`); |
| 118 | + } else { |
| 119 | + console.log(`📦 Will publish: ${name}@${version}`); |
| 120 | + packagesToPublish.push(`${name}@${version}`); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Print summary |
| 125 | + console.log('\n' + '='.repeat(60)); |
| 126 | + console.log('Summary:'); |
| 127 | + console.log(` Packages to publish: ${packagesToPublish.length}`); |
| 128 | + console.log(` Already on NPM: ${packagesAlreadyPublished.length}`); |
| 129 | + console.log(` Skipped: ${packagesSkipped.length}`); |
| 130 | + console.log('='.repeat(60)); |
| 131 | + |
| 132 | + // Print packages to publish if any |
| 133 | + if (dryRun && packagesToPublish.length > 0) { |
| 134 | + console.log('\nPackages that will be published:'); |
| 135 | + packagesToPublish.forEach(pkg => console.log(` - ${pkg}`)); |
| 136 | + } |
| 137 | + |
| 138 | + // Fail if nothing to publish (unless dry-run) |
| 139 | + if (packagesToPublish.length === 0) { |
| 140 | + if (dryRun) { |
| 141 | + console.log('\n✅ Dry-run: No packages would be published'); |
| 142 | + console.log('All package versions already exist on NPM.'); |
| 143 | + process.exit(0); |
| 144 | + } else { |
| 145 | + console.log('\n❌ ERROR: No packages need publishing!'); |
| 146 | + console.log('All package versions already exist on NPM.\n'); |
| 147 | + console.log('This likely means:'); |
| 148 | + console.log(' 1. The version bump PR was merged without actually bumping versions'); |
| 149 | + console.log(' 2. Packages were already published manually'); |
| 150 | + console.log(' 3. The version bump workflow didn\'t run correctly'); |
| 151 | + process.exit(1); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (dryRun) { |
| 156 | + console.log(`\n✅ Dry-run: ${packagesToPublish.length} package(s) would be published`); |
| 157 | + } else { |
| 158 | + console.log(`\n✅ Ready to publish ${packagesToPublish.length} package(s)`); |
| 159 | + } |
| 160 | + process.exit(0); |
| 161 | +} |
| 162 | + |
| 163 | +// Parse CLI args |
| 164 | +const args = process.argv.slice(2); |
| 165 | +const dryRun = args.includes('--dry-run'); |
| 166 | + |
| 167 | +main(dryRun); |
0 commit comments