-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.js
More file actions
175 lines (135 loc) · 4.26 KB
/
document.js
File metadata and controls
175 lines (135 loc) · 4.26 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { promises, existsSync } from "node:fs";
import path from "node:path";
import ignored from "./ignore.js";
import mappings from "./mappings.js";
const template = ({ name, description, signature, examples, questions }) => {
let content = `# ${name}
`;
if (description && !description.startsWith("TODO")) {
content += `
${description}
`;
}
if (signature && !signature.startsWith("TODO")) {
content += `
## Type signature
<!-- prettier-ignore-start -->
\`\`\`typescript
${signature}
\`\`\`
<!-- prettier-ignore-end -->
`;
}
if (
examples &&
examples.length > 0 &&
!examples[0].content.includes("TODO")
) {
content += `
## Examples
<!-- prettier-ignore-start -->`;
content += examples
.map(
item => `
\`\`\`${item.language}
${item.content}
\`\`\``
)
.join("\n");
content += "\n<!-- prettier-ignore-end -->\n";
}
if (questions && questions.length > 0 && !questions[0].startsWith("TODO")) {
content += `
## Questions
`;
content += questions.map(item => `- ${item}`).join("\n");
content += "\n";
}
return content;
};
const jsonTemplate = name => ({
name,
description: "TODO: Fill short description here.",
signature: "TODO: Fill type signature here.",
examples: [
{
language: "javascript",
content: `${name}();\n// ⇒ TODO`
}
],
questions: ["TODO: List questions that may this function answers."]
});
const [ignoredFiles, ignoredDirectories] = ignored;
const {
readdir: readDirectoryAsync,
writeFile: writeFileAsync,
readFile: readFileAsync
} = promises;
const [, , cwd = process.cwd()] = process.argv;
const identifier = name => mappings[name] || name;
// Do not match type definition files *.d.ts but match *.ts:
// https://stackoverflow.com/a/43493203/1384679
const extension = /(^.?|\.[^d]|[^.]d|[^.][^d])\.ts$/i;
const testFilePattern = /\.test\.[tj]s$/i;
const importFormat = ""; // js = ".js";
const getSignature = async signaturePath => {
const fileContent = await readFileAsync(signaturePath, "utf8");
const exportDefaultTag = "export default ";
const exportedModuleName = fileContent
.slice(fileContent.indexOf(exportDefaultTag) + exportDefaultTag.length)
.trim()
.slice(0, -1);
const startTag = `${exportedModuleName}:`;
const startIndex = fileContent.indexOf(startTag) + startTag.length;
const endTag = "\n\n";
const endIndex = fileContent.indexOf(endTag, startIndex);
return fileContent
.slice(startIndex, endIndex)
.trim()
.slice(0, -1)
.replace(";\nexport default _default", "")
.trim();
};
const main = async cwd => {
console.log(`Generating documentation files in ${cwd}...`);
const entries = await readDirectoryAsync(cwd, { withFileTypes: true });
const files = entries
.filter(x => x.isFile())
.map(x => x.name)
.filter(x => extension.test(x))
.filter(x => !testFilePattern.test(x))
.filter(x => !ignoredFiles.includes(x));
const directories = entries
.filter(x => x.isDirectory())
.map(x => x.name)
.filter(x => !ignoredDirectories.includes(x));
for (const directory of directories) {
await main(path.join(cwd, directory));
}
const submodules = files.map(filePath => {
const { base: fileName } = path.parse(filePath);
const splitted = fileName.split(".");
const id = identifier(splitted.slice(0, splitted.length - 1).join("_"));
const extension = `.${splitted[splitted.length - 1]}`;
return [fileName.replace(extension, importFormat), id, extension];
});
for (const [fileName, id] of submodules) {
const filePath = path.join(cwd, `${fileName}.md`);
const jsonPath = path.join(cwd, `${fileName}.json`);
if (!existsSync(jsonPath)) {
const content = jsonTemplate(id);
await writeFileAsync(jsonPath, JSON.stringify(content, null, 2), "utf8");
}
const fileContent = await readFileAsync(jsonPath, "utf8");
const data = JSON.parse(fileContent);
const signaturePath = path.join(cwd, `${fileName}.d.ts`);
const signature = await getSignature(signaturePath);
if (signature !== data.signature) {
data.signature = signature;
await writeFileAsync(jsonPath, JSON.stringify(data, null, 2), "utf8");
}
const content = template(data);
await writeFileAsync(filePath, content);
}
};
main(cwd);