release.mjs 6.4 KB

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