after-version-bump.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env node
  2. // Called by the `version` npm script.
  3. // This is run _after_ lerna updates the version numbers,
  4. // but _before_ it commits, so we have time to update the
  5. // version numbers throughout the repo and add it to the
  6. // release commit.
  7. // After updating version numbers, this runs a full
  8. // IS_RELEASE_BUILD=1 build, so that the version numbers
  9. // are properly embedded in the JS bundles.
  10. // NOTE this _amends_ the previous commit, which should
  11. // already be a "Release" commit generated by bin/release.
  12. const lastCommitMessage = require('last-commit-message')
  13. const { spawn } = require('child_process')
  14. const { promisify } = require('util')
  15. const once = require('events.once')
  16. const globby = require('globby')
  17. const fs = require('fs')
  18. const readFile = promisify(fs.readFile)
  19. const writeFile = promisify(fs.writeFile)
  20. async function replaceInFile (filename, replacements) {
  21. let content = await readFile(filename, 'utf8')
  22. for (const [rx, replacement] of replacements) {
  23. content = content.replace(rx, replacement)
  24. }
  25. await writeFile(filename, content, 'utf8')
  26. }
  27. async function updateVersions (files, packageName) {
  28. const { version } = require(`../packages/${packageName}/package.json`)
  29. // uppy → uppy
  30. // @uppy/robodog → uppy/robodog
  31. const urlPart = packageName === 'uppy' ? packageName : packageName.slice(1)
  32. const replacements = new Map([
  33. [RegExp(`${urlPart}/v\\d+\\.\\d+\\.\\d+\\/`, 'g'), `${urlPart}/v${version}/`],
  34. // maybe more later
  35. ])
  36. console.log('replacing', replacements, 'in', files.length, 'files')
  37. for (const f of files) {
  38. await replaceInFile(f, replacements)
  39. }
  40. }
  41. async function gitAdd (files) {
  42. const git = spawn('git', ['add', ...files], { stdio: 'inherit' })
  43. const [exitCode] = await once(git, 'exit')
  44. if (exitCode !== 0) {
  45. throw new Error(`git add failed with ${exitCode}`)
  46. }
  47. }
  48. // Run the build as a release build (that inlines version numbers etc.)
  49. async function npmRunBuild () {
  50. const npmRun = spawn('npm', ['run', 'build'], {
  51. stdio: 'inherit',
  52. env: {
  53. ...process.env,
  54. FRESH: true, // force rebuild everything
  55. IS_RELEASE_BUILD: true,
  56. },
  57. })
  58. const [exitCode] = await once(npmRun, 'exit')
  59. if (exitCode !== 0) {
  60. throw new Error(`npm run build failed with ${exitCode}`)
  61. }
  62. }
  63. async function main () {
  64. if (process.env.ENDTOEND === '1') {
  65. console.log('Publishing for e2e tests, skipping version number sync.')
  66. process.exit(0)
  67. }
  68. const message = await lastCommitMessage()
  69. if (message.trim() !== 'Release') {
  70. console.error(`Last commit is not a release commit, but '${message}'`)
  71. process.exit(1)
  72. }
  73. const files = await globby([
  74. 'README.md',
  75. 'examples/**/*.html',
  76. 'packages/*/README.md',
  77. 'packages/@uppy/*/README.md',
  78. 'website/src/docs/**',
  79. 'website/src/examples/**',
  80. 'website/themes/uppy/layout/**',
  81. '!**/node_modules/**',
  82. ])
  83. await updateVersions(files, 'uppy')
  84. await updateVersions(files, '@uppy/robodog')
  85. await updateVersions(files, '@uppy/locales')
  86. // gitignored files were updated for the npm package, but can't be updated
  87. // on git.
  88. const isIgnored = await globby.gitignore()
  89. await gitAdd(files.filter((filename) => !isIgnored(filename)))
  90. await npmRunBuild()
  91. }
  92. main().catch((err) => {
  93. console.error(err.stack)
  94. process.exit(1)
  95. })