forked from YousefED/typescript-json-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-docs.js
executable file
·27 lines (22 loc) · 1 KB
/
update-docs.js
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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
function getFiles(base) {
const directory = fs.readdirSync(base, { withFileTypes: true });
const files = directory.map((dir) => {
const res = path.resolve(base, dir.name);
return dir.isDirectory() ? getFiles(res) : res;
});
return Array.prototype.concat(...files).filter((file) => file.indexOf(".ts") > 0);
}
const tests = getFiles("./test/programs");
const ensureTrailingLineBrake = (contents) => (contents[contents.length - 1] === "\n" ? contents : contents + "\n");
let source =
"_Auto-generated file. Updated with NPM deploy. Update manually with 'yarn docs'._\n\n" +
"# typescript-json-schema test examples\n\n";
tests.forEach((file) => {
const contents = fs.readFileSync(file, "utf8");
const folder = file.split("/").slice(-2)[0];
source += `## [${folder}](./test/programs/${folder})\n\n\`\`\`ts\n${ensureTrailingLineBrake(contents)}\`\`\`\n\n\n`;
});
fs.writeFileSync("./api.md", source);