-
Notifications
You must be signed in to change notification settings - Fork 32
/
copy-generated-component-docs.ts
90 lines (79 loc) · 2.5 KB
/
copy-generated-component-docs.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
84
85
86
87
88
89
90
const fs = require('fs');
const generatedDocsPath = 'temp-component-docs/classes';
const componentDocsPath = process.argv[2];
const startOfGeneratedContentMark = '[//]: # "Start of generated content"';
const endOfGeneratedContentMark = '[//]: # "End of generated content"';
const extractProperties = (content: string) => {
const lines: string[] = content.split('\n');
const startOfPropertiesMark = '## Properties';
const endOfPropertiesMark = '## '; // End of properties: next heading
const startOfPropertiesLineNumber = lines.indexOf(startOfPropertiesMark);
const endOfPropertiesLineNumber = lines.findIndex(
(line, index) =>
line.startsWith(endOfPropertiesMark) &&
index > startOfPropertiesLineNumber
);
if (startOfPropertiesLineNumber === -1) {
return [];
}
return lines.filter(
(_, lineNumber) =>
lineNumber > startOfPropertiesLineNumber &&
(endOfPropertiesLineNumber === -1 ||
lineNumber < endOfPropertiesLineNumber)
);
};
const insertGeneratedParts = (
fileContent: string,
generatedContent: string[]
) => {
const lines: string[] = fileContent.split('\n');
const startOfGeneratedContentLineNumber = lines.indexOf(
startOfGeneratedContentMark
);
const result = [
...lines.splice(0, startOfGeneratedContentLineNumber + 1),
generatedContent.length ? '## Inputs and outputs \n' : '\n',
...generatedContent,
...lines.splice(0),
];
return result.join('\n');
};
fs.readdir(generatedDocsPath, (err: any, files: string[]) => {
if (err) {
throw err;
}
files.forEach((file) => {
fs.readFile(
`${generatedDocsPath}/${file}`,
'utf8',
(err: any, generatedFileContent: string) => {
if (err) {
throw err;
}
const propertiesContent = extractProperties(generatedFileContent);
fs.readFile(
`${componentDocsPath}/${file}x`,
'utf8',
(err: any, docFile: any) => {
if (err)
throw new Error(
`${componentDocsPath}/${file}x couldn't be opened, error: ${err}, make sure that this file exists`
);
if (file !== '_category_.json') {
const result = insertGeneratedParts(docFile, propertiesContent);
fs.writeFile(
`${componentDocsPath}/${file}x`,
result,
'utf8',
(err: any) => {
if (err) throw err;
}
);
}
}
);
}
);
});
});