All files / scripts cli.ts

0% Statements 0/58
100% Branches 1/1
100% Functions 1/1
0% Lines 0/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                                                                                                                                                 
import type { VersionBumpResults } from 'bumpp'
import path from 'node:path'
import process from 'node:process'
import chalk from 'chalk'
import enquirer from 'enquirer'
import { x } from 'tinyexec'
import { generateChangelog } from './changelog'
import { squashLastNCommits } from './git-utils'
import { gitCommit } from './git-utils/commit'
import GitTagParser from './git-utils/tags'
import { detectMonorepo } from './monorepo/detect'
import { findPackages } from './monorepo/packages'
import { prefix } from './utils/cli-utilities'
import { selectPackages } from './utils/enquirer'
import { createFile } from './utils/file'
import logger from './utils/logger'
import { bumpVersion } from './version/bump'
 
async function main() {
  console.log(chalk.blue('Checking monorepo structure...'))
  const monorepo = await detectMonorepo()
  if (!monorepo) {
    console.log(chalk.red('Not a pnpm monorepo project'))
    return process.exit(1)
  }
 
  const allPackages = await findPackages(monorepo)
  const publishable = allPackages.filter(p => !p.private)
  const selected = await selectPackages(publishable)
 
  // Bump versions
  const { confirm_dump } = await enquirer.prompt<{ confirm_dump: boolean }>({
    type: 'confirm',
    name: 'confirm_dump',
    prefix,
    message: 'Ready to dump version?',
  })
  if (confirm_dump !== true) {
    return logger.info(`You have canceled dump version`)
  }
 
  const bumpCache: Record<string, VersionBumpResults> = {}
  for (const pkg of selected) {
    logger.info(`Bumping version for ${chalk.bold(pkg.name)} ...`)
    const res = await bumpVersion(pkg, `${pkg.name}@%s`)
    bumpCache[pkg.name] = res
    // 创建当前更新日志
    const parser = new GitTagParser()
    await parser.fetchTags()
    const lastTag = parser.lastTag
    if (lastTag && lastTag === res.tag) {
      const prevTag = (await parser.getPreviousTag(lastTag, true))?.tag ?? parser.firstTag
      const commits = await parser.getCommitsBetweenTags(lastTag, prevTag, true)
      const changelog = generateChangelog(commits, lastTag)
      const changelogPath = createFile(path.join(pkg.path, 'changelog.md'), changelog, { prepend: true })
      await x('npx', ['eslint', '--fix', changelogPath])
      await gitCommit([changelogPath], `chore(changelog): ${lastTag}`)
      await squashLastNCommits(2)
    }
  }
  if (selected.length) {
    await squashLastNCommits(selected.length)
  }
 
  // Publish
  // const { confirm } = await enquirer.prompt({
  //   type: 'confirm',
  //   name: 'confirm',
  //   message: 'Ready to publish packages?',
  // })
 
  // if (confirm) {
  //   for (const pkg of selected) {
  //     console.log(chalk.blue(`Publishing ${pkg.name}...`))
  //     await publishPackage(pkg)
  //   }
  // }
}
 
main().catch(console.error)