release.mjs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. let 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 run = (bin, args, opts = {}) =>
  31. execa(bin, args, { stdio: 'inherit', ...opts })
  32. const dryRun = (bin, args, opts = {}) =>
  33. console.log(chalk.blue(`[dryrun] ${bin} ${args.join(' ')}`), opts)
  34. const runIfNotDry = isDryRun ? dryRun : run
  35. const getPkgRoot = pkg => path.resolve(__dirname, '../packages/' + pkg)
  36. const step = msg => console.log(chalk.cyan(msg))
  37. async function main() {
  38. let targetVersion = args._[0]
  39. if (!targetVersion) {
  40. // no explicit version, offer suggestions
  41. // @ts-ignore
  42. const { release } = await prompt({
  43. type: 'select',
  44. name: 'release',
  45. message: 'Select release type',
  46. choices: versionIncrements.map(i => `${i} (${inc(i)})`).concat(['custom'])
  47. })
  48. if (release === 'custom') {
  49. const result = await prompt({
  50. type: 'input',
  51. name: 'version',
  52. message: 'Input custom version',
  53. initial: currentVersion
  54. })
  55. // @ts-ignore
  56. targetVersion = result.version
  57. } else {
  58. targetVersion = release.match(/\((.*)\)/)[1]
  59. }
  60. }
  61. if (!semver.valid(targetVersion)) {
  62. throw new Error(`invalid target version: ${targetVersion}`)
  63. }
  64. // @ts-ignore
  65. const { yes: confirmRelease } = await prompt({
  66. type: 'confirm',
  67. name: 'yes',
  68. message: `Releasing v${targetVersion}. Confirm?`
  69. })
  70. if (!confirmRelease) {
  71. return
  72. }
  73. step('Checking CI status for HEAD...')
  74. let isCIPassed = true
  75. try {
  76. const { stdout: sha } = await execa('git', ['rev-parse', 'HEAD'])
  77. const res = await fetch(
  78. `https://api.github.com/repos/vuejs/core/actions/runs?head_sha=${sha}` +
  79. `&status=success&exclude_pull_requests=true`
  80. )
  81. const data = await res.json()
  82. isCIPassed = data.workflow_runs.length > 0
  83. } catch (e) {
  84. isCIPassed = false
  85. }
  86. if (isCIPassed) {
  87. // @ts-ignore
  88. const { yes: promptSkipTests } = await prompt({
  89. type: 'confirm',
  90. name: 'yes',
  91. message: `CI for this commit passed. Skip local tests?`
  92. })
  93. if (promptSkipTests) {
  94. skipTests = true
  95. }
  96. }
  97. if (!skipTests) {
  98. step('\nRunning tests...')
  99. if (!isDryRun) {
  100. await run('pnpm', ['test'])
  101. await run('pnpm', ['test-dts'])
  102. } else {
  103. console.log(`Skipped (dry run)`)
  104. }
  105. } else {
  106. step('Tests skipped.')
  107. }
  108. // update all package versions and inter-dependencies
  109. step('\nUpdating cross dependencies...')
  110. updateVersions(targetVersion)
  111. // build all packages with types
  112. step('\nBuilding all packages...')
  113. if (!skipBuild && !isDryRun) {
  114. await run('pnpm', ['run', 'build', '--release'])
  115. await run('pnpm', ['run', 'build-dts'])
  116. // test generated dts files
  117. step('\nVerifying type declarations...')
  118. await run('pnpm', ['run', 'test-dts-only'])
  119. } else {
  120. console.log(`(skipped)`)
  121. }
  122. // generate changelog
  123. step('\nGenerating changelog...')
  124. await run(`pnpm`, ['run', 'changelog'])
  125. // update pnpm-lock.yaml
  126. step('\nUpdating lockfile...')
  127. await run(`pnpm`, ['install', '--prefer-offline'])
  128. const { stdout } = await run('git', ['diff'], { stdio: 'pipe' })
  129. if (stdout) {
  130. step('\nCommitting changes...')
  131. await runIfNotDry('git', ['add', '-A'])
  132. await runIfNotDry('git', ['commit', '-m', `release: v${targetVersion}`])
  133. } else {
  134. console.log('No changes to commit.')
  135. }
  136. // publish packages
  137. step('\nPublishing packages...')
  138. for (const pkg of packages) {
  139. await publishPackage(pkg, targetVersion, runIfNotDry)
  140. }
  141. // push to GitHub
  142. step('\nPushing to GitHub...')
  143. await runIfNotDry('git', ['tag', `v${targetVersion}`])
  144. await runIfNotDry('git', ['push', 'origin', `refs/tags/v${targetVersion}`])
  145. await runIfNotDry('git', ['push'])
  146. if (isDryRun) {
  147. console.log(`\nDry run finished - run git diff to see package changes.`)
  148. }
  149. if (skippedPackages.length) {
  150. console.log(
  151. chalk.yellow(
  152. `The following packages are skipped and NOT published:\n- ${skippedPackages.join(
  153. '\n- '
  154. )}`
  155. )
  156. )
  157. }
  158. console.log()
  159. }
  160. function updateVersions(version) {
  161. // 1. update root package.json
  162. updatePackage(path.resolve(__dirname, '..'), version)
  163. // 2. update all packages
  164. packages.forEach(p => updatePackage(getPkgRoot(p), version))
  165. }
  166. function updatePackage(pkgRoot, version) {
  167. const pkgPath = path.resolve(pkgRoot, 'package.json')
  168. const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
  169. pkg.version = version
  170. updateDeps(pkg, 'dependencies', version)
  171. updateDeps(pkg, 'peerDependencies', version)
  172. fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
  173. }
  174. function updateDeps(pkg, depType, version) {
  175. const deps = pkg[depType]
  176. if (!deps) return
  177. Object.keys(deps).forEach(dep => {
  178. if (
  179. dep === 'vue' ||
  180. (dep.startsWith('@vue') && packages.includes(dep.replace(/^@vue\//, '')))
  181. ) {
  182. console.log(
  183. chalk.yellow(`${pkg.name} -> ${depType} -> ${dep}@${version}`)
  184. )
  185. deps[dep] = version
  186. }
  187. })
  188. }
  189. async function publishPackage(pkgName, version, runIfNotDry) {
  190. if (skippedPackages.includes(pkgName)) {
  191. return
  192. }
  193. const pkgRoot = getPkgRoot(pkgName)
  194. const pkgPath = path.resolve(pkgRoot, 'package.json')
  195. const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
  196. if (pkg.private) {
  197. return
  198. }
  199. let releaseTag = null
  200. if (args.tag) {
  201. releaseTag = args.tag
  202. } else if (version.includes('alpha')) {
  203. releaseTag = 'alpha'
  204. } else if (version.includes('beta')) {
  205. releaseTag = 'beta'
  206. } else if (version.includes('rc')) {
  207. releaseTag = 'rc'
  208. }
  209. step(`Publishing ${pkgName}...`)
  210. try {
  211. await runIfNotDry(
  212. // note: use of yarn is intentional here as we rely on its publishing
  213. // behavior.
  214. 'yarn',
  215. [
  216. 'publish',
  217. '--new-version',
  218. version,
  219. ...(releaseTag ? ['--tag', releaseTag] : []),
  220. '--access',
  221. 'public'
  222. ],
  223. {
  224. cwd: pkgRoot,
  225. stdio: 'pipe'
  226. }
  227. )
  228. console.log(chalk.green(`Successfully published ${pkgName}@${version}`))
  229. } catch (e) {
  230. if (e.stderr.match(/previously published/)) {
  231. console.log(chalk.red(`Skipping already published: ${pkgName}`))
  232. } else {
  233. throw e
  234. }
  235. }
  236. }
  237. main().catch(err => {
  238. updateVersions(currentVersion)
  239. console.error(err)
  240. })