-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrenovate.js
More file actions
55 lines (47 loc) · 1.66 KB
/
Copy pathrenovate.js
File metadata and controls
55 lines (47 loc) · 1.66 KB
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
import { readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { ROOT_DIR } from "./constants.js";
const RENOVATE_CONFIG_PATH = join(ROOT_DIR, ".github", "renovate.json");
const START_MARKER = "// BEGIN GENERATED SYNC EXAMPLES";
const END_MARKER = "// END GENERATED SYNC EXAMPLES";
/**
* Emit every generated `matchFileNames` entry with a trailing comma so the
* marker region stays self-contained JSONC whether or not hand-written entries
* follow the END marker before the closing `]`.
*
* @param {string[]} exampleNames
*/
export function generatedMatchFileNames(exampleNames) {
return [
`\t\t\t\t${START_MARKER}`,
...exampleNames.map((name) => `\t\t\t\t"examples/${name}/**",`),
`\t\t\t\t${END_MARKER}`,
].join("\n");
}
/**
* Keep Renovate's generated-example exclusion in sync with playground metadata.
*/
export function syncRenovateConfig(exampleNames, checkOnly = false) {
const content = readFileSync(RENOVATE_CONFIG_PATH, "utf8");
const markerStart = content.indexOf(START_MARKER);
const start = content.lastIndexOf("\n", markerStart) + 1;
const end = content.indexOf(END_MARKER);
if (markerStart === -1 || end === -1 || end < start) {
throw new Error(
`Renovate config must contain ${START_MARKER} and ${END_MARKER}`,
);
}
const generated = generatedMatchFileNames([...new Set(exampleNames)].sort());
const current = content.slice(start, end + END_MARKER.length);
const updated =
content.slice(0, start) +
generated +
content.slice(end + END_MARKER.length);
if (checkOnly) {
return current !== generated;
}
if (current !== generated) {
writeFileSync(RENOVATE_CONFIG_PATH, updated);
}
return false;
}