release.js 2.9 KB

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