-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
91 lines (84 loc) · 2.82 KB
/
main.js
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
import fs from "fs-extra";
import traverse from "@babel/traverse";
import * as parser from "@babel/parser";
import * as ejs from "ejs";
import * as path from 'path'
const file = fs.readFileSync("./ExampleComponent.js", "utf8");
const ast = parser.parse(file, {
sourceType: "module",
strictMode: false,
plugins: [
"optionalChaining",
"classProperties",
"decorators-legacy",
"exportDefaultFrom",
"doExpressions",
"numericSeparator",
"dynamicImport",
"jsx",
"typescript",
"flowComments",
],
});
const setAttributes = []
const getComponentAttributes = []
const templateNamespaces = []
function traverseFile() {
traverse.default(ast, {
enter(path) {
//console.log(path)
},
CallExpression(path) {
if (path.node.callee.property?.name === "getAttribute") {
console.log(
"name: ",
path.node.callee.property?.name,
"value: ",
path.node.arguments?.[0].value
);
getComponentAttributes.push(path.node.arguments?.[0].value)
}
if (path.node.callee.property?.name === "setAttribute") {
console.log(
"SET name: ",
path.node.callee.property?.name,
"SET value: ",
path.node.arguments?.[0].value
);
setAttributes.push(path.node.arguments?.[0].value)
}
},
SwitchStatement: function (path) {
//getAttribute
const getAttribute = path.node.discriminant.callee.property.name
//namespace
const namespace = path.node.discriminant.arguments[0].value
// cases - namespace name
// TODO: ARR.LOOP
const oneNamespace = path.node.cases[0].test.value
const twoNamespace = path.node.cases[1].test.value
console.log(getAttribute, namespace, oneNamespace)
templateNamespaces.push(oneNamespace)
templateNamespaces.push(twoNamespace)
},
// ExportDefaultDeclaration: function (path) {
// console.log(path.node.leadingComments[0].value);
// },
});
}
function renderMD() {
console.log(setAttributes, path.join('/', 'readmeTemplate.md'))
ejs.renderFile("readmeTemplate.md",
{
setAttributes: setAttributes.join('\n'),
getAttributes: getComponentAttributes.join('\n'),
templates: templateNamespaces.join('\n')
}, (err, str) => {
if (err) console.error(err)
const outputFile = "readme.md"
fs.ensureFileSync(outputFile)
fs.outputFileSync(outputFile, str)
})
}
traverseFile()
renderMD()