blueprint-shared-templates 0.6.2 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/transform.mjs DELETED
@@ -1,63 +0,0 @@
1
- // The text conversions that turn a template file into the file of a repository. No dependency, so the same file runs
2
- // from the published package (blueprint-shared-templates/transform.mjs) and from the repository that holds the templates.
3
- // fillFields @@field@@ -> the value the organization gives the field
4
- // roleBlocks `# @if <role>` ... `# @end` blocks, kept only for the roles the repository has
5
- // publicRunners the self-hosted runner labels of a public repository -> GitHub-hosted ones
6
- // fieldsIn the fields a text uses
7
- // What the facts generate (the merge configuration, the release-please configuration, the unit list of feature.yml) is
8
- // not a text conversion and stays with the code that composes a repository.
9
-
10
- // The fields of the templates, the same list as the fact blueprint-shared.templates.fields (a test compares them).
11
- export const FIELDS = ["aggregatePackageName", "aggregateRegistry", "clientIdVariable", "licenseHolder", "mergeBotApp", "mergeBotUserId", "mergeLabel", "organization", "privateKeySecret", "repoName", "scope"];
12
-
13
- const FIELD = /@@(\w+)@@/g;
14
-
15
- // The fields a text uses, distinct and sorted.
16
- export function fieldsIn(text) {
17
- return [...new Set([...text.matchAll(FIELD)].map((m) => m[1]))].toSorted();
18
- }
19
-
20
- // Writes the value of every @@field@@. A field that is not in FIELDS, or one `values` has no value for, is an error that
21
- // names the file and the field: a placeholder left in a repository would be written as it is.
22
- export function fillFields(text, values, file = "template") {
23
- return text.replace(FIELD, (_, name) => {
24
- if (!FIELDS.includes(name)) throw new Error(`${file}: unknown field @@${name}@@ (the fields are ${FIELDS.join(", ")})`);
25
- const value = values?.[name];
26
- if (typeof value !== "string") throw new Error(`${file}: no value for field @@${name}@@`);
27
- return value;
28
- });
29
- }
30
-
31
- // Blocks of a template that depend on the repository's roles. A line `# @if <role>` or `# @if !<role>` opens a block and
32
- // `# @end` closes it; the marker lines are always dropped, and the lines between them stay only when the repository has
33
- // the role (or, with `!`, does not have it). Blocks do not nest. This is how ci.yml has the policy check and the facts
34
- // scripts only in a repository with the release role, which is the only role that ships those scripts (decision
35
- // ci-scope-source-only). `# @if a+b` is kept only when the repository has every role joined by `+`: blocks do not
36
- // nest, so this is how a line that depends on two roles is written.
37
- export function roleBlocks(text, roles, file = "workflow") {
38
- const out = [];
39
- let keep = null;
40
- for (const line of text.split("\n")) {
41
- const open = line.match(/^\s*# @if (!?)([\w+-]+)\s*$/);
42
- if (open) {
43
- if (keep !== null) throw new Error(`${file}: "# @if" inside another block`);
44
- keep = open[2].split("+").every((r) => roles.includes(r)) !== (open[1] === "!");
45
- } else if (/^\s*# @end\s*$/.test(line)) {
46
- if (keep === null) throw new Error(`${file}: "# @end" without "# @if"`);
47
- keep = null;
48
- } else if (keep !== false) out.push(line);
49
- }
50
- if (keep !== null) throw new Error(`${file}: "# @if" without "# @end"`);
51
- return out.join("\n");
52
- }
53
-
54
- // A public repository runs every job on a GitHub-hosted runner (blueprint-shared.git-flow.runner-rule-public-repo): every
55
- // self-hosted runner label becomes ubuntu-latest (macos-latest for the macOS one), and the line
56
- // `# 獨立發布 job:{理由}` above a runs-on, which explains why a private repository runs one job on a hosted runner,
57
- // is dropped with the label change (check-workflows public-runner).
58
- export function publicRunners(text) {
59
- return text
60
- .replace(/self-hosted-linux(?:-vm)?/g, "ubuntu-latest")
61
- .replace(/self-hosted-macos-lite/g, "macos-latest")
62
- .replace(/^[ \t]*# 獨立發布 job:[^\n]*\n(?=[ \t]*runs-on:)/gm, "");
63
- }