91 lines
2.3 KiB
JavaScript
91 lines
2.3 KiB
JavaScript
import fs from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
|
|
const repoRoot = process.env.BARE_DISCORD_REPO_ROOT
|
|
? path.resolve(process.env.BARE_DISCORD_REPO_ROOT)
|
|
: process.cwd();
|
|
|
|
const targetsRoot = process.env.BARE_DISCORD_PATCH_TARGETS_ROOT
|
|
? path.resolve(process.env.BARE_DISCORD_PATCH_TARGETS_ROOT)
|
|
: repoRoot;
|
|
|
|
const configPath = path.join(repoRoot, 'patches', 'runtime-dependency-patches.json');
|
|
|
|
async function read(file) {
|
|
return fs.readFile(path.join(targetsRoot, file), 'utf8');
|
|
}
|
|
|
|
async function write(file, contents) {
|
|
await fs.writeFile(path.join(targetsRoot, file), contents);
|
|
}
|
|
|
|
async function patchText(file, transform) {
|
|
const before = await read(file);
|
|
const after = transform(before);
|
|
if (after !== before) {
|
|
await write(file, after);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function decodeEscapedReplacement(value) {
|
|
return value.replace(/\\n/g, '\n').replace(/\\t/g, '\t');
|
|
}
|
|
|
|
function replaceAllVersionRanges(input) {
|
|
return input.replace(/>=v(\d+(?:\.\d+){0,2})/g, '>=$1');
|
|
}
|
|
|
|
const config = JSON.parse(await fs.readFile(configPath, 'utf8'));
|
|
|
|
let changed = 0;
|
|
let applied = 0;
|
|
|
|
const bareEngineMinimum = config.bareEngineMinimum ?? '>=1.24.0';
|
|
for (const file of config.bareEngineRelaxTargets ?? []) {
|
|
try {
|
|
const fullPath = path.join(targetsRoot, file);
|
|
const raw = await fs.readFile(fullPath, 'utf8');
|
|
const pkg = JSON.parse(raw);
|
|
if (!pkg.engines) pkg.engines = {};
|
|
const prev = pkg.engines.bare;
|
|
pkg.engines.bare = bareEngineMinimum;
|
|
if (prev !== pkg.engines.bare) {
|
|
await fs.writeFile(fullPath, JSON.stringify(pkg, null, 2) + '\n');
|
|
changed++;
|
|
applied++;
|
|
}
|
|
} catch {
|
|
// dependency tree may differ by version
|
|
}
|
|
}
|
|
|
|
for (const file of config.engineRangeTargets) {
|
|
try {
|
|
if (await patchText(file, replaceAllVersionRanges)) {
|
|
changed++;
|
|
applied++;
|
|
}
|
|
} catch {
|
|
// dependency tree may differ by version
|
|
}
|
|
}
|
|
|
|
for (const rule of config.replaceRules) {
|
|
try {
|
|
if (
|
|
await patchText(rule.file, (text) =>
|
|
text.replace(new RegExp(rule.findRegex, rule.flags ?? ''), decodeEscapedReplacement(rule.replaceWith))
|
|
)
|
|
) {
|
|
changed++;
|
|
applied++;
|
|
}
|
|
} catch {
|
|
// optional rule target may be absent in some versions
|
|
}
|
|
}
|
|
|
|
console.log(`Runtime patching complete. Rules applied: ${applied}, files changed: ${changed}`);
|