version-bumper.js 5.9 KB

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