version-bumper.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 'stable':
  83. version = semver.inc(version, 'patch')
  84. break
  85. default:
  86. throw new Error('Invalid bump type.')
  87. }
  88. } else {
  89. throw new Error(`Invalid current version: ${version}`)
  90. }
  91. return version
  92. }
  93. // update VERSION file with latest release info
  94. async function updateVersion (version) {
  95. const versionPath = path.resolve(ELECTRON_DIR, 'ELECTRON_VERSION')
  96. await writeFile(versionPath, version, 'utf8')
  97. }
  98. // update package metadata files with new version
  99. async function updatePackageJSON (version) {
  100. const filePath = path.resolve(ELECTRON_DIR, 'package.json')
  101. const file = require(filePath)
  102. file.version = version
  103. await writeFile(filePath, JSON.stringify(file, null, 2))
  104. }
  105. // push bump commit to release branch
  106. async function commitVersionBump (version) {
  107. const gitArgs = ['commit', '-a', '-m', `Bump v${version}`, '-n']
  108. await GitProcess.exec(gitArgs, ELECTRON_DIR)
  109. }
  110. // updates atom.rc file with new semver values
  111. async function updateWinRC (components) {
  112. const filePath = path.resolve(ELECTRON_DIR, 'shell', 'browser', 'resources', 'win', 'atom.rc')
  113. const data = await readFile(filePath, 'utf8')
  114. const arr = data.split('\n')
  115. arr.forEach((line, idx) => {
  116. if (line.includes('FILEVERSION')) {
  117. arr[idx] = ` FILEVERSION ${versionUtils.makeVersion(components, ',', versionUtils.preType.PARTIAL)}`
  118. arr[idx + 1] = ` PRODUCTVERSION ${versionUtils.makeVersion(components, ',', versionUtils.preType.PARTIAL)}`
  119. } else if (line.includes('FileVersion')) {
  120. arr[idx] = ` VALUE "FileVersion", "${versionUtils.makeVersion(components, '.')}"`
  121. arr[idx + 5] = ` VALUE "ProductVersion", "${versionUtils.makeVersion(components, '.')}"`
  122. }
  123. })
  124. await writeFile(filePath, arr.join('\n'))
  125. }
  126. if (process.mainModule === module) {
  127. main().catch((error) => {
  128. console.error(error)
  129. process.exit(1)
  130. })
  131. }
  132. module.exports = { nextVersion }