-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathscript.ts
More file actions
51 lines (38 loc) · 1.58 KB
/
Copy pathscript.ts
File metadata and controls
51 lines (38 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import fs from "node:fs";
const JOINER = "\n\n[`⬆ BACK TO TOP ⬆`](#table-of-contents)";
const CODE_ADDRESS = "[EXAMPLE-FILE-ADDRESS]";
const DOCS_DIRECTORY = "./Documentations";
function wrapInTypeScriptBlock(content: string) {
const PREFIX = "```typescript";
const SUFFIX = "```";
return `${PREFIX}\n${content}\n${SUFFIX}`;
}
/**
* `readdirSync` returns entries in filesystem order, which is alphabetical on
* macOS but arbitrary on Linux. The documents must be concatenated in the order
* of their numeric prefix, otherwise the README comes out scrambled on CI.
*/
function getDocumentFileNames(): Array<string> {
const leadingNumberOf = (fileName: string) => Number.parseInt(fileName, 10);
return fs
.readdirSync(DOCS_DIRECTORY)
.filter((fileName) => fileName.endsWith(".md"))
.sort((first, second) => leadingNumberOf(first) - leadingNumberOf(second) || first.localeCompare(second));
}
const contents = getDocumentFileNames().map((fileName) => {
const fileContent = fs.readFileSync(`${DOCS_DIRECTORY}/${fileName}`, "utf-8");
const lines = fileContent.split("\n");
const newLines = lines.map((line) => {
if (line.startsWith(CODE_ADDRESS)) {
const codeFileAddress = line.replace(CODE_ADDRESS, "").slice(1, -1);
const code = fs.readFileSync(`.${codeFileAddress}`, "utf-8");
return wrapInTypeScriptBlock(code);
} else {
return line;
}
});
const joinedLines = newLines.join("\n").trim();
return `${joinedLines}${JOINER}`;
});
const fullDocumentation = contents.join(`\n\n`);
fs.writeFileSync("./README.md", fullDocumentation);