UNPKG

4.91 kBJavaScriptView Raw
1"use strict";
2
3var _fs = require("fs");
4
5var _path = require("path");
6
7function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
8
9function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
10
11const convertersPath = (0, _path.resolve)(__dirname, "../Generator/src/generators/markupGenerator/converters");
12const docs = getDocFiles(convertersPath);
13const content = generateContent(docs).trim().replace(/[\r\n]{2,}/g, "\n");
14(0, _fs.writeFileSync)("DOCUMENTATION.md", content, "utf8");
15
16function getDocFiles(convertersDirPath) {
17 const convertersNames = (0, _fs.readdirSync)(convertersDirPath);
18 const convertersPaths = convertersNames.map(converterName => (0, _path.resolve)(convertersDirPath, converterName));
19 const convertersDirs = convertersPaths.filter(converterPath => (0, _fs.statSync)(converterPath).isDirectory());
20 const documents = convertersDirs.map(converterPath => {
21 const dirName = (0, _path.basename)(converterPath);
22 const docFilePath = (0, _path.resolve)(converterPath, "docs.json");
23 const doc = isFileExists(docFilePath) ? JSON.parse((0, _fs.readFileSync)(docFilePath, "utf8")) : {};
24 const examplePath = (0, _path.resolve)(converterPath, "example.md");
25 const example = isFileExists(examplePath) ? {
26 example: (0, _fs.readFileSync)(examplePath, "utf8")
27 } : {};
28 const descriptionPath = (0, _path.resolve)(converterPath, "description.md");
29 const description = isFileExists(descriptionPath) ? {
30 description: (0, _fs.readFileSync)(descriptionPath, "utf8")
31 } : {};
32 return _objectSpread({
33 name: dirName
34 }, doc, example, description);
35 });
36 return documents;
37}
38
39function generateContent(documents) {
40 return `
41${generateAdditionalDosc()}
42# Документация по сахару
43${generateNavigation(documents)}
44${documents.filter(doc => Object.keys(doc).length !== 1).map(generateSugarElementDoc).join("\n")}
45`;
46}
47
48function generateAdditionalDosc() {
49 const docsPath = (0, _path.resolve)(__dirname, "../docs");
50 const documents = (0, _fs.readdirSync)(docsPath);
51 const additionalDocs = documents.map(doc => {
52 const file = (0, _fs.readFileSync)((0, _path.resolve)(docsPath, doc), "utf8");
53 const strings = file.includes("\r\n") ? file.split("\r\n") : file.split("\n");
54 const header = strings.length !== 0 ? strings[0].replace(/#/g, "").trim() : doc;
55 return `# [${header}](/docs/${doc})`;
56 }).join("\n");
57 return additionalDocs;
58}
59
60function generateNavigation(documents) {
61 return documents.map(doc => {
62 if (Object.keys(doc).length === 1) {
63 return `* ${doc.name}`;
64 }
65
66 return `* [${doc.name}](#${doc.name.toLowerCase()})`;
67 }).join("\n");
68}
69
70function generateSugarElementDoc(doc) {
71 return `
72## ${doc.name} ##
73${doc.description}
74${generateAttributesDoc(doc.attributes, "#### Описание атрибутов")}
75${generateChildrenDoc(doc.children)}
76${generateExample(doc.example)}
77`;
78}
79
80function generateAttributesDoc(attributes, header) {
81 if (attributes == undefined) {
82 return "";
83 }
84
85 const tableHeader = "|name|type|required|default|description|";
86 const tableRows = attributes.map(attribute => {
87 const enumeration = attribute.enumeration ? attribute.enumeration.join(", ") : "не указаны";
88 const type = attribute.type === "enumeration" ? `enumeration: ${enumeration}` : attribute.type;
89 return `
90|${attribute.name}|${wrapOptionalText(type)}|${attribute.required || false}|${wrapOptionalText(attribute.default)}|${wrapOptionalText(attribute.description)}|`;
91 });
92 return `
93${header}
94${tableHeader}
95|:---:|:---:|:---:|:---:|:---|${tableRows.join("/n")}
96`;
97}
98
99function generateExample(example) {
100 if (example == undefined) {
101 return "";
102 }
103
104 return `
105#### Пример
106${example}
107 `;
108}
109
110function generateChildrenDoc(children) {
111 if (children == undefined) {
112 return "";
113 }
114
115 const childrenInfo = children.map(child => `
116* [${child}](#${child.toLowerCase()})`);
117 return `
118#### Дочерние элементы
119${childrenInfo.join("")}
120`;
121}
122
123function wrapOptionalText(text) {
124 return text != undefined ? text.replace(/\|/g, ",") : "—";
125}
126
127function isFileExists(directoryPath) {
128 try {
129 const dirStat = (0, _fs.statSync)(directoryPath);
130 return dirStat.isFile();
131 } catch (ignoreError) {
132 return false;
133 }
134}
135//# sourceMappingURL=generateDocumentation.js.map
\No newline at end of file