| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- // @ts-check
- import minimist from 'minimist'
- import fs from 'node:fs'
- import path from 'node:path'
- import chalk from 'chalk'
- import semver from 'semver'
- import enquirer from 'enquirer'
- import execa from 'execa'
- import { createRequire } from 'node:module'
- import { fileURLToPath } from 'node:url'
- const { prompt } = enquirer
- const currentVersion = createRequire(import.meta.url)('../package.json').version
- const __dirname = path.dirname(fileURLToPath(import.meta.url))
- const args = minimist(process.argv.slice(2))
- const preId = args.preid || semver.prerelease(currentVersion)?.[0]
- const isDryRun = args.dry
- let skipTests = args.skipTests
- const skipBuild = args.skipBuild
- const packages = fs
- .readdirSync(path.resolve(__dirname, '../packages'))
- .filter(p => !p.endsWith('.ts') && !p.startsWith('.'))
- const skippedPackages = []
- const versionIncrements = [
- 'patch',
- 'minor',
- 'major',
- ...(preId ? ['prepatch', 'preminor', 'premajor', 'prerelease'] : [])
- ]
- const inc = i => semver.inc(currentVersion, i, preId)
- const run = (bin, args, opts = {}) =>
- execa(bin, args, { stdio: 'inherit', ...opts })
- const dryRun = (bin, args, opts = {}) =>
- console.log(chalk.blue(`[dryrun] ${bin} ${args.join(' ')}`), opts)
- const runIfNotDry = isDryRun ? dryRun : run
- const getPkgRoot = pkg => path.resolve(__dirname, '../packages/' + pkg)
- const step = msg => console.log(chalk.cyan(msg))
- async function main() {
- let targetVersion = args._[0]
- if (!targetVersion) {
- // no explicit version, offer suggestions
- // @ts-ignore
- const { release } = await prompt({
- type: 'select',
- name: 'release',
- message: 'Select release type',
- choices: versionIncrements.map(i => `${i} (${inc(i)})`).concat(['custom'])
- })
- if (release === 'custom') {
- const result = await prompt({
- type: 'input',
- name: 'version',
- message: 'Input custom version',
- initial: currentVersion
- })
- // @ts-ignore
- targetVersion = result.version
- } else {
- targetVersion = release.match(/\((.*)\)/)[1]
- }
- }
- if (!semver.valid(targetVersion)) {
- throw new Error(`invalid target version: ${targetVersion}`)
- }
- // @ts-ignore
- const { yes: confirmRelease } = await prompt({
- type: 'confirm',
- name: 'yes',
- message: `Releasing v${targetVersion}. Confirm?`
- })
- if (!confirmRelease) {
- return
- }
- step('Checking CI status for HEAD...')
- let isCIPassed = true
- try {
- const { stdout: sha } = await execa('git', ['rev-parse', 'HEAD'])
- const res = await fetch(
- `https://api.github.com/repos/vuejs/core/actions/runs?head_sha=${sha}` +
- `&status=success&exclude_pull_requests=true`
- )
- const data = await res.json()
- isCIPassed = data.workflow_runs.length > 0
- } catch (e) {
- isCIPassed = false
- }
- if (isCIPassed) {
- // @ts-ignore
- const { yes: promptSkipTests } = await prompt({
- type: 'confirm',
- name: 'yes',
- message: `CI for this commit passed. Skip local tests?`
- })
- if (promptSkipTests) {
- skipTests = true
- }
- }
- if (!skipTests) {
- step('\nRunning tests...')
- if (!isDryRun) {
- await run('pnpm', ['test', 'run'])
- } else {
- console.log(`Skipped (dry run)`)
- }
- } else {
- step('Tests skipped.')
- }
- // update all package versions and inter-dependencies
- step('\nUpdating cross dependencies...')
- updateVersions(targetVersion)
- // build all packages with types
- step('\nBuilding all packages...')
- if (!skipBuild && !isDryRun) {
- await run('pnpm', ['run', 'build'])
- step('\nBuilding and testing types...')
- await run('pnpm', ['test-dts'])
- } else {
- console.log(`(skipped)`)
- }
- // generate changelog
- step('\nGenerating changelog...')
- await run(`pnpm`, ['run', 'changelog'])
- // update pnpm-lock.yaml
- step('\nUpdating lockfile...')
- await run(`pnpm`, ['install', '--prefer-offline'])
- const { stdout } = await run('git', ['diff'], { stdio: 'pipe' })
- if (stdout) {
- step('\nCommitting changes...')
- await runIfNotDry('git', ['add', '-A'])
- await runIfNotDry('git', ['commit', '-m', `release: v${targetVersion}`])
- } else {
- console.log('No changes to commit.')
- }
- // publish packages
- step('\nPublishing packages...')
- for (const pkg of packages) {
- await publishPackage(pkg, targetVersion, runIfNotDry)
- }
- // push to GitHub
- step('\nPushing to GitHub...')
- await runIfNotDry('git', ['tag', `v${targetVersion}`])
- await runIfNotDry('git', ['push', 'origin', `refs/tags/v${targetVersion}`])
- await runIfNotDry('git', ['push'])
- if (isDryRun) {
- console.log(`\nDry run finished - run git diff to see package changes.`)
- }
- if (skippedPackages.length) {
- console.log(
- chalk.yellow(
- `The following packages are skipped and NOT published:\n- ${skippedPackages.join(
- '\n- '
- )}`
- )
- )
- }
- console.log()
- }
- function updateVersions(version) {
- // 1. update root package.json
- updatePackage(path.resolve(__dirname, '..'), version)
- // 2. update all packages
- packages.forEach(p => updatePackage(getPkgRoot(p), version))
- }
- function updatePackage(pkgRoot, version) {
- const pkgPath = path.resolve(pkgRoot, 'package.json')
- const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
- pkg.version = version
- updateDeps(pkg, 'dependencies', version)
- updateDeps(pkg, 'peerDependencies', version)
- fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
- }
- function updateDeps(pkg, depType, version) {
- const deps = pkg[depType]
- if (!deps) return
- Object.keys(deps).forEach(dep => {
- if (deps[dep] === 'workspace:*') {
- return
- }
- if (
- dep === 'vue' ||
- (dep.startsWith('@vue') && packages.includes(dep.replace(/^@vue\//, '')))
- ) {
- console.log(
- chalk.yellow(`${pkg.name} -> ${depType} -> ${dep}@${version}`)
- )
- deps[dep] = version
- }
- })
- }
- async function publishPackage(pkgName, version, runIfNotDry) {
- if (skippedPackages.includes(pkgName)) {
- return
- }
- const pkgRoot = getPkgRoot(pkgName)
- const pkgPath = path.resolve(pkgRoot, 'package.json')
- const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
- if (pkg.private) {
- return
- }
- let releaseTag = null
- if (args.tag) {
- releaseTag = args.tag
- } else if (version.includes('alpha')) {
- releaseTag = 'alpha'
- } else if (version.includes('beta')) {
- releaseTag = 'beta'
- } else if (version.includes('rc')) {
- releaseTag = 'rc'
- }
- step(`Publishing ${pkgName}...`)
- try {
- await runIfNotDry(
- // note: use of yarn is intentional here as we rely on its publishing
- // behavior.
- 'yarn',
- [
- 'publish',
- '--new-version',
- version,
- ...(releaseTag ? ['--tag', releaseTag] : []),
- '--access',
- 'public'
- ],
- {
- cwd: pkgRoot,
- stdio: 'pipe'
- }
- )
- console.log(chalk.green(`Successfully published ${pkgName}@${version}`))
- } catch (e) {
- if (e.stderr.match(/previously published/)) {
- console.log(chalk.red(`Skipping already published: ${pkgName}`))
- } else {
- throw e
- }
- }
- }
- main().catch(err => {
- updateVersions(currentVersion)
- console.error(err)
- })
|