Skip to content

Commit 0ea8490

Browse files
committed
Switch from js-yaml to yaml for better output formatting
1 parent a85af80 commit 0ea8490

File tree

3 files changed

+41
-46
lines changed

3 files changed

+41
-46
lines changed

pr-checks/package-lock.json

Lines changed: 16 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pr-checks/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
"private": true,
33
"description": "Dependencies for the sync.ts",
44
"dependencies": {
5-
"js-yaml": "^4.1.1"
5+
"yaml": "^2.8.2"
66
},
77
"devDependencies": {
8-
"tsx": "^4.21.0",
9-
"typescript": "^5.9.3",
108
"@types/node": "^20.19.9",
11-
"@types/js-yaml": "^4.0.9"
9+
"tsx": "^4.21.0",
10+
"typescript": "^5.9.3"
1211
}
1312
}

pr-checks/sync.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import * as fs from "fs";
44
import * as path from "path";
55

6-
import * as yaml from "js-yaml";
6+
import * as yaml from "yaml";
77

88
/**
99
* Represents workflow input definitions.
@@ -84,30 +84,31 @@ const CHECKS_DIR = path.join(THIS_DIR, "checks");
8484
const OUTPUT_DIR = path.join(THIS_DIR, "new-output");
8585

8686
/**
87-
* Loads and parses a YAML file as a `Specification`.
87+
* Loads and parses a YAML file.
8888
*/
89-
function loadYaml(filePath: string): Specification {
89+
function loadYaml(filePath: string): yaml.Document {
9090
const content = fs.readFileSync(filePath, "utf8");
91-
return yaml.load(content) as Specification;
91+
return yaml.parseDocument(content);
9292
}
9393

9494
/**
9595
* Serialize a value to YAML and write it to a file, prepended with the
9696
* standard header comment.
9797
*/
98-
function writeYaml(filePath: string, data: any): void {
98+
function writeYaml(filePath: string, workflow: any): void {
9999
const header = `# Warning: This file is generated automatically, and should not be modified.
100100
# Instead, please modify the template in the pr-checks directory and run:
101101
# pr-checks/sync.sh
102102
# to regenerate this file.
103103
104104
`;
105-
const yamlStr = yaml.dump(data, {
106-
indent: 2,
107-
lineWidth: -1, // Don't wrap long lines
108-
noRefs: true, // Don't use YAML anchors/aliases
109-
quotingType: "'", // Use single quotes where quoting is needed
110-
forceQuotes: false,
105+
const workflowDoc = new yaml.Document(workflow, {
106+
aliasDuplicateObjects: false,
107+
});
108+
const yamlStr = yaml.stringify(workflowDoc, {
109+
aliasDuplicateObjects: false,
110+
singleQuote: true,
111+
lineWidth: 0,
111112
});
112113
fs.writeFileSync(filePath, stripTrailingWhitespace(header + yamlStr), "utf8");
113114
}
@@ -156,7 +157,8 @@ function main(): void {
156157

157158
for (const file of checkFiles) {
158159
const checkName = path.basename(file, ".yml");
159-
const checkSpecification = loadYaml(file);
160+
const specDocument = loadYaml(file);
161+
const checkSpecification = specDocument.toJS() as Specification;
160162

161163
console.log(`Processing: ${checkName} — "${checkSpecification.name}"`);
162164

@@ -361,7 +363,9 @@ function main(): void {
361363
});
362364
}
363365

364-
steps.push(...checkSpecification.steps);
366+
// Extract the sequence of steps from the YAML document to persist as much formatting as possible.
367+
const specSteps = specDocument.get("steps") as yaml.YAMLSeq;
368+
specSteps.items.unshift(...steps);
365369

366370
const checkJob: Record<string, any> = {
367371
strategy: {
@@ -378,7 +382,7 @@ function main(): void {
378382
},
379383
"timeout-minutes": 45,
380384
"runs-on": "${{ matrix.os }}",
381-
steps,
385+
steps: specSteps,
382386
};
383387

384388
if (checkSpecification.permissions) {
@@ -414,6 +418,9 @@ function main(): void {
414418
extraGroupName += "-${{inputs." + inputName + "}}";
415419
}
416420

421+
const cron = new yaml.Scalar("0 5 * * *");
422+
cron.type = yaml.Scalar.QUOTE_SINGLE;
423+
417424
const workflow = {
418425
name: `PR Check - ${checkSpecification.name}`,
419426
env: {
@@ -430,7 +437,7 @@ function main(): void {
430437
merge_group: {
431438
types: ["checks_requested"],
432439
},
433-
schedule: [{ cron: "0 5 * * *" }],
440+
schedule: [{ cron }],
434441
workflow_dispatch: {
435442
inputs: workflowInputs,
436443
},

0 commit comments

Comments
 (0)