Skip to content

Commit 026fe28

Browse files
JS script to copy ADRs automatically
The script copies ADRs from the `doc/adr` folder into the web-doc project, so that HTML pages can be generated for them. The script runs automatically when the user starts the project with `yarn dev`. Before copying the files the script compares folders and only copies folders and files that are missing, or that were modified. The script runs synchronously, so that the project only starts after the ADRs have been copied. Singed-off-by: Rodrigo Pinto <rodrigo.pinto@calian.ca>
1 parent d93ea53 commit 026fe28

3 files changed

Lines changed: 127 additions & 14 deletions

File tree

doc/web-doc/README.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,47 @@
22

33
## Installation
44

5-
Requirements: NodeJS version 16 or above.
5+
Requirements: NodeJS on the same version as the one required for [Theia IDE][theia-prereq].
66

77
```bash
88
cd doc/web-doc
99
yarn
1010
```
1111

12+
> Executing `yarn` from the root of `theia-trace-extension` does not install the dependencies in `doc/web-doc`. The configuration for Lerna to support such a feature may be added in the future.
13+
1214
Run site
1315

1416
```bash
1517
yarn dev
18+
```
19+
20+
Or start the server and open the app in a new browser tab
1621

17-
# or start the server and open the app in a new browser tab
22+
```bash
1823
yarn dev --open
1924
```
2025

21-
The site will be running at `http://localhost:5173`.
26+
The site will be running at [localhost, port 5173].
2227

23-
## Code style
28+
## Development
2429

25-
Run Prettier from the root.
30+
Run Prettier from the root of the web-doc project (at `doc/web-doc/` from the root of `theia-trace-extension`) using the following commands:
2631

27-
Check code style:
32+
To check code style:
2833

29-
yarn lint
34+
```bash
35+
yarn lint
36+
```
3037

31-
Fix code style:
38+
To fix code style:
3239

33-
yarn format
40+
```bash
41+
yarn format
42+
```
3443

35-
## Development
44+
Website created with [SvelteKit].
3645

37-
Website created with [SvelteKit](https://kit.svelte.dev)
46+
[theia-prereq]: https://github.com/eclipse-theia/theia/blob/master/doc/Developing.md#prerequisites
47+
[sveltekit]: https://kit.svelte.dev
48+
[localhost, port 5173]: http://localhost:5173

doc/web-doc/doc-loader.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import fs from 'fs-extra';
2+
import { compareSync } from 'dir-compare';
3+
import { once } from 'node:events';
4+
import { createInterface } from 'node:readline';
5+
6+
const src = '../adr/';
7+
const dest = 'doc-sources/adr-source/';
8+
9+
const errorCallback = (err) => {
10+
if (err) throw err;
11+
};
12+
13+
const comparisonOptions = {
14+
excludeFilter: '.DS_Store',
15+
compareSize: true,
16+
compareContent: true
17+
};
18+
19+
// Sanitizer of markdown files
20+
21+
const sanitizer = (chunk) => {
22+
return chunk.replace(/{/g, '&#123;').replace(/}/g, '&#125;');
23+
};
24+
25+
const processLineByLine = async (file) => {
26+
try {
27+
let result = '';
28+
let mermaid = false;
29+
let mermaidBlock = '';
30+
31+
const rl = createInterface({
32+
input: file,
33+
crlfDelay: Infinity
34+
});
35+
36+
rl.on('line', (line) => {
37+
if (mermaid) {
38+
mermaidBlock += line + '\n';
39+
40+
if (line.includes('```')) {
41+
mermaid = false;
42+
result += sanitizer(mermaidBlock);
43+
mermaidBlock = '';
44+
}
45+
} else {
46+
result += line + '\n';
47+
}
48+
49+
if (line.includes('```mermaid')) {
50+
mermaid = true;
51+
}
52+
});
53+
54+
await once(rl, 'close');
55+
56+
return result;
57+
} catch (err) {
58+
console.error(err);
59+
}
60+
};
61+
62+
// Compare folders
63+
64+
if (!fs.existsSync('doc-sources')) {
65+
fs.mkdirSync('doc-sources');
66+
fs.mkdirSync(dest);
67+
} else if (!fs.existsSync(dest)) {
68+
fs.mkdirSync(dest);
69+
}
70+
71+
const comparison = compareSync(src, dest, comparisonOptions);
72+
73+
// Copy missing files and folders
74+
75+
if (!comparison.same) {
76+
comparison.diffSet.forEach(async (dif) => {
77+
if ((dif.state === 'left' || dif.state === 'distinct') && dif.path1 === src) {
78+
if (dif.type1 === 'file' && dif.name1.slice(-3) === '.md') {
79+
// Remove outdated file
80+
if (dif.state === 'distinct') {
81+
fs.rmSync(dest + dif.name2, { force: true })
82+
}
83+
84+
// Sanitize Markdown files
85+
const file = fs.createReadStream(src + dif.name1, 'utf8');
86+
const sanitized = await processLineByLine(file);
87+
fs.writeFileSync(dest + dif.name1, sanitized.toString());
88+
} else {
89+
// Copy
90+
fs.copySync(src + dif.name1, dest + dif.name1, { recursive: true }, errorCallback);
91+
}
92+
}
93+
});
94+
}

doc/web-doc/package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
"version": "0.0.1",
44
"private": true,
55
"scripts": {
6-
"dev": "vite dev",
6+
"dev": "node doc-loader;vite dev",
77
"build": "vite build",
88
"preview": "vite preview",
99
"lint": "prettier --plugin-search-dir . --check . && eslint .",
10-
"format": "prettier --plugin-search-dir . --write ."
10+
"format": "prettier --plugin-search-dir . --write .",
11+
"load": "node doc-loader"
1112
},
1213
"devDependencies": {
1314
"@sveltejs/adapter-auto": "^1.0.0",
@@ -20,5 +21,12 @@
2021
"svelte": "^3.54.0",
2122
"vite": "^4.0.0"
2223
},
23-
"type": "module"
24+
"type": "module",
25+
"dependencies": {
26+
"@ysuzuki19/remark-mermaid": "^1.0.2",
27+
"dir-compare": "^4.0.0",
28+
"fs-extra": "^11.1.0",
29+
"mdsvex": "^0.10.6",
30+
"mermaid": "^9.3.0"
31+
}
2432
}

0 commit comments

Comments
 (0)