Skip to content

Commit d92d5fe

Browse files
committed
more sketching
1 parent da4030d commit d92d5fe

4 files changed

Lines changed: 72 additions & 39 deletions

File tree

.github/workflows/lint.yml

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,5 @@ jobs:
2323
run: npm install
2424
- name: Run the linter
2525
run: ./lint
26-
- name: Create the diagram
27-
run: ./diagram > diagram.md
28-
- name: Save the diagram
29-
run: |
30-
git add diagram.md
31-
git status
32-
git remote -v
33-
new_branch="${{ github.ref_name }}-diagram"
34-
git branch -D "${new_branch}" || :
35-
git checkout -b "${new_branch}"
36-
git config user.name "${{ github.workflow }}"
37-
git config user.email "github-actions@example.com"
38-
git commit -m "Diagram saved by lint.yml workflow"
39-
git push --set-upstream "$( git remote )" --force "${new_branch}"
26+
- name: Check the diagram
27+
run: ./diagram --check

diagram

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
#!/bin/bash
22

3-
set -euo pipefail
3+
if [ $# -eq 1 -a "$1" == "--check" ] ; then
4+
node diagram.js
5+
git add diagram.md
6+
if ! git diff --cached --exit-code diagram.md ; then
7+
echo "Changes detected in diagram.md. Please run './diagram --write' and commit the results."
8+
exit 1
9+
fi
10+
exit 0
11+
fi
412

5-
cat <<MERMAID
6-
\`\`\`mermaid
7-
graph TD;
8-
A-->B;
9-
A-->C;
10-
B-->D;
11-
C-->D;
12-
D-->E;
13-
E["generated at $( date )"]
14-
\`\`\`
15-
MERMAID
13+
if [ $# -eq 1 -a "$1" == "--write" ] ; then
14+
node diagram.js
15+
git add diagram.md
16+
git status diagram.md
17+
exit 0
18+
fi
19+
20+
echo "Usage: './diagram --write' or './diagram --check'" >&2
21+
exit 1

diagram.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use strict";
2+
13
import { exec } from "node:child_process";
24
import * as path from "node:path";
35

@@ -73,10 +75,9 @@ const makeResolver = (filenameToNodeId) => {
7375
};
7476
};
7577

76-
const main = async () => {
77-
const markdownFilenames = await findMarkdownFiles();
78-
const links = await scanForLinks(markdownFilenames);
79-
const trimmed = localMarkdownToMarkdownLinks(links);
78+
const buildGraph = (markdownFilenames, trimmed) => {
79+
const nodeLines = [];
80+
const edgeLines = [];
8081

8182
const filenameToNodeId = new Map(
8283
markdownFilenames.map((filename, index) => [filename, `id${index}`]),
@@ -86,12 +87,8 @@ const main = async () => {
8687
const targetResolver = makeResolver(filenameToNodeId);
8788
const seenEdges = new Set();
8889

89-
console.log("```mermaid");
90-
// console.log("graph TD;"); // top-down
91-
console.log("graph LR;"); // left-right
92-
9390
for (const [filename, nodeId] of filenameToNodeId.entries()) {
94-
console.log(` ${nodeId}[${JSON.stringify(filename)}]`);
91+
nodeLines.push(` ${nodeId}[${JSON.stringify(filename)}]`);
9592
}
9693

9794
for (const links of trimmed) {
@@ -102,20 +99,32 @@ const main = async () => {
10299

103100
if (r.isNew) {
104101
const label = `?? ${r.resolved} ??`;
105-
console.log(` ${r.id}[${JSON.stringify(label)}]`);
102+
nodeLines.push(` ${r.id}[${JSON.stringify(label)}]`);
106103
}
107104

108105
const edgeKey = `${fromNodeId} to ${r.id}`;
109106
if (!seenEdges.has(edgeKey)) {
110-
console.log(` ${fromNodeId}-->${r.id}`);
107+
edgeLines.push(` ${fromNodeId}-->${r.id}`);
111108
seenEdges.add(edgeKey);
112109
}
113110
}
114111
}
115112

116-
console.log("```");
113+
// Stable output
114+
return [...nodeLines.toSorted(), ...edgeLines.toSorted()];
115+
};
116+
117+
const main = async () => {
118+
const markdownFilenames = await findMarkdownFiles();
119+
const links = await scanForLinks(markdownFilenames);
120+
const trimmed = localMarkdownToMarkdownLinks(links);
121+
122+
const graphLines = buildGraph(markdownFilenames, trimmed);
123+
const graphContent =
124+
["```mermaid", "graph LR;", ...graphLines, "```"].join("\n") + "\n";
117125

118-
// console.dir({ trimmed, e: [...m.entries()] }, { depth: 5 });
126+
await writeFile("diagram.md", graphContent, "utf-8");
127+
console.log("Wrote diagram.md");
119128
};
120129

121130
main().catch((error) => {

diagram.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
```mermaid
2+
graph LR;
3+
id0["courses/Backend/README.md"]
4+
id10["shared-modules/JavaScript/legacy-js1-week1.md"]
5+
id11["shared-modules/JavaScript/legacy-js1-week2.md"]
6+
id12["shared-modules/JavaScript/legacy-js1-week3.md"]
7+
id13["shared-modules/JavaScript/legacy-js1-week4.md"]
8+
id14["shared-modules/JavaScript/legacy-js2-week1.md"]
9+
id15["shared-modules/JavaScript/legacy-js2-week2.md"]
10+
id16["shared-modules/JavaScript/legacy-js2-week3.md"]
11+
id17["shared-modules/JavaScript/legacy-js3-week1.md"]
12+
id18["shared-modules/JavaScript/legacy-js3-week2.md"]
13+
id19["shared-modules/JavaScript/legacy-js3-week3.md"]
14+
id1["courses/Foundation/HTML-CSS/Pre-course/readme.md"]
15+
id20["shared-modules/README.md"]
16+
id21["shared-modules/legacy-git1.md"]
17+
id22["shared-modules/legacy-git2.md"]
18+
id2["courses/Foundation/HTML-CSS/The-one-and-only-week1/readme.md"]
19+
id3["courses/Foundation/HTML-CSS/first-homework-submission.md"]
20+
id4["courses/Foundation/HTML-CSS/homework.md"]
21+
id5["courses/Foundation/HTML-CSS/readme.md"]
22+
id6["courses/Foundation/README.md"]
23+
id7["courses/Frontend/README.md"]
24+
id8["courses/Pre-Course/README.md"]
25+
id9["diagram.md"]
26+
id5-->id1
27+
id5-->id2
28+
id5-->id4
29+
id6-->id5
30+
```

0 commit comments

Comments
 (0)