Skip to content

Commit e1f430c

Browse files
Convert scripts to TS
1 parent 5cad481 commit e1f430c

File tree

5 files changed

+99
-5
lines changed

5 files changed

+99
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"scripts": {
77
"build": "tsc",
88
"buildJson": "npx tsx scripts/generate-json-definitions.ts",
9+
"buildDiagrams": "npx tsx scripts/generate-diagrams.ts",
910
"validateGeneration": "npx tsx scripts/validate-json-generated.ts",
1011
"test": "jest"
1112
},

scripts/generate-diagrams.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import fs from "fs";
2+
import path from "path";
3+
import { fileURLToPath } from "url";
4+
import { walkDir, isJsonFile, getMermaidOutputPath } from "./utils";
5+
import logger from "./log-styling";
6+
7+
const __filename = fileURLToPath(import.meta.url);
8+
const __dirname = path.dirname(__filename);
9+
10+
const INPUT_DIR = path.resolve(__dirname, "../generated/json");
11+
const OUTPUT_DIR = path.resolve(__dirname, "../generated/mermaid");
12+
13+
type Event = {
14+
id: string;
15+
label: string;
16+
next?: string;
17+
success?: string;
18+
error?: string;
19+
};
20+
21+
function toMermaid(events: Event[]): string {
22+
const lines: string[] = ["graph TD"];
23+
24+
for (const event of events) {
25+
const { id, label, next, success, error } = event;
26+
27+
lines.push(`${id}["${label}"]`);
28+
if (next) {
29+
lines.push(`${id} --> ${next}`);
30+
}
31+
if (success) {
32+
lines.push(`${id} -->|Success| ${success}`);
33+
}
34+
if (error) {
35+
lines.push(`${id} -->|Error| ${error}`);
36+
}
37+
}
38+
39+
return lines.join("\n");
40+
}
41+
42+
function generateDiagram(filePath: string): void {
43+
const relativePath = path.relative(INPUT_DIR, filePath);
44+
logger.info(`Generating diagram for: ${relativePath}`);
45+
46+
const outputFile = getMermaidOutputPath(filePath, INPUT_DIR, OUTPUT_DIR);
47+
fs.mkdirSync(path.dirname(outputFile), { recursive: true });
48+
49+
const events: Event[] = JSON.parse(fs.readFileSync(filePath, "utf-8"));
50+
const output = toMermaid(events);
51+
52+
fs.writeFileSync(outputFile, output);
53+
}
54+
55+
if (!fs.existsSync(INPUT_DIR)) {
56+
logger.warn(`Generated directory is missing: ${INPUT_DIR}`);
57+
process.exit(0);
58+
}
59+
60+
logger.heading("Generating Mermaid diagrams from JSON files...");
61+
logger.blank();
62+
63+
walkDir(INPUT_DIR, (filePath: string) => {
64+
if (isJsonFile(filePath)) {
65+
generateDiagram(filePath);
66+
} else {
67+
logger.warn(
68+
`Skipping unsupported file: ${path.relative(INPUT_DIR, filePath)}`
69+
);
70+
}
71+
});
72+
73+
logger.blank();
74+
logger.success("Mermaid diagrams generated successfully!");

scripts/generate-json-definitions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
walkDir,
77
isJsonnetFile,
88
isLibSonnetFile,
9-
getOutputPath,
9+
getJsonOutputPath,
1010
} from "./utils";
1111
import logger from "./log-styling";
1212

@@ -18,7 +18,7 @@ const OUTPUT_DIR = path.resolve(__dirname, "../generated/json");
1818

1919
function buildJsonnetFile(inputPath: string): void {
2020
const relativePath = path.relative(INPUT_DIR, inputPath);
21-
const outputFile = getOutputPath(inputPath, INPUT_DIR, OUTPUT_DIR);
21+
const outputFile = getJsonOutputPath(inputPath, INPUT_DIR, OUTPUT_DIR);
2222

2323
fs.mkdirSync(path.dirname(outputFile), { recursive: true });
2424

scripts/utils.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,33 @@ export function isLibSonnetFile(file: string) {
2929
return file.endsWith(".libsonnet");
3030
}
3131

32+
/**
33+
* Determines if a file is a valid JSON file
34+
*/
35+
export function isJsonFile(file: string) {
36+
return file.endsWith(".json");
37+
}
38+
3239
/**
3340
* Converts a .jsonnet input path to a .json output path.
3441
*/
35-
export function getOutputPath(
42+
export function getJsonOutputPath(
3643
inputPath: string,
3744
inputRoot: string,
3845
outputRoot: string
3946
) {
4047
const relativePath = path.relative(inputRoot, inputPath);
4148
return path.join(outputRoot, relativePath.replace(/\.jsonnet$/, ".json"));
4249
}
50+
51+
/**
52+
* Converts a .json input path to a .mmd output path.
53+
*/
54+
export function getMermaidOutputPath(
55+
inputPath: string,
56+
inputRoot: string,
57+
outputRoot: string
58+
) {
59+
const relativePath = path.relative(inputRoot, inputPath);
60+
return path.join(outputRoot, relativePath.replace(/\.json$/, ".mmd"));
61+
}

scripts/validate-json-generated.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "fs";
22
import path from "path";
33
import { fileURLToPath } from "url";
44
import logger from "./log-styling";
5-
import { walkDir, isJsonnetFile, getOutputPath } from "./utils";
5+
import { walkDir, isJsonnetFile, getJsonOutputPath } from "./utils";
66

77
const __filename = fileURLToPath(import.meta.url);
88
const __dirname = path.dirname(__filename);
@@ -20,7 +20,7 @@ const missingFiles: string[] = [];
2020
walkDir(INPUT_DIR, (inputPath: string) => {
2121
if (isJsonnetFile(inputPath)) {
2222
const relativePath = path.relative(INPUT_DIR, inputPath);
23-
const expectedOutputPath = getOutputPath(inputPath, INPUT_DIR, OUTPUT_DIR);
23+
const expectedOutputPath = getJsonOutputPath(inputPath, INPUT_DIR, OUTPUT_DIR);
2424

2525
if (!fs.existsSync(expectedOutputPath)) {
2626
missingFiles.push(relativePath);

0 commit comments

Comments
 (0)