|
| 1 | +// Note: this is not a technically a esbuild-related script. |
| 2 | + |
| 3 | +import {readFileSync, writeFileSync} from 'node:fs' |
| 4 | +import semver from 'semver' |
| 5 | +import {executeCommand, getCommandOutput, getWorkspaces} from './common.js' |
| 6 | + |
| 7 | +const main = async () => { |
| 8 | + if (process.argv.length < 3) { |
| 9 | + console.error('Usage: node publish.js semver') |
| 10 | + process.exit(1) |
| 11 | + } |
| 12 | + const ver = process.argv[2] |
| 13 | + if (!semver.valid(ver)) { |
| 14 | + console.error(`❌ Invalid semver version: ${ver}`) |
| 15 | + process.exit(1) |
| 16 | + } |
| 17 | + |
| 18 | + console.log('🔍 Checking if all changes are committed') |
| 19 | + const changes = getCommandOutput('git', 'status', '--porcelain') |
| 20 | + if (changes !== '') { |
| 21 | + console.error('❌ Please commit all changes before publishing') |
| 22 | + process.exit(1) |
| 23 | + } |
| 24 | + |
| 25 | + console.log('🔍 Checking we are on main') |
| 26 | + const branch = getCommandOutput('git', 'branch', '--show-current') |
| 27 | + if (branch !== 'main') { |
| 28 | + console.error('❌ Please switch to main branch before publishing') |
| 29 | + process.exit(1) |
| 30 | + } |
| 31 | + |
| 32 | + const pkgs = [] |
| 33 | + const workspaces = getWorkspaces() |
| 34 | + const workspaceNames = workspaces.map((w) => w.name) |
| 35 | + for (const workspace of workspaces) { |
| 36 | + console.log(`✏️ Updating ${workspace.name} version to: ${ver}`) |
| 37 | + const pkgFile = `${workspace.location}/package.json` |
| 38 | + const pkg = JSON.parse(readFileSync(pkgFile)) |
| 39 | + pkg.version = ver |
| 40 | + for (const dep of Object.keys(pkg.dependencies ?? {})) { |
| 41 | + if (workspaceNames.includes(dep)) { |
| 42 | + pkg.dependencies[dep] = `^${ver}` |
| 43 | + } |
| 44 | + } |
| 45 | + console.log(`📝 Writing ${workspace.name} package.json`) |
| 46 | + const content = JSON.stringify(pkg, null, 2) |
| 47 | + writeFileSync(pkgFile, content) |
| 48 | + pkgs.push(pkgFile) |
| 49 | + } |
| 50 | + |
| 51 | + console.log('✅ Make sure the package.json are formatted correctly') |
| 52 | + executeCommand('npm', ['run', 'format:fix:internal', '--', ...pkgs]) |
| 53 | + |
| 54 | + for (const workspace of workspaces) { |
| 55 | + console.log(`🔨 Publishing ${workspace.name}`) |
| 56 | + executeCommand('npm', ['publish'], { |
| 57 | + cwd: workspace.location, |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + console.log('↩️ Committing changes') |
| 62 | + executeCommand('git', ['commit', '-am', `release: v${ver}`]) |
| 63 | + |
| 64 | + console.log('🏷️ Tagging') |
| 65 | + executeCommand('git', ['tag', `v${ver}`]) |
| 66 | + executeCommand('git', ['push', '--tags']) |
| 67 | + executeCommand('gh', ['release', 'create', `v${ver}`, '--generate-notes']) |
| 68 | +} |
| 69 | + |
| 70 | +main() |
0 commit comments