All files / packages/design-system/scripts/scs scs-helpers.js

77.46% Statements 55/71
43.24% Branches 16/37
100% Functions 3/3
80.88% Lines 55/68

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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                1x       1x 1x         2x 2x       2x 2x   1x 1x   2x 2x 2x 2x 2x         2x 2x       2x   2x 2x 1x 1x 1x   1x 1x                                         3x   3x 3x   3x 3x   3x 3x   3x 3x       3x     3x 1x 1x       1x       3x                                           3x 3x 3x 3x     3x 3x 3x 3x   3x 3x 3x 3x         3x     3x 3x       3x                      
/**
 * Shared helpers for the SCS release pipeline.
 * Extracted from scs.js to enable unit testing and reduce duplication.
 */
import path from 'node:path';
import process from 'node:process';
import execa from './exec-adapter.js';
import enquirer from 'enquirer';
const { prompt } = enquirer;
 
import * as autoVersion from './version-utils.js';
 
const NEXUS_NPM_REGISTRY = 'https://nexus-proxy.repo.local.sfdc.net/nexus/content/repositories/npmjs-internal/';
const SCS_PACKAGE_NAME = 'slds-modules';
 
// Fetches latest published version from Nexus, bumps patch, and stages the updated package.json.
export function bumpScsVersionFromNexus({ monorepoRoot, scsPackageDir, gitAdd, exec = execa }) {
  let packageVersion;
  try {
    const { stdout } = exec.commandSync(
      `npm view ${SCS_PACKAGE_NAME} version --registry=${NEXUS_NPM_REGISTRY}`,
      { cwd: monorepoRoot },
    );
    packageVersion = stdout.trim();
    console.log(`\nLatest ${SCS_PACKAGE_NAME} on Nexus: ${packageVersion}`);
  } catch {
    packageVersion = autoVersion.getVersion(scsPackageDir);
    console.log(`\nCould not reach Nexus — using seeded version: ${packageVersion}`);
  }
  const newPackageVersion = autoVersion.patch(packageVersion);
  autoVersion.setVersion(newPackageVersion, scsPackageDir);
  gitAdd('package.json');
  console.log(`Bumped slds-scs: ${packageVersion} → ${newPackageVersion}`);
  return { from: packageVersion, to: newPackageVersion };
}
 
// Stages all slds-scs changes, formats JSON files, and commits with the given message.
export function commitInMonorepo(message, { monorepoRoot, gitInMonorepo, exec = execa }) {
  gitInMonorepo('git add packages/slds-scs/');
  exec.commandSync(
    'npx prettier --write packages/slds-scs/package.json packages/slds-scs/scs/files.json',
    { cwd: monorepoRoot },
  );
  gitInMonorepo('git add packages/slds-scs/package.json packages/slds-scs/scs/files.json');
 
  const { stdout: diffIndex } = exec.commandSync('git diff --cached --name-only', { cwd: monorepoRoot });
  if (diffIndex.trim()) {
    exec.sync('git', ['commit', '--no-verify', '-m', message], { cwd: monorepoRoot });
    console.log('Committed in monorepo');
    return true;
  } else {
    console.log('No changes to commit in monorepo (already up to date)');
    return false;
  }
}
 
// Exports assembled slds-scs state to the downstream repo and opens a PR for the SFCI publish pipeline.
export async function pushReleaseBranchAndOpenPR({
  branchName,
  commitMsg,
  prTitle,
  prBody,
  coreBranch,
  workItemId,
  confirmForcePush = true,
  // Injected dependencies
  scsPackageDir,
  scsCloneDir,
  monorepoRoot,
  config = {},
  exec = execa,
  promptFn = prompt,
}) {
  console.log(`\n── Phase B: Export to aura/slds-scs (${coreBranch}) ──`);
 
  exec.commandSync(`git checkout ${coreBranch}`, { cwd: scsCloneDir });
  exec.commandSync(`git reset --hard origin/${coreBranch}`, { cwd: scsCloneDir });
 
  exec.sync('rsync', ['-a', '--delete', '--exclude=.git', '--exclude=.gitignore', '--exclude=.strata.yml', `${scsPackageDir}/`, `${scsCloneDir}/`], { cwd: monorepoRoot });
  console.log('  synced packages/slds-scs/ → .scs-clone/');
 
  let remoteBranchExists = false;
  let existingPrUrl = null;
 
  try {
    const { stdout: lsRemote } = exec.commandSync(
      `git ls-remote --heads origin ${branchName}`,
      { cwd: scsCloneDir },
    );
    remoteBranchExists = lsRemote.trim().length > 0;
  } catch { /* ls-remote failed */ }
 
  if (remoteBranchExists) {
    try {
      const { stdout: prList } = exec.sync(
        'gh', ['pr', 'list', '-R', 'git.soma.salesforce.com/aura/slds-scs', '--head', branchName, '-B', coreBranch, '--json', 'url', '-q', '.[0].url'],
        { cwd: scsCloneDir },
      );
      Iif (prList.trim()) existingPrUrl = prList.trim();
    } catch { /* PR detection unavailable */ }
  }
 
  Iif (confirmForcePush && (remoteBranchExists || existingPrUrl)) {
    console.log('\n  ⚠  Existing release artifacts detected:');
    if (remoteBranchExists) console.log(`     branch: ${branchName}`);
    if (existingPrUrl) console.log(`     PR:     ${existingPrUrl}`);
 
    const forcePush = config.forcePush != null
      ? config.forcePush === true || config.forcePush === 'true'
      : (await promptFn({
          type: 'toggle',
          name: 'forcePush',
          message: 'Force-push over the existing branch (and update the PR)?',
          enabled: 'Yes, overwrite',
          disabled: 'No, abort',
          initial: false,
        })).forcePush;
 
    if (!forcePush) {
      console.log('  Aborted Phase B — existing branch/PR left untouched.');
      process.exit(0);
    }
  }
 
  try {
    exec.commandSync(`git show-ref --quiet refs/heads/${branchName}`, { cwd: scsCloneDir });
    exec.commandSync(`git branch -D ${branchName}`, { cwd: scsCloneDir });
    console.log(`  cleaned up stale local branch ${branchName}`);
  } catch { /* Branch doesn't exist locally */ }
 
  exec.commandSync(`git checkout -b ${branchName}`, { cwd: scsCloneDir });
  exec.commandSync('git add -A', { cwd: scsCloneDir });
  exec.sync('git', ['commit', '-m', commitMsg], { cwd: scsCloneDir });
  console.log(`  committed on ${branchName}`);
 
  const pushFlag = remoteBranchExists ? '--force' : '--force-with-lease';
  try {
    exec.sync('git', ['push', pushFlag, 'origin', branchName], { cwd: scsCloneDir });
    console.log(`  pushed to aura/slds-scs (${pushFlag})`);
  } catch (err) {
    console.error(`  push failed: ${err.shortMessage || err.message}`);
  }
 
  Iif (existingPrUrl) {
    console.log(`  PR already open — force-push updated it: ${existingPrUrl}`);
  } else {
    try {
      const { stdout: prUrl } = exec.sync(
        'gh', ['pr', 'create', '-t', prTitle, '-b', prBody, '-R', 'git.soma.salesforce.com/aura/slds-scs', '--head', branchName, '-B', coreBranch, '--assignee', '@me'],
        { cwd: scsCloneDir },
      );
      console.log(`  PR created: ${prUrl.trim()}`);
    } catch (err) {
      const prMatch = err.stderr?.match(/https:\/\/\S+\/pull\/\d+/);
      if (prMatch) {
        console.log(`  PR already exists: ${prMatch[0]}`);
      } else {
        console.error(`  Failed to create PR: ${err.shortMessage || err.message}`);
      }
    }
  }
}