after-version-bump.js 3.4 KB

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