release.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const fs = require('fs')
  2. const path = require('path')
  3. const chalk = require('chalk')
  4. const semver = require('semver')
  5. const { prompt } = require('enquirer')
  6. const execa = require('execa')
  7. const currentVersion = require('../package.json').version
  8. const versionIncrements = [
  9. 'patch',
  10. 'minor',
  11. 'major'
  12. ]
  13. const tags = [
  14. 'latest',
  15. 'next'
  16. ]
  17. const inc = (i) => semver.inc(currentVersion, i)
  18. const bin = (name) => path.resolve(__dirname, `../node_modules/.bin/${name}`)
  19. const run = (bin, args, opts = {}) => execa(bin, args, { stdio: 'inherit', ...opts })
  20. const step = (msg) => console.log(chalk.cyan(msg))
  21. async function main() {
  22. let targetVersion
  23. const { release } = await prompt({
  24. type: 'select',
  25. name: 'release',
  26. message: 'Select release type',
  27. choices: versionIncrements.map(i => `${i} (${inc(i)})`).concat(['custom'])
  28. })
  29. if (release === 'custom') {
  30. targetVersion = (await prompt({
  31. type: 'input',
  32. name: 'version',
  33. message: 'Input custom version',
  34. initial: currentVersion
  35. })).version
  36. } else {
  37. targetVersion = release.match(/\((.*)\)/)[1]
  38. }
  39. if (!semver.valid(targetVersion)) {
  40. throw new Error(`Invalid target version: ${targetVersion}`)
  41. }
  42. const { tag } = await prompt({
  43. type: 'select',
  44. name: 'tag',
  45. message: 'Select tag type',
  46. choices: tags
  47. })
  48. console.log(tag)
  49. const { yes } = await prompt({
  50. type: 'confirm',
  51. name: 'yes',
  52. message: `Releasing v${targetVersion} with the "${tag}" tag. Confirm?`
  53. })
  54. if (!yes) {
  55. return
  56. }
  57. // Run tests before release.
  58. step('\nRunning tests...')
  59. await run('yarn', ['test'])
  60. // Update the package version.
  61. step('\nUpdating the package version...')
  62. updatePackage(targetVersion)
  63. // Build the package.
  64. step('\nBuilding the package...')
  65. await run('yarn', ['build'])
  66. // Generate the changelog.
  67. step('\nGenerating the changelog...')
  68. await run('yarn', ['changelog'])
  69. // Commit changes to the Git.
  70. step('\nCommitting changes...')
  71. await run('git', ['add', '-A'])
  72. await run('git', ['commit', '-m', `release: v${targetVersion}`])
  73. // Publish the package.
  74. step('\nPublishing the package...')
  75. await run ('yarn', [
  76. 'publish', '--tag', tag, '--new-version', targetVersion, '--no-commit-hooks',
  77. '--no-git-tag-version'
  78. ])
  79. // Push to GitHub.
  80. step('\nPushing to GitHub...')
  81. await run('git', ['tag', `v${targetVersion}`])
  82. await run('git', ['push', 'origin', `refs/tags/v${targetVersion}`])
  83. await run('git', ['push'])
  84. }
  85. function updatePackage(version) {
  86. const pkgPath = path.resolve(path.resolve(__dirname, '..'), 'package.json')
  87. const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
  88. pkg.version = version
  89. fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
  90. }
  91. main().catch((err) => console.error(err))