bump-release.mjs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env node
  2. /**
  3. * Bump the launcher workspace root and packages/* to one version, refresh the
  4. * repository lockfile, and verify. Usage: `pnpm release:bump <major|minor|patch|x.y.z>`.
  5. */
  6. import fs from 'node:fs';
  7. import path from 'node:path';
  8. import { spawnSync } from 'node:child_process';
  9. import { packageDirs, readJson, root } from './repo.mjs';
  10. const bump = process.argv[2];
  11. const releaseTypes = new Set(['major', 'minor', 'patch']);
  12. const repositoryRoot = path.resolve(root, '../..');
  13. function writeJson(file, value) {
  14. fs.writeFileSync(file, `${JSON.stringify(value, null, 2)}\n`);
  15. }
  16. function run(command, args, cwd = root) {
  17. const result = spawnSync(command, args, {
  18. cwd,
  19. stdio: 'inherit',
  20. env: { ...process.env, CI: 'true' },
  21. });
  22. if (result.error) throw result.error;
  23. if (result.status !== 0) {
  24. process.exit(result.status ?? 1);
  25. }
  26. }
  27. function packageFiles() {
  28. return ['package.json', ...packageDirs().map((dir) => path.join(dir, 'package.json'))];
  29. }
  30. function parseVersion(version) {
  31. const match = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.exec(version);
  32. if (!match) {
  33. throw new Error(`increment types need a plain x.y.z current version (current: ${version}) — pass an explicit target version instead`);
  34. }
  35. return match.slice(1).map((part) => Number(part));
  36. }
  37. /** Explicit target versions accept full semver, prereleases included (test publishes). */
  38. const EXPLICIT_VERSION = /^\d+\.\d+\.\d+(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$/;
  39. function nextVersion(current, release) {
  40. if (EXPLICIT_VERSION.test(release)) return release;
  41. if (!releaseTypes.has(release)) {
  42. throw new Error('Usage: pnpm release:bump <major|minor|patch|x.y.z>');
  43. }
  44. const [major, minor, patch] = parseVersion(current);
  45. if (release === 'major') return `${major + 1}.0.0`;
  46. if (release === 'minor') return `${major}.${minor + 1}.0`;
  47. return `${major}.${minor}.${patch + 1}`;
  48. }
  49. function currentPublishedVersion(files) {
  50. const versions = new Set(
  51. files
  52. .filter((file) => file.startsWith('packages/'))
  53. .map((file) => readJson(path.join(root, file)).version),
  54. );
  55. if (versions.size !== 1) {
  56. throw new Error(`published package versions differ: ${[...versions].join(', ')}`);
  57. }
  58. return [...versions][0];
  59. }
  60. if (!bump) {
  61. console.error('Usage: pnpm release:bump <major|minor|patch|x.y.z>');
  62. process.exit(1);
  63. }
  64. const files = packageFiles();
  65. const targetVersion = nextVersion(currentPublishedVersion(files), bump);
  66. for (const file of files) {
  67. const fullPath = path.join(root, file);
  68. const json = readJson(fullPath);
  69. json.version = targetVersion;
  70. writeJson(fullPath, json);
  71. console.log(`${file}: ${targetVersion}`);
  72. }
  73. run('pnpm', ['install', '--ignore-scripts', '--lockfile-only'], repositoryRoot);
  74. run('node', ['./scripts/verify-release.mjs']);
  75. console.log(`Release version bumped to ${targetVersion}`);