release.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. const args = require('minimist')(process.argv.slice(2))
  2. const fs = require('fs')
  3. const path = require('path')
  4. const chalk = require('chalk')
  5. const semver = require('semver')
  6. const currentVersion = require('../package.json').version
  7. const { prompt } = require('enquirer')
  8. const execa = require('execa')
  9. const preId =
  10. args.preid ||
  11. (semver.prerelease(currentVersion) && semver.prerelease(currentVersion)[0])
  12. const isDryRun = args.dry
  13. const skipTests = args.skipTests
  14. const skipBuild = args.skipBuild
  15. const packages = fs
  16. .readdirSync(path.resolve(__dirname, '../packages'))
  17. .filter(p => !p.endsWith('.ts') && !p.startsWith('.'))
  18. const skippedPackages = []
  19. const versionIncrements = [
  20. 'patch',
  21. 'minor',
  22. 'major',
  23. ...(preId ? ['prepatch', 'preminor', 'premajor', 'prerelease'] : [])
  24. ]
  25. const inc = i => semver.inc(currentVersion, i, preId)
  26. const bin = name => path.resolve(__dirname, '../node_modules/.bin/' + name)
  27. const run = (bin, args, opts = {}) =>
  28. execa(bin, args, { stdio: 'inherit', ...opts })
  29. const dryRun = (bin, args, opts = {}) =>
  30. console.log(chalk.blue(`[dryrun] ${bin} ${args.join(' ')}`), opts)
  31. const runIfNotDry = isDryRun ? dryRun : run
  32. const getPkgRoot = pkg => path.resolve(__dirname, '../packages/' + pkg)
  33. const step = msg => console.log(chalk.cyan(msg))
  34. async function main() {
  35. let targetVersion = args._[0]
  36. if (!targetVersion) {
  37. // no explicit version, offer suggestions
  38. const { release } = await prompt({
  39. type: 'select',
  40. name: 'release',
  41. message: 'Select release type',
  42. choices: versionIncrements.map(i => `${i} (${inc(i)})`).concat(['custom'])
  43. })
  44. if (release === 'custom') {
  45. targetVersion = (
  46. await prompt({
  47. type: 'input',
  48. name: 'version',
  49. message: 'Input custom version',
  50. initial: currentVersion
  51. })
  52. ).version
  53. } else {
  54. targetVersion = release.match(/\((.*)\)/)[1]
  55. }
  56. }
  57. if (!semver.valid(targetVersion)) {
  58. throw new Error(`invalid target version: ${targetVersion}`)
  59. }
  60. const { yes } = await prompt({
  61. type: 'confirm',
  62. name: 'yes',
  63. message: `Releasing v${targetVersion}. Confirm?`
  64. })
  65. if (!yes) {
  66. return
  67. }
  68. // run tests before release
  69. step('\nRunning tests...')
  70. if (!skipTests && !isDryRun) {
  71. await run(bin('jest'), ['--clearCache'])
  72. await run('pnpm', ['test', '--bail'])
  73. } else {
  74. console.log(`(skipped)`)
  75. }
  76. // update all package versions and inter-dependencies
  77. step('\nUpdating cross dependencies...')
  78. updateVersions(targetVersion)
  79. // build all packages with types
  80. step('\nBuilding all packages...')
  81. if (!skipBuild && !isDryRun) {
  82. await run('pnpm', ['run', 'build', '--release'])
  83. // test generated dts files
  84. step('\nVerifying type declarations...')
  85. await run('pnpm', ['run', 'test-dts-only'])
  86. } else {
  87. console.log(`(skipped)`)
  88. }
  89. // generate changelog
  90. step('\nGenerating changelog...')
  91. await run(`pnpm`, ['run', 'changelog'])
  92. // update pnpm-lock.yaml
  93. step('\nUpdating lockfile...')
  94. await run(`pnpm`, ['install', '--prefer-offline'])
  95. const { stdout } = await run('git', ['diff'], { stdio: 'pipe' })
  96. if (stdout) {
  97. step('\nCommitting changes...')
  98. await runIfNotDry('git', ['add', '-A'])
  99. await runIfNotDry('git', ['commit', '-m', `release: v${targetVersion}`])
  100. } else {
  101. console.log('No changes to commit.')
  102. }
  103. // publish packages
  104. step('\nPublishing packages...')
  105. for (const pkg of packages) {
  106. await publishPackage(pkg, targetVersion, runIfNotDry)
  107. }
  108. // push to GitHub
  109. step('\nPushing to GitHub...')
  110. await runIfNotDry('git', ['tag', `v${targetVersion}`])
  111. await runIfNotDry('git', ['push', 'origin', `refs/tags/v${targetVersion}`])
  112. await runIfNotDry('git', ['push'])
  113. if (isDryRun) {
  114. console.log(`\nDry run finished - run git diff to see package changes.`)
  115. }
  116. if (skippedPackages.length) {
  117. console.log(
  118. chalk.yellow(
  119. `The following packages are skipped and NOT published:\n- ${skippedPackages.join(
  120. '\n- '
  121. )}`
  122. )
  123. )
  124. }
  125. console.log()
  126. }
  127. function updateVersions(version) {
  128. // 1. update root package.json
  129. updatePackage(path.resolve(__dirname, '..'), version)
  130. // 2. update all packages
  131. packages.forEach(p => updatePackage(getPkgRoot(p), version))
  132. }
  133. function updatePackage(pkgRoot, version) {
  134. const pkgPath = path.resolve(pkgRoot, 'package.json')
  135. const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
  136. pkg.version = version
  137. updateDeps(pkg, 'dependencies', version)
  138. updateDeps(pkg, 'peerDependencies', version)
  139. fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
  140. }
  141. function updateDeps(pkg, depType, version) {
  142. const deps = pkg[depType]
  143. if (!deps) return
  144. Object.keys(deps).forEach(dep => {
  145. if (
  146. dep === 'vue' ||
  147. (dep.startsWith('@vue') && packages.includes(dep.replace(/^@vue\//, '')))
  148. ) {
  149. console.log(
  150. chalk.yellow(`${pkg.name} -> ${depType} -> ${dep}@${version}`)
  151. )
  152. deps[dep] = version
  153. }
  154. })
  155. }
  156. async function publishPackage(pkgName, version, runIfNotDry) {
  157. if (skippedPackages.includes(pkgName)) {
  158. return
  159. }
  160. const pkgRoot = getPkgRoot(pkgName)
  161. const pkgPath = path.resolve(pkgRoot, 'package.json')
  162. const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
  163. if (pkg.private) {
  164. return
  165. }
  166. let releaseTag = null
  167. if (args.tag) {
  168. releaseTag = args.tag
  169. } else if (version.includes('alpha')) {
  170. releaseTag = 'alpha'
  171. } else if (version.includes('beta')) {
  172. releaseTag = 'beta'
  173. } else if (version.includes('rc')) {
  174. releaseTag = 'rc'
  175. }
  176. step(`Publishing ${pkgName}...`)
  177. try {
  178. await runIfNotDry(
  179. // note: use of yarn is intentional here as we rely on its publishing
  180. // behavior.
  181. 'yarn',
  182. [
  183. 'publish',
  184. '--new-version',
  185. version,
  186. ...(releaseTag ? ['--tag', releaseTag] : []),
  187. '--access',
  188. 'public'
  189. ],
  190. {
  191. cwd: pkgRoot,
  192. stdio: 'pipe'
  193. }
  194. )
  195. console.log(chalk.green(`Successfully published ${pkgName}@${version}`))
  196. } catch (e) {
  197. if (e.stderr.match(/previously published/)) {
  198. console.log(chalk.red(`Skipping already published: ${pkgName}`))
  199. } else {
  200. throw e
  201. }
  202. }
  203. }
  204. main().catch(err => {
  205. updateVersions(currentVersion)
  206. console.error(err)
  207. })