version-bumper.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/usr/bin/env node
  2. const { GitProcess } = require('dugite');
  3. const { promises: fs } = require('fs');
  4. const semver = require('semver');
  5. const path = require('path');
  6. const minimist = require('minimist');
  7. const { ELECTRON_DIR } = require('../lib/utils');
  8. const versionUtils = require('./version-utils');
  9. const supported = path.resolve(ELECTRON_DIR, 'docs', 'tutorial', 'support.md');
  10. const writeFile = fs.writeFile;
  11. const readFile = 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. if (shouldUpdateSupported(opts.bump, currentVersion, version)) {
  50. await updateSupported(version, supported);
  51. }
  52. // update all version-related files
  53. await Promise.all([
  54. updateVersion(version),
  55. updatePackageJSON(version),
  56. updateWinRC(components)
  57. ]);
  58. // commit all updated version-related files
  59. await commitVersionBump(version);
  60. console.log(`Bumped to version: ${version}`);
  61. }
  62. // get next version for release based on [nightly, alpha, beta, stable]
  63. async function nextVersion (bumpType, version) {
  64. if (
  65. versionUtils.isNightly(version) ||
  66. versionUtils.isAlpha(version) ||
  67. versionUtils.isBeta(version)
  68. ) {
  69. switch (bumpType) {
  70. case 'nightly':
  71. version = await versionUtils.nextNightly(version);
  72. break;
  73. case 'alpha':
  74. version = await versionUtils.nextAlpha(version);
  75. break;
  76. case 'beta':
  77. version = await versionUtils.nextBeta(version);
  78. break;
  79. case 'stable':
  80. version = semver.valid(semver.coerce(version));
  81. break;
  82. default:
  83. throw new Error('Invalid bump type.');
  84. }
  85. } else if (versionUtils.isStable(version)) {
  86. switch (bumpType) {
  87. case 'nightly':
  88. version = versionUtils.nextNightly(version);
  89. break;
  90. case 'alpha':
  91. throw new Error('Cannot bump to alpha from stable.');
  92. case 'beta':
  93. throw new Error('Cannot bump to beta from stable.');
  94. case 'minor':
  95. version = semver.inc(version, 'minor');
  96. break;
  97. case 'stable':
  98. version = semver.inc(version, 'patch');
  99. break;
  100. default:
  101. throw new Error('Invalid bump type.');
  102. }
  103. } else {
  104. throw new Error(`Invalid current version: ${version}`);
  105. }
  106. return version;
  107. }
  108. function shouldUpdateSupported (bump, current, version) {
  109. return isMajorStable(bump, current) || isMajorNightly(version, current);
  110. }
  111. function isMajorStable (bump, currentVersion) {
  112. if (versionUtils.isBeta(currentVersion) && (bump === 'stable')) return true;
  113. return false;
  114. }
  115. function isMajorNightly (version, currentVersion) {
  116. const parsed = semver.parse(version);
  117. const current = semver.parse(currentVersion);
  118. if (versionUtils.isNightly(currentVersion) && (parsed.major > current.major)) return true;
  119. return false;
  120. }
  121. // update VERSION file with latest release info
  122. async function updateVersion (version) {
  123. const versionPath = path.resolve(ELECTRON_DIR, 'ELECTRON_VERSION');
  124. await writeFile(versionPath, version, 'utf8');
  125. }
  126. // update package metadata files with new version
  127. async function updatePackageJSON (version) {
  128. const filePath = path.resolve(ELECTRON_DIR, 'package.json');
  129. const file = require(filePath);
  130. file.version = version;
  131. await writeFile(filePath, JSON.stringify(file, null, 2));
  132. }
  133. // push bump commit to release branch
  134. async function commitVersionBump (version) {
  135. const gitArgs = ['commit', '-a', '-m', `Bump v${version}`, '-n'];
  136. await GitProcess.exec(gitArgs, ELECTRON_DIR);
  137. }
  138. // updates electron.rc file with new semver values
  139. async function updateWinRC (components) {
  140. const filePath = path.resolve(ELECTRON_DIR, 'shell', 'browser', 'resources', 'win', 'electron.rc');
  141. const data = await readFile(filePath, 'utf8');
  142. const arr = data.split('\n');
  143. arr.forEach((line, idx) => {
  144. if (line.includes('FILEVERSION')) {
  145. arr[idx] = ` FILEVERSION ${versionUtils.makeVersion(components, ',', versionUtils.preType.PARTIAL)}`;
  146. arr[idx + 1] = ` PRODUCTVERSION ${versionUtils.makeVersion(components, ',', versionUtils.preType.PARTIAL)}`;
  147. } else if (line.includes('FileVersion')) {
  148. arr[idx] = ` VALUE "FileVersion", "${versionUtils.makeVersion(components, '.')}"`;
  149. arr[idx + 5] = ` VALUE "ProductVersion", "${versionUtils.makeVersion(components, '.')}"`;
  150. }
  151. });
  152. await writeFile(filePath, arr.join('\n'));
  153. }
  154. // updates support.md file with new semver values (stable only)
  155. async function updateSupported (version, filePath) {
  156. const v = parseInt(version);
  157. const newVersions = [`* ${v}.x.y`, `* ${v - 1}.x.y`, `* ${v - 2}.x.y`, `* ${v - 3}`];
  158. const contents = await readFile(filePath, 'utf8');
  159. const previousVersions = contents.split('\n').filter((elem) => {
  160. return (/[^\n]*\.x\.y[^\n]*/).test(elem);
  161. }, []);
  162. const newContents = previousVersions.reduce((contents, current, i) => {
  163. return contents.replace(current, newVersions[i]);
  164. }, contents);
  165. await writeFile(filePath, newContents, 'utf8');
  166. }
  167. if (process.mainModule === module) {
  168. main().catch((error) => {
  169. console.error(error);
  170. process.exit(1);
  171. });
  172. }
  173. module.exports = { nextVersion, shouldUpdateSupported, updateSupported };