|
| 1 | +import { relative } from 'node:path'; |
| 2 | +import { log, colorize } from '../utils/logger.ts'; |
| 3 | +import { loadConfig } from '../core/config.ts'; |
| 4 | +import { discoverWorkspace } from '../core/workspace.ts'; |
| 5 | +import { readChangesets } from '../core/changeset.ts'; |
| 6 | +import { tryRun } from '../utils/shell.ts'; |
| 7 | +import type { WorkspacePackage } from '../types.ts'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Local check: detect which packages have changed on this branch |
| 11 | + * and verify they have corresponding changesets. |
| 12 | + * Designed for pre-push hooks — no GitHub API needed. |
| 13 | + */ |
| 14 | +export async function checkCommand(rootDir: string): Promise<void> { |
| 15 | + const config = await loadConfig(rootDir); |
| 16 | + const { packages } = await discoverWorkspace(rootDir, config); |
| 17 | + const changesets = await readChangesets(rootDir); |
| 18 | + |
| 19 | + // Find which packages already have changesets |
| 20 | + const coveredPackages = new Set<string>(); |
| 21 | + for (const cs of changesets) { |
| 22 | + for (const release of cs.releases) { |
| 23 | + coveredPackages.add(release.name); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + // Find which packages have changed on this branch vs base |
| 28 | + const baseBranch = config.baseBranch; |
| 29 | + const changedFiles = getChangedFiles(rootDir, baseBranch); |
| 30 | + |
| 31 | + if (changedFiles.length === 0) { |
| 32 | + log.info('No changed files detected.'); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + const changedPackages = findChangedPackages(changedFiles, packages, rootDir); |
| 37 | + |
| 38 | + if (changedPackages.length === 0) { |
| 39 | + log.info('No managed packages have changed.'); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + // Check which changed packages are missing changesets |
| 44 | + const missing = changedPackages.filter((name) => !coveredPackages.has(name)); |
| 45 | + |
| 46 | + if (missing.length === 0) { |
| 47 | + log.success(`All ${changedPackages.length} changed package(s) have changesets.`); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // Report missing |
| 52 | + log.warn(`${missing.length} changed package(s) missing changesets:\n`); |
| 53 | + for (const name of missing) { |
| 54 | + console.log(` ${colorize(name, 'yellow')}`); |
| 55 | + } |
| 56 | + console.log(); |
| 57 | + log.dim('Run `bumpy add` to create a changeset, or `bumpy add --empty` if no release is needed.'); |
| 58 | + process.exit(1); |
| 59 | +} |
| 60 | + |
| 61 | +/** Get files changed on this branch compared to the base branch */ |
| 62 | +function getChangedFiles(rootDir: string, baseBranch: string): string[] { |
| 63 | + // Try merge-base first (works on branches) |
| 64 | + const mergeBase = tryRun(`git merge-base HEAD origin/${baseBranch}`, { cwd: rootDir }); |
| 65 | + const ref = mergeBase || `origin/${baseBranch}`; |
| 66 | + const diff = tryRun(`git diff --name-only ${ref}`, { cwd: rootDir }); |
| 67 | + if (!diff) return []; |
| 68 | + return diff.split('\n').filter(Boolean); |
| 69 | +} |
| 70 | + |
| 71 | +/** Map changed files to the packages they belong to */ |
| 72 | +function findChangedPackages( |
| 73 | + changedFiles: string[], |
| 74 | + packages: Map<string, WorkspacePackage>, |
| 75 | + rootDir: string, |
| 76 | +): string[] { |
| 77 | + const changed = new Set<string>(); |
| 78 | + |
| 79 | + for (const file of changedFiles) { |
| 80 | + for (const [name, pkg] of packages) { |
| 81 | + const pkgRelDir = relative(rootDir, pkg.dir); |
| 82 | + if (file.startsWith(pkgRelDir + '/')) { |
| 83 | + changed.add(name); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return [...changed]; |
| 89 | +} |
0 commit comments