version-bumper.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env node
  2. import { valid, coerce, inc } from 'semver';
  3. import { parseArgs } from 'node:util';
  4. import { VersionBumpType } from './types';
  5. import {
  6. isNightly,
  7. isAlpha,
  8. isBeta,
  9. nextNightly,
  10. nextAlpha,
  11. nextBeta,
  12. isStable
  13. } from './version-utils';
  14. import { getElectronVersion } from '../lib/get-version';
  15. // run the script
  16. async function main () {
  17. const { values: { bump, dryRun, help } } = parseArgs({
  18. options: {
  19. bump: {
  20. type: 'string'
  21. },
  22. dryRun: {
  23. type: 'boolean'
  24. },
  25. help: {
  26. type: 'boolean'
  27. }
  28. }
  29. });
  30. if (!bump || help) {
  31. console.log(`
  32. Bump release version number. Possible arguments:\n
  33. --bump=patch to increment patch version\n
  34. --version={version} to set version number directly\n
  35. --dryRun to print the next version without updating files
  36. Note that you can use both --bump and --stable simultaneously.
  37. `);
  38. if (!bump) process.exit(0);
  39. else process.exit(1);
  40. }
  41. const currentVersion = getElectronVersion();
  42. const version = await nextVersion(bump as VersionBumpType, currentVersion);
  43. // print would-be new version and exit early
  44. if (dryRun) {
  45. console.log(`new version number would be: ${version}\n`);
  46. return 0;
  47. }
  48. console.log(`Bumped to version: ${version}`);
  49. }
  50. // get next version for release based on [nightly, alpha, beta, stable]
  51. export async function nextVersion (bumpType: VersionBumpType, version: string) {
  52. if (isNightly(version) || isAlpha(version) || isBeta(version)) {
  53. switch (bumpType) {
  54. case 'nightly':
  55. version = await nextNightly(version);
  56. break;
  57. case 'alpha':
  58. version = await nextAlpha(version);
  59. break;
  60. case 'beta':
  61. version = await nextBeta(version);
  62. break;
  63. case 'stable':
  64. version = valid(coerce(version))!;
  65. break;
  66. default:
  67. throw new Error('Invalid bump type.');
  68. }
  69. } else if (isStable(version)) {
  70. switch (bumpType) {
  71. case 'nightly':
  72. version = await nextNightly(version);
  73. break;
  74. case 'alpha':
  75. throw new Error('Cannot bump to alpha from stable.');
  76. case 'beta':
  77. throw new Error('Cannot bump to beta from stable.');
  78. case 'minor':
  79. version = inc(version, 'minor')!;
  80. break;
  81. case 'stable':
  82. version = inc(version, 'patch')!;
  83. break;
  84. default:
  85. throw new Error('Invalid bump type.');
  86. }
  87. } else {
  88. throw new Error(`Invalid current version: ${version}`);
  89. }
  90. return version;
  91. }
  92. if (require.main === module) {
  93. main().catch((error) => {
  94. console.error(error);
  95. process.exit(1);
  96. });
  97. }