semantic-release-npm-workspaces-monorepo
Version:
Help you use semantic-release with npm workspaces
34 lines (33 loc) • 1.29 kB
JavaScript
import { applyEdits, modify } from 'jsonc-parser';
import { isDeepStrictEqual } from 'node:util';
function detectFormattingOptions(text) {
const eol = text.includes('\r\n') ? '\r\n' : '\n';
const indent = text.match(/\n([ \t]+)["{[]/);
if (!indent) {
return { tabSize: 2, insertSpaces: true, eol };
}
return { tabSize: indent[1].length, insertSpaces: indent[1][0] !== '\t', eol };
}
export function editPackageJson(rawText, original, current) {
const formattingOptions = detectFormattingOptions(rawText);
let text = rawText;
for (const depKey of ['dependencies', 'devDependencies']) {
const currentDeps = current[depKey] || {};
const originalDeps = original[depKey] || {};
for (const name in currentDeps) {
if (currentDeps[name] !== originalDeps[name]) {
const edits = modify(text, [depKey, name], currentDeps[name], {
formattingOptions,
});
text = applyEdits(text, edits);
}
}
}
if (!isDeepStrictEqual(current.release, original.release)) {
const edits = modify(text, ['release'], current.release, {
formattingOptions,
});
text = applyEdits(text, edits);
}
return text;
}