release.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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: tagOk } = await prompt({
  50. type: 'confirm',
  51. name: 'yes',
  52. message: `Releasing v${targetVersion} with the "${tag}" tag. Confirm?`
  53. })
  54. if (!tagOk) {
  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. const { yes: changelogOk } = await prompt({
  70. type: 'confirm',
  71. name: 'yes',
  72. message: `Changelog generated. Does it look good?`
  73. })
  74. if (!changelogOk) {
  75. return
  76. }
  77. // Commit changes to the Git.
  78. step('\nCommitting changes...')
  79. await run('git', ['add', '-A'])
  80. await run('git', ['commit', '-m', `release: v${targetVersion}`])
  81. // Publish the package.
  82. step('\nPublishing the package...')
  83. await run ('yarn', [
  84. 'publish', '--tag', tag, '--new-version', targetVersion, '--no-commit-hooks',
  85. '--no-git-tag-version'
  86. ])
  87. // Push to GitHub.
  88. step('\nPushing to GitHub...')
  89. await run('git', ['tag', `v${targetVersion}`])
  90. await run('git', ['push', 'origin', `refs/tags/v${targetVersion}`])
  91. await run('git', ['push'])
  92. }
  93. function updatePackage(version) {
  94. const pkgPath = path.resolve(path.resolve(__dirname, '..'), 'package.json')
  95. const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
  96. pkg.version = version
  97. fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
  98. }
  99. main().catch((err) => console.error(err))