-
Notifications
You must be signed in to change notification settings - Fork 953
/
Copy pathgenerate-readmes.ts
executable file
·83 lines (72 loc) · 2.61 KB
/
generate-readmes.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env node
const fs = require("fs-extra");
const { glob } = require("glob");
const path = require("path");
const { loadJsonFileSync } = require("load-json-file");
const yaml = require("yamljs");
(async () => {
// documentation v14 has moved to ESM so need to import as if async, and wrap
// in an IIFE as top level async not allowed.
const documentation = await import("documentation");
/**
* When firing `npm run docs`:
* - inside a module, only the docs of that module will be generated
* - outside or at the root level it will generate docs for all modules
*/
const currentFolder = process.cwd().split(path.sep).pop() as string;
const packages = currentFolder.includes("packages/turf-")
? [path.join(process.cwd(), "package.json")]
: glob.sync(
path.join(__dirname, "..", "packages", "turf-*", "package.json")
);
// Template for README Markdown
const postfix = fs.readFileSync(path.join(__dirname, "postfix.md"), "utf8");
const paths = yaml.parse(
fs.readFileSync(path.join(__dirname, "..", "documentation.yml"), "utf8")
).paths;
packages.forEach((packagePath) => {
const directory = path.parse(packagePath).dir;
let indexPath = path.join(directory, "index.js");
const pckg = loadJsonFileSync(packagePath);
const name = pckg.name;
const diagrams = glob
.sync(path.join(directory, "diagrams", "*"))
.filter(isImage);
// some of the packages are typescript instead
if (!fs.existsSync(indexPath)) {
indexPath = path.join(directory, "index.ts");
}
// Build Documentation
documentation
.build(indexPath, { shallow: true })
.then((res) => {
if (res === undefined) return console.warn(packagePath);
console.log("Building Docs: " + name);
// Format Markdown
documentation.formats
.md(res, { paths })
.then((markdown) => {
markdown = `# ${name}\n\n${markdown}${postfix.replace(
/{module}/,
name
)}`;
if (diagrams.length)
markdown += "\n\n### Diagrams\n\n" + diagramToMarkdown(diagrams);
fs.writeFileSync(path.join(directory, "README.md"), markdown);
})
.catch((error) => console.warn(error));
})
.catch((error) => console.warn(error));
});
})();
function isImage(image) {
return [".gif", ".jpg", ".png"].indexOf(path.parse(image).ext) !== -1;
}
function diagramToMarkdown(diagrams) {
return diagrams
.map((image) => {
const { name, base } = path.parse(image);
return `![${name}](diagrams/${base})`;
})
.join("\n");
}