version-bumper.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env node
  2. const { GitProcess } = require('dugite');
  3. const fs = require('fs');
  4. const semver = require('semver');
  5. const path = require('path');
  6. const { promisify } = require('util');
  7. const minimist = require('minimist');
  8. const { ELECTRON_DIR } = require('../lib/utils');
  9. const versionUtils = require('./version-utils');
  10. const writeFile = promisify(fs.writeFile);
  11. const readFile = promisify(fs.readFile);
  12. function parseCommandLine () {
  13. let help;
  14. const opts = minimist(process.argv.slice(2), {
  15. string: [ 'bump', 'version' ],
  16. boolean: [ 'dryRun', 'help' ],
  17. alias: { 'version': ['v'] },
  18. unknown: arg => { help = true; }
  19. });
  20. if (help || opts.help || !opts.bump) {
  21. console.log(`
  22. Bump release version number. Possible arguments:\n
  23. --bump=patch to increment patch version\n
  24. --version={version} to set version number directly\n
  25. --dryRun to print the next version without updating files
  26. Note that you can use both --bump and --stable simultaneously.
  27. `);
  28. process.exit(0);
  29. }
  30. return opts;
  31. }
  32. // run the script
  33. async function main () {
  34. const opts = parseCommandLine();
  35. const currentVersion = await versionUtils.getElectronVersion();
  36. const version = await nextVersion(opts.bump, currentVersion);
  37. const parsed = semver.parse(version);
  38. const components = {
  39. major: parsed.major,
  40. minor: parsed.minor,
  41. patch: parsed.patch,
  42. pre: parsed.prerelease
  43. };
  44. // print would-be new version and exit early
  45. if (opts.dryRun) {
  46. console.log(`new version number would be: ${version}\n`);
  47. return 0;
  48. }
  49. // update all version-related files
  50. await Promise.all([
  51. updateVersion(version),
  52. updatePackageJSON(version),
  53. updateWinRC(components)
  54. ]);
  55. // commit all updated version-related files
  56. await commitVersionBump(version);
  57. console.log(`Bumped to version: ${version}`);
  58. }
  59. // get next version for release based on [nightly, beta, stable]
  60. async function nextVersion (bumpType, version) {
  61. if (versionUtils.isNightly(version) || versionUtils.isBeta(version)) {
  62. switch (bumpType) {
  63. case 'nightly':
  64. version = await versionUtils.nextNightly(version);
  65. break;
  66. case 'beta':
  67. version = await versionUtils.nextBeta(version);
  68. break;
  69. case 'stable':
  70. version = semver.valid(semver.coerce(version));
  71. break;
  72. default:
  73. throw new Error('Invalid bump type.');
  74. }
  75. } else if (versionUtils.isStable(version)) {
  76. switch (bumpType) {
  77. case 'nightly':
  78. version = versionUtils.nextNightly(version);
  79. break;
  80. case 'beta':
  81. throw new Error('Cannot bump to beta from stable.');
  82. case 'minor':
  83. version = semver.inc(version, 'minor');
  84. break;
  85. case 'stable':
  86. version = semver.inc(version, 'patch');
  87. break;
  88. default:
  89. throw new Error('Invalid bump type.');
  90. }
  91. } else {
  92. throw new Error(`Invalid current version: ${version}`);
  93. }
  94. return version;
  95. }
  96. // update VERSION file with latest release info
  97. async function updateVersion (version) {
  98. const versionPath = path.resolve(ELECTRON_DIR, 'ELECTRON_VERSION');
  99. await writeFile(versionPath, version, 'utf8');
  100. }
  101. // update package metadata files with new version
  102. async function updatePackageJSON (version) {
  103. const filePath = path.resolve(ELECTRON_DIR, 'package.json');
  104. const file = require(filePath);
  105. file.version = version;
  106. await writeFile(filePath, JSON.stringify(file, null, 2));
  107. }
  108. // push bump commit to release branch
  109. async function commitVersionBump (version) {
  110. const gitArgs = ['commit', '-a', '-m', `Bump v${version}`, '-n'];
  111. await GitProcess.exec(gitArgs, ELECTRON_DIR);
  112. }
  113. // updates atom.rc file with new semver values
  114. async function updateWinRC (components) {
  115. const filePath = path.resolve(ELECTRON_DIR, 'shell', 'browser', 'resources', 'win', 'atom.rc');
  116. const data = await readFile(filePath, 'utf8');
  117. const arr = data.split('\n');
  118. arr.forEach((line, idx) => {
  119. if (line.includes('FILEVERSION')) {
  120. arr[idx] = ` FILEVERSION ${versionUtils.makeVersion(components, ',', versionUtils.preType.PARTIAL)}`;
  121. arr[idx + 1] = ` PRODUCTVERSION ${versionUtils.makeVersion(components, ',', versionUtils.preType.PARTIAL)}`;
  122. } else if (line.includes('FileVersion')) {
  123. arr[idx] = ` VALUE "FileVersion", "${versionUtils.makeVersion(components, '.')}"`;
  124. arr[idx + 5] = ` VALUE "ProductVersion", "${versionUtils.makeVersion(components, '.')}"`;
  125. }
  126. });
  127. await writeFile(filePath, arr.join('\n'));
  128. }
  129. if (process.mainModule === module) {
  130. main().catch((error) => {
  131. console.error(error);
  132. process.exit(1);
  133. });
  134. }
  135. module.exports = { nextVersion };