after-version-bump.js 3.1 KB

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