|
| 1 | +#!/usr/bin/env -S tsx |
| 2 | + |
| 3 | +import { promises as fs } from 'node:fs'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { spawn } from 'node:child_process'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const __dirname = path.dirname(__filename); |
| 10 | +const repoRoot = path.resolve(__dirname, '..'); |
| 11 | + |
| 12 | +const IGNORED_DIRECTORIES = new Set([ |
| 13 | + '.git', |
| 14 | + 'node_modules', |
| 15 | + 'target', |
| 16 | + 'dist', |
| 17 | + 'build', |
| 18 | + '.pnpm-store' |
| 19 | +]); |
| 20 | + |
| 21 | +async function main(): Promise<void> { |
| 22 | + const version = process.argv[2]; |
| 23 | + |
| 24 | + if (!version) { |
| 25 | + console.error('Usage: release <version>'); |
| 26 | + process.exitCode = 1; |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (!isValidVersion(version)) { |
| 31 | + console.error(`Invalid version: ${version}`); |
| 32 | + process.exitCode = 1; |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + const manifests = await collectManifests(repoRoot); |
| 37 | + |
| 38 | + await Promise.all(manifests.packageJson.map(file => updatePackageJson(file, version))); |
| 39 | + await Promise.all(manifests.cargoToml.map(file => updateCargoToml(file, version))); |
| 40 | + |
| 41 | + await runCommand('pnpm', ['publish'], path.join(repoRoot, 'typescript')); |
| 42 | + await runCommand('cargo', ['publish'], path.join(repoRoot, 'rust')); |
| 43 | +} |
| 44 | + |
| 45 | +function isValidVersion(version: string): boolean { |
| 46 | + return /^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$/.test(version); |
| 47 | +} |
| 48 | + |
| 49 | +async function collectManifests(root: string): Promise<{ packageJson: string[]; cargoToml: string[] }> { |
| 50 | + const packageJson: string[] = []; |
| 51 | + const cargoToml: string[] = []; |
| 52 | + |
| 53 | + await walk(root, async filePath => { |
| 54 | + const base = path.basename(filePath); |
| 55 | + |
| 56 | + if (base === 'package.json') { |
| 57 | + packageJson.push(filePath); |
| 58 | + } else if (base === 'Cargo.toml') { |
| 59 | + cargoToml.push(filePath); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + return { packageJson, cargoToml }; |
| 64 | +} |
| 65 | + |
| 66 | +async function walk(dir: string, onFile: (filePath: string) => Promise<void>): Promise<void> { |
| 67 | + const entries = await fs.readdir(dir, { withFileTypes: true }); |
| 68 | + |
| 69 | + for (const entry of entries) { |
| 70 | + if (IGNORED_DIRECTORIES.has(entry.name)) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + const fullPath = path.join(dir, entry.name); |
| 75 | + |
| 76 | + if (entry.isDirectory()) { |
| 77 | + await walk(fullPath, onFile); |
| 78 | + } else if (entry.isFile()) { |
| 79 | + await onFile(fullPath); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +async function updatePackageJson(filePath: string, version: string): Promise<void> { |
| 85 | + const raw = await fs.readFile(filePath, 'utf8'); |
| 86 | + let parsed: unknown; |
| 87 | + |
| 88 | + try { |
| 89 | + parsed = JSON.parse(raw); |
| 90 | + } catch (error) { |
| 91 | + throw new Error(`Failed to parse JSON in ${relative(filePath)}: ${(error as Error).message}`); |
| 92 | + } |
| 93 | + |
| 94 | + if (!parsed || typeof parsed !== 'object') { |
| 95 | + throw new Error(`Unexpected JSON shape in ${relative(filePath)} (expected object)`); |
| 96 | + } |
| 97 | + |
| 98 | + const pkg = parsed as { version?: unknown; [key: string]: unknown }; |
| 99 | + |
| 100 | + if (typeof pkg.version !== 'string') { |
| 101 | + console.warn(`Skipping ${relative(filePath)} (no version field)`); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + pkg.version = version; |
| 106 | + |
| 107 | + const newline = raw.includes('\r\n') ? '\r\n' : '\n'; |
| 108 | + let serialized = JSON.stringify(pkg, null, 2); |
| 109 | + serialized = serialized.replace(/\n/g, newline) + newline; |
| 110 | + |
| 111 | + await fs.writeFile(filePath, serialized, 'utf8'); |
| 112 | + console.log(`Updated ${relative(filePath)} to ${version}`); |
| 113 | +} |
| 114 | + |
| 115 | +async function updateCargoToml(filePath: string, version: string): Promise<void> { |
| 116 | + const raw = await fs.readFile(filePath, 'utf8'); |
| 117 | + const newline = raw.includes('\r\n') ? '\r\n' : '\n'; |
| 118 | + const lines = raw.split(/\r?\n/); |
| 119 | + const hadFinalNewline = raw.endsWith('\n') || raw.endsWith('\r\n'); |
| 120 | + |
| 121 | + let inPackageSection = false; |
| 122 | + let updated = false; |
| 123 | + |
| 124 | + const updatedLines = lines.map(line => { |
| 125 | + const trimmed = line.trim(); |
| 126 | + |
| 127 | + if (trimmed.startsWith('[') && trimmed.endsWith(']')) { |
| 128 | + inPackageSection = trimmed === '[package]'; |
| 129 | + return line; |
| 130 | + } |
| 131 | + |
| 132 | + if (inPackageSection) { |
| 133 | + const match = line.match(/^(\s*version\s*=\s*")([^\"]*)(".*)$/); |
| 134 | + if (match) { |
| 135 | + updated = true; |
| 136 | + return `${match[1]}${version}${match[3]}`; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return line; |
| 141 | + }); |
| 142 | + |
| 143 | + if (!updated) { |
| 144 | + console.warn(`Skipping ${relative(filePath)} (no [package] version field found)`); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + let content = updatedLines.join(newline); |
| 149 | + if (hadFinalNewline && !content.endsWith(newline)) { |
| 150 | + content += newline; |
| 151 | + } |
| 152 | + |
| 153 | + await fs.writeFile(filePath, content, 'utf8'); |
| 154 | + console.log(`Updated ${relative(filePath)} to ${version}`); |
| 155 | +} |
| 156 | + |
| 157 | +async function runCommand(command: string, args: string[], cwd: string): Promise<void> { |
| 158 | + console.log(`Running ${command} ${args.join(' ')} in ${relative(cwd)}`); |
| 159 | + |
| 160 | + await new Promise<void>((resolve, reject) => { |
| 161 | + const child = spawn(command, args, { |
| 162 | + cwd, |
| 163 | + stdio: 'inherit', |
| 164 | + env: process.env |
| 165 | + }); |
| 166 | + |
| 167 | + child.on('error', reject); |
| 168 | + child.on('exit', code => { |
| 169 | + if (code === 0) { |
| 170 | + resolve(); |
| 171 | + } else { |
| 172 | + reject(new Error(`${command} ${args.join(' ')} exited with code ${code}`)); |
| 173 | + } |
| 174 | + }); |
| 175 | + }); |
| 176 | +} |
| 177 | + |
| 178 | +function relative(filePath: string): string { |
| 179 | + return path.relative(repoRoot, filePath) || '.'; |
| 180 | +} |
| 181 | + |
| 182 | +main().catch(error => { |
| 183 | + console.error(error); |
| 184 | + process.exit(1); |
| 185 | +}); |
0 commit comments