-
Notifications
You must be signed in to change notification settings - Fork 32
/
remove-generated-component-docs-content.ts
61 lines (56 loc) · 1.59 KB
/
remove-generated-component-docs-content.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
const fs = require('fs');
const componentDocsPath = process.argv[2];
const startOfGeneratedContentMark = '[//]: # "Start of generated content"';
const endOfGeneratedContentMark = '[//]: # "End of generated content"';
const removeGeneratedParts = (fileContent: string, file: string) => {
const lines: string[] = fileContent.split('\n');
const startOfGeneratedContentLineNumber = lines.indexOf(
startOfGeneratedContentMark
);
const endOfGeneratedContentLineNumber = lines.indexOf(
endOfGeneratedContentMark
);
if (
startOfGeneratedContentLineNumber === -1 ||
endOfGeneratedContentLineNumber === -1
) {
throw new Error(`Generated content markers are missing from ${file}`);
}
return lines
.filter(
(_: string, lineNr: number) =>
lineNr <= startOfGeneratedContentLineNumber ||
lineNr >= endOfGeneratedContentLineNumber
)
.join('\n');
};
fs.readdir(componentDocsPath, (err: any, files: string[]) => {
if (err) {
throw err;
}
files.forEach((file) => {
fs.readFile(
`${componentDocsPath}/${file}`,
'utf8',
(err: any, content: string) => {
if (err) {
throw err;
}
if (file !== '_category_.json') {
const contentWithoutGeneratedParts = removeGeneratedParts(
content,
`${componentDocsPath}/${file}`
);
fs.writeFile(
`${componentDocsPath}/${file}`,
contentWithoutGeneratedParts,
'utf8',
(err: any) => {
if (err) throw err;
}
);
}
}
);
});
});