bump-version.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/usr/bin/env node
  2. const { GitProcess } = require('dugite')
  3. const utils = require('./lib/version-utils')
  4. const plist = require('plist')
  5. const fs = require('fs')
  6. const semver = require('semver')
  7. const path = require('path')
  8. const { promisify } = require('util')
  9. const minimist = require('minimist')
  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 utils.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. updateInfoPlist(version),
  53. updatePackageJSON(version),
  54. updateVersionH(components),
  55. updateWinRC(components)
  56. ])
  57. // commit all updated version-related files
  58. await commitVersionBump(version)
  59. console.log(`Bumped to version: ${version}`)
  60. }
  61. // get next version for release based on [nightly, beta, stable]
  62. async function nextVersion (bumpType, version) {
  63. if (utils.isNightly(version) || utils.isBeta(version)) {
  64. switch (bumpType) {
  65. case 'nightly':
  66. version = await utils.nextNightly(version)
  67. break
  68. case 'beta':
  69. version = await utils.nextBeta(version)
  70. break
  71. case 'stable':
  72. version = semver.valid(semver.coerce(version))
  73. break
  74. default:
  75. throw new Error('Invalid bump type.')
  76. }
  77. } else if (utils.isStable(version)) {
  78. switch (bumpType) {
  79. case 'nightly':
  80. version = utils.nextNightly(version)
  81. break
  82. case 'beta':
  83. throw new Error('Cannot bump to beta from stable.')
  84. case 'stable':
  85. version = semver.inc(version, 'patch')
  86. break
  87. default:
  88. throw new Error('Invalid bump type.')
  89. }
  90. } else {
  91. throw new Error(`Invalid current version: ${version}`)
  92. }
  93. return version
  94. }
  95. // update VERSION file with latest release info
  96. async function updateVersion (version) {
  97. const versionPath = path.resolve(__dirname, '..', 'ELECTRON_VERSION')
  98. await writeFile(versionPath, version, 'utf8')
  99. }
  100. // update package metadata files with new version
  101. async function updatePackageJSON (version) {
  102. ['package.json'].forEach(async fileName => {
  103. const filePath = path.resolve(__dirname, '..', fileName)
  104. const file = require(filePath)
  105. file.version = version
  106. await writeFile(filePath, JSON.stringify(file, null, 2))
  107. })
  108. }
  109. // update CFBundle version information and overwrite pre-existing file
  110. // TODO(codebytere): provide these version fields at GN build time
  111. async function updateInfoPlist (version) {
  112. const filePath = path.resolve(__dirname, '..', 'atom', 'browser', 'resources', 'mac', 'Info.plist')
  113. const file = plist.parse(await readFile(filePath, { encoding: 'utf8' }))
  114. file.CFBundleVersion = version
  115. file.CFBundleShortVersionString = version
  116. await writeFile(filePath, plist.build(file))
  117. }
  118. // push bump commit to release branch
  119. async function commitVersionBump (version) {
  120. const gitDir = path.resolve(__dirname, '..')
  121. const gitArgs = ['commit', '-a', '-m', `Bump v${version}`, '-n']
  122. await GitProcess.exec(gitArgs, gitDir)
  123. }
  124. // updates atom_version.h file with new semver values
  125. // TODO(codebytere): auto-generate this
  126. async function updateVersionH (components) {
  127. const filePath = path.resolve(__dirname, '..', 'atom', 'common', 'atom_version.h')
  128. const data = await readFile(filePath, 'utf8')
  129. const arr = data.split('\n')
  130. const pre = components.pre && components.pre.length >= 2 ? `-${components.pre[0]}.${components.pre[1]}` : null
  131. arr.forEach((item, idx) => {
  132. if (item.includes('#define ATOM_MAJOR_VERSION')) {
  133. arr[idx] = `#define ATOM_MAJOR_VERSION ${components.major}`
  134. arr[idx + 1] = `#define ATOM_MINOR_VERSION ${components.minor}`
  135. arr[idx + 2] = `#define ATOM_PATCH_VERSION ${components.patch}`
  136. arr[idx + 4] = pre ? `#define ATOM_PRE_RELEASE_VERSION ${pre}` : '// #define ATOM_PRE_RELEASE_VERSION'
  137. }
  138. })
  139. await writeFile(filePath, arr.join('\n'))
  140. }
  141. // updates atom.rc file with new semver values
  142. async function updateWinRC (components) {
  143. const filePath = path.resolve(__dirname, '..', 'atom', 'browser', 'resources', 'win', 'atom.rc')
  144. const data = await readFile(filePath, 'utf8')
  145. const arr = data.split('\n')
  146. arr.forEach((line, idx) => {
  147. if (line.includes('FILEVERSION')) {
  148. arr[idx] = ` FILEVERSION ${utils.makeVersion(components, ',', utils.preType.PARTIAL)}`
  149. arr[idx + 1] = ` PRODUCTVERSION ${utils.makeVersion(components, ',', utils.preType.PARTIAL)}`
  150. } else if (line.includes('FileVersion')) {
  151. arr[idx] = ` VALUE "FileVersion", "${utils.makeVersion(components, '.')}"`
  152. arr[idx + 5] = ` VALUE "ProductVersion", "${utils.makeVersion(components, '.')}"`
  153. }
  154. })
  155. await writeFile(filePath, arr.join('\n'))
  156. }
  157. if (process.mainModule === module) {
  158. main().catch((error) => {
  159. console.error(error)
  160. process.exit(1)
  161. })
  162. }
  163. module.exports = { nextVersion }