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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | /**
* Per-package post-version hooks.
*
* Configured under `releasePostVersionHooks` in `package-release.config.json`.
* Mapping: package name (full, e.g. `@salesforce-ux/design-system`) → array of
* hook objects executed in array order after `version` writes the package's
* `package.json` and `CHANGELOG.md`.
*
* Hook types:
*
* { description, type: 'regex-replace', file, find, replace, flags? }
* Substitute matches of `find` (regex source) in `file` with `replace`.
* `replace` supports placeholders. `flags` is an optional regex flags
* string (default 'g').
*
* { description, type: 'prepend-section', file, section, anchor?, skipIfPresent? }
* Prepend `section` to `file`. If `anchor` is given (regex source), the
* section is inserted immediately AFTER the first anchor match (preserves
* a leading comment block, for example). If the file is missing, it is
* created with `section` as its only content. If `skipIfPresent` (regex
* source, placeholder VALUES substituted and regex-escaped, tested with the
* 'm' flag) is given and matches the existing file, the hook is a no-op —
* makes the hook
* idempotent so a version whose {versionBase} already has a section (e.g.
* beta.2 after beta.1 in the same cycle) doesn't get a duplicate header.
*
* { description, type: 'run-script', command }
* Execute `command` (an array of args) via `execFileSync` from the repo
* root. No shell, no string interpolation. Placeholders in array elements
* are substituted before execution.
*
* Available placeholders (substituted in `replace`, `section`, and `command`
* array elements):
*
* {version} → the new version (e.g. "2.30.7", "2.266.0-beta.1")
* {versionBase} → the new version with any pre-release/build suffix
* stripped (e.g. "2.266.0-beta.1" → "2.266.0"). Use for
* fields that must carry the bare MAJOR.MINOR.PATCH only,
* such as the SCSS $app-version stamped into shipped CSS.
* {previousVersion} → the prior version (e.g. "2.30.6")
* {packageName} → full package name (e.g. "@salesforce-ux/design-system")
* {shortName} → final segment of the package name (e.g. "design-system")
* {date} → ISO date (e.g. "2026-06-16")
* {date-long} → human date (e.g. "June 16, 2026")
* {date-short} → MM-DD-YY
*/
import { execFileSync } from 'child_process';
import { readFileSync, writeFileSync, existsSync } from 'fs';
import path from 'path';
import { getConfig, getRepoRoot } from './config.js';
import { dryRunAction, info, success, warn, error } from './log.js';
const VALID_TYPES = new Set(['regex-replace', 'prepend-section', 'run-script']);
/**
* Return the hook array for a package, or [] if none configured.
*/
export function getHooksFor(packageName) {
const config = getConfig();
const all = config.releasePostVersionHooks || {};
return all[packageName] || [];
}
/**
* Run all hooks for a package. Returns an array of additional file paths
* that the caller should `git add` (file-mutating hooks contribute their
* file path; run-script hooks may also contribute files via the optional
* `tracksFiles` array).
*/
export function runHooksForPackage({ pkg, newVersion, previousVersion }) {
const hooks = getHooksFor(pkg.name);
if (hooks.length === 0) return [];
const subs = buildSubstitutions({ pkg, newVersion, previousVersion });
const touchedFiles = [];
for (let i = 0; i < hooks.length; i++) {
const hook = hooks[i];
const label = hook.description ? `"${hook.description}"` : `${hook.type} #${i + 1}`;
if (!VALID_TYPES.has(hook.type)) {
error(`releasePostVersionHooks: unknown type "${hook.type}" (${label}). ` +
`Valid: ${[...VALID_TYPES].join(', ')}`);
process.exit(1);
}
info(` hook: ${label} (${hook.type})`);
try {
const files = applyHook(hook, subs);
for (const f of files) touchedFiles.push(f);
} catch (e) {
error(`releasePostVersionHooks: hook ${label} failed: ${e.message}`);
throw e;
}
}
return touchedFiles;
}
function applyHook(hook, subs) {
switch (hook.type) {
case 'regex-replace':
return applyRegexReplace(hook, subs);
case 'prepend-section':
return applyPrependSection(hook, subs);
case 'run-script':
return applyRunScript(hook, subs);
}
}
function applyRegexReplace(hook, subs) {
if (!hook.file) throw new Error('regex-replace requires "file"');
if (!hook.find) throw new Error('regex-replace requires "find"');
if (typeof hook.replace !== 'string') throw new Error('regex-replace requires "replace"');
const filePath = path.join(getRepoRoot(), hook.file);
if (!existsSync(filePath)) {
warn(` skipped: file does not exist (${hook.file})`);
return [];
}
const flags = hook.flags || 'g';
const re = new RegExp(hook.find, flags);
const original = readFileSync(filePath, 'utf8');
const replacement = substitute(hook.replace, subs);
const updated = original.replace(re, replacement);
if (updated === original) {
warn(` skipped: no match in ${hook.file}`);
return [];
}
if (dryRunAction(`write regex-replaced ${hook.file}`)) {
return [hook.file];
}
writeFileSync(filePath, updated);
success(` updated ${hook.file}`);
return [hook.file];
}
function applyPrependSection(hook, subs) {
if (!hook.file) throw new Error('prepend-section requires "file"');
if (typeof hook.section !== 'string') throw new Error('prepend-section requires "section"');
const filePath = path.join(getRepoRoot(), hook.file);
const section = substitute(hook.section, subs);
let updated;
if (!existsSync(filePath)) {
updated = section.endsWith('\n') ? section : `${section}\n`;
} else {
const original = readFileSync(filePath, 'utf8');
// Idempotency guard. `skipIfPresent` (regex source, placeholder values
// substituted and regex-escaped) is tested against the file; a match means
// this section is already present, so the hook does nothing. Escaping the
// substituted value keeps a version like 2.266.0 from matching literally
// ("2\.266\.0"), not as a dot-wildcard. Without the guard, prepend-section is
// NOT idempotent: on the 2nd+ beta of a release cycle {versionBase} is the
// bare MAJOR.MINOR.PATCH (identical for beta.1 and beta.2), so a header
// keyed on {versionBase} would be prepended a second time — the duplicate
// "## 2.266.0 …" header bug seen in the 266 run.
if (hook.skipIfPresent) {
const guard = new RegExp(substituteRegex(hook.skipIfPresent, subs), 'm');
if (guard.test(original)) {
warn(` skipped: skipIfPresent matched in ${hook.file} (section already present)`);
return [];
}
}
if (hook.anchor) {
const re = new RegExp(hook.anchor, 's');
const match = original.match(re);
if (!match) {
warn(` skipped: anchor regex did not match in ${hook.file}`);
return [];
}
const insertAt = match.index + match[0].length;
const sectionWithSpacing = `\n\n${section.replace(/^\n+|\n+$/g, '')}\n`;
updated = original.slice(0, insertAt) + sectionWithSpacing + original.slice(insertAt);
} else {
updated = `${section.replace(/\n+$/, '')}\n\n${original}`;
}
}
if (dryRunAction(`prepend section to ${hook.file}`)) {
return [hook.file];
}
writeFileSync(filePath, updated);
success(` prepended section to ${hook.file}`);
return [hook.file];
}
function applyRunScript(hook, subs) {
if (!Array.isArray(hook.command) || hook.command.length === 0) {
throw new Error('run-script requires "command" (non-empty array of args)');
}
const cmd = hook.command.map((arg) => {
if (typeof arg !== 'string') {
throw new Error(`run-script command args must be strings; got ${typeof arg}`);
}
return substitute(arg, subs);
});
const [program, ...rest] = cmd;
if (dryRunAction(`run-script ${cmd.join(' ')}`)) {
return Array.isArray(hook.tracksFiles) ? hook.tracksFiles : [];
}
try {
execFileSync(program, rest, {
cwd: getRepoRoot(),
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'pipe'],
});
} catch (e) {
const detail = (e.stderr || e.stdout || e.message || '').toString().trim();
throw new Error(`${program} ${rest.join(' ')} failed: ${detail.slice(0, 500)}`, { cause: e });
}
success(` ran ${program} ${rest.join(' ')}`);
return Array.isArray(hook.tracksFiles) ? hook.tracksFiles : [];
}
function buildSubstitutions({ pkg, newVersion, previousVersion }) {
const now = new Date();
const iso = now.toISOString().split('T')[0];
const monthNames = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December',
];
const long = `${monthNames[now.getMonth()]} ${now.getDate()}, ${now.getFullYear()}`;
const mm = String(now.getMonth() + 1).padStart(2, '0');
const dd = String(now.getDate()).padStart(2, '0');
const yy = String(now.getFullYear()).slice(-2);
const short = `${mm}-${dd}-${yy}`;
return {
// {versionBase} MUST precede {version}: substitute() replaces keys in order
// and "{version}" is a prefix of "{versionBase}", so replacing {version}
// first would corrupt an un-replaced {versionBase} token.
'{versionBase}': String(newVersion || '').split('+')[0].split('-')[0],
'{version}': newVersion,
'{previousVersion}': previousVersion || '',
'{packageName}': pkg.name,
'{shortName}': pkg.name.split('/').pop(),
'{date}': iso,
'{date-long}': long,
'{date-short}': short,
};
}
function substitute(text, subs) {
let out = text;
for (const [key, val] of Object.entries(subs)) {
out = out.split(key).join(val);
}
return out;
}
/**
* Regex-safe variant of substitute(): escapes each substituted VALUE so its
* characters match literally when the result is used as a RegExp source. The
* template's own authored regex syntax (anchors, character classes, etc.) is
* left intact — only the interpolated placeholder values are escaped. Without
* this, a value like "2.266.0" would let its dots act as wildcards and also
* match "2X266X0". Used for `skipIfPresent`, the one placeholder-bearing regex
* source.
*/
function substituteRegex(text, subs) {
let out = text;
for (const [key, val] of Object.entries(subs)) {
out = out.split(key).join(escapeRegExp(String(val)));
}
return out;
}
function escapeRegExp(s) {
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
|