after-version-bump.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 { spawn } = require('child_process')
  15. const { readFile, writeFile } = require('fs/promises')
  16. const once = require('events.once')
  17. const globby = require('globby')
  18. async function replaceInFile (filename, replacements) {
  19. let content = await readFile(filename, 'utf8')
  20. for (const [rx, replacement] of replacements) {
  21. content = content.replace(rx, replacement)
  22. }
  23. await writeFile(filename, content, 'utf8')
  24. }
  25. async function updateVersions (files, packageName) {
  26. const { version } = require(`../packages/${packageName}/package.json`)
  27. // uppy → uppy
  28. // @uppy/robodog → uppy/robodog
  29. const urlPart = packageName === 'uppy' ? packageName : packageName.slice(1)
  30. const replacements = new Map([
  31. [RegExp(`${urlPart}/v\\d+\\.\\d+\\.\\d+\\/`, 'g'), `${urlPart}/v${version}/`],
  32. // maybe more later
  33. ])
  34. console.log('replacing', replacements, 'in', files.length, 'files')
  35. for (const f of files) {
  36. // eslint-disable-next-line no-await-in-loop
  37. await replaceInFile(f, replacements)
  38. }
  39. }
  40. async function gitAdd (files) {
  41. const git = spawn('git', ['add', ...files], { stdio: 'inherit' })
  42. const [exitCode] = await once(git, 'exit')
  43. if (exitCode !== 0) {
  44. throw new Error(`git add failed with ${exitCode}`)
  45. }
  46. }
  47. // Run the build as a release build (that inlines version numbers etc.)
  48. async function npmRunBuild () {
  49. const npmRun = spawn('yarn', ['run', 'build'], {
  50. stdio: 'inherit',
  51. env: {
  52. ...process.env,
  53. FRESH: true, // force rebuild everything
  54. IS_RELEASE_BUILD: true,
  55. },
  56. })
  57. const [exitCode] = await once(npmRun, 'exit')
  58. if (exitCode !== 0) {
  59. throw new Error(`yarn run build failed with ${exitCode}`)
  60. }
  61. }
  62. async function main () {
  63. if (process.env.ENDTOEND === '1') {
  64. console.log('Publishing for e2e tests, skipping version number sync.')
  65. process.exit(0)
  66. }
  67. const files = await globby([
  68. 'README.md',
  69. 'BUNDLE-README.md',
  70. 'examples/**/*.html',
  71. 'packages/*/README.md',
  72. 'packages/@uppy/*/README.md',
  73. 'website/src/docs/**',
  74. 'website/src/examples/**',
  75. 'website/themes/uppy/layout/**',
  76. '!**/node_modules/**',
  77. ])
  78. await updateVersions(files, 'uppy')
  79. await updateVersions(files, '@uppy/robodog')
  80. await updateVersions(files, '@uppy/locales')
  81. // gitignored files were updated for the npm package, but can't be updated
  82. // on git.
  83. const isIgnored = await globby.gitignore()
  84. await gitAdd(files.filter((filename) => !isIgnored(filename)))
  85. await npmRunBuild()
  86. }
  87. main().catch((err) => {
  88. console.error(err.stack)
  89. process.exit(1)
  90. })