All files / scripts/package-release/commands changelog.js

0% Statements 0/28
0% Branches 0/10
0% Functions 0/6
0% Lines 0/27

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                                                                                                                                     
import chalk from 'chalk';
import { resolvePackage } from '../lib/workspace.js';
import { generateChangelogNotes, prependChangelog } from '../lib/changelog.js';
import { heading, info, success, error, setDryRun } from '../lib/log.js';
 
export function registerChangelogCommand(program) {
  program
    .command('changelog')
    .description('Regenerate CHANGELOG.md for a package version from a specific commit range')
    .requiredOption('--packages <list>', 'Comma-separated list of name@version entries')
    .option('--from <ref>', 'Commit range start (default: previous tag)')
    .option('--to <ref>', 'Commit range end (default: HEAD)', 'HEAD')
    .option('--dry-run', 'Print changelog content without writing')
    .action(async (opts) => {
      if (opts.dryRun) setDryRun(true);
      try {
        await runChangelog(opts);
      } catch (e) {
        error(e.message);
        process.exit(1);
      }
    });
}
 
function parsePackageVersionEntries(packagesArg) {
  return packagesArg
    .split(',')
    .map((s) => s.trim())
    .filter(Boolean)
    .map((entry) => {
      const atIdx = entry.lastIndexOf('@');
      if (atIdx <= 0) {
        error(`Invalid --packages entry "${entry}". Expected format: @scope/name@version`);
        process.exit(1);
      }
      return {
        name: entry.substring(0, atIdx),
        version: entry.substring(atIdx + 1),
      };
    });
}
 
async function runChangelog(opts) {
  const entries = parsePackageVersionEntries(opts.packages);
 
  heading('Changelog Regeneration');
 
  for (const entry of entries) {
    const pkg = resolvePackage(entry.name);
    if (!pkg) {
      error(`Package "${entry.name}" not found in workspace.`);
      process.exit(1);
    }
 
    const fromRef = opts.from || null;
    const pkgPaths = [pkg.relativePath];
 
    info(`  ${chalk.bold(entry.name)} @ ${entry.version}`);
    info(`    Range: ${chalk.dim(fromRef || 'beginning')} → ${chalk.dim(opts.to)}`);
 
    const notes = generateChangelogNotes(entry.name, entry.version, fromRef, pkgPaths);
 
    const changelogPath = prependChangelog(pkg.path, notes);
    success(`Updated: ${changelogPath}`);
  }
}