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

0% Statements 0/47
0% Branches 0/26
0% Functions 0/6
0% Lines 0/46

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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117                                                                                                                                                                                                                                         
import chalk from 'chalk';
import { enforceBranchGuardrail } from '../lib/branch.js';
import { gitTag, gitPushTag, revParse, tagExists } from '../lib/git.js';
import { getConfig } from '../lib/config.js';
import { heading, info, success, warn, error, setDryRun } from '../lib/log.js';
 
export function registerTagCommand(program) {
  program
    .command('tag')
    .description('Create annotated git tags for package releases')
    .requiredOption(
      '--packages <list>',
      'Comma-separated list of name@version entries (e.g. @salesforce-ux/design-system@2.31.0)',
    )
    .option('--sha <ref>', 'Target SHA (default: HEAD)', 'HEAD')
    .option('--tag-format <format>', 'Tag name format (default: {name}@{version})')
    .option('--tag-message <message>', 'Tag message format (default: {tag})')
    .option('--no-annotate', 'Create lightweight tag instead (not recommended)')
    .option('--force', 'Replace existing tag')
    .option('--skip-existing', 'Silently skip tags that already exist (idempotent for CI re-runs)')
    .option('--push', 'Push tags to origin after creation')
    .option('--allow-branch <pattern>', 'Additive branch pattern override')
    .option('--no-branch-check', 'Bypass branch guardrail entirely')
    .option('--dry-run', 'Print planned actions without executing')
    .action(async (opts) => {
      if (opts.dryRun) setDryRun(true);
      try {
        await runTag(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 or name@version`);
        process.exit(1);
      }
      return {
        name: entry.substring(0, atIdx),
        version: entry.substring(atIdx + 1),
      };
    });
}
 
async function runTag(opts) {
  if (opts.branchCheck !== false) {
    enforceBranchGuardrail('tag', {
      noBranchCheck: false,
      allowBranch: opts.allowBranch,
    });
  }
 
  const config = getConfig();
  const entries = parsePackageVersionEntries(opts.packages);
  const tagFormat = opts.tagFormat || config.tagFormat;
  const tagMessageFormat = opts.tagMessage || config.tagMessage;
  const annotate = opts.annotate !== false;
  const sha = opts.sha;
 
  if (!annotate) {
    warn(
      '--no-annotate: lightweight tags do not carry author/date metadata and break release parity with Lerna.',
    );
  }
 
  heading('Tag Plan');
 
  const resolvedSha = revParse(sha);
  info(`Target SHA: ${chalk.cyan(resolvedSha)} (${sha})`);
  info('');
 
  const tags = [];
  for (const entry of entries) {
    const tagName = tagFormat.replace('{name}', entry.name).replace('{version}', entry.version);
    const tagMsg = tagMessageFormat.replace('{tag}', tagName);
 
    tags.push({ tagName, tagMsg, entry });
    info(`  ${chalk.bold(tagName)} → ${chalk.dim(resolvedSha.substring(0, 10))}`);
  }
 
  if (opts.force && opts.skipExisting) {
    error('--force and --skip-existing are mutually exclusive.');
    process.exit(1);
  }
 
  heading('Creating tags');
 
  for (const { tagName, tagMsg } of tags) {
    if (opts.skipExisting && tagExists(tagName)) {
      info(`Skipping ${tagName}: tag already exists.`);
      continue;
    }
 
    gitTag(tagName, {
      sha,
      message: tagMsg,
      annotate,
      force: opts.force || false,
    });
    success(`Tagged: ${tagName}`);
 
    if (opts.push) {
      gitPushTag(tagName, { force: opts.force || false });
      success(`Pushed: ${tagName}`);
    }
  }
}