-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfollowUpDocs.js
More file actions
91 lines (71 loc) · 2.79 KB
/
followUpDocs.js
File metadata and controls
91 lines (71 loc) · 2.79 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
// Fügt Beispiel-Code der Doku hinzu
import fs from "fs"
import path from "path"
import packageJson from "./package.json" with {type: "json"}
const docPath = "./src/docs/data/docs.json"
import doc from "./src/docs/data/docs.json" with {type: "json"}
const packageName = packageJson.name
function prepareExampleCode(exampleCode) {
/**
* Transforms imports to package imports from "blue-react"
* */
exampleCode.replace('"../../../index.js"', `"${packageName}"`)
const matches = [
...exampleCode.matchAll(
/import (.*) from "..\/..\/(..\/)?components\/(.*)"/gm
)
]
let extraModules = []
let componentNames = matches.map((match) => {
if (match[1].includes(",")) {
match[1]
.replace("{", "")
.replace("}", "")
.split(",")
.forEach((s) => {
const moduleName = s.trim()
if (match[3] !== moduleName) {
extraModules.push(moduleName)
}
})
}
return match[3]
})
componentNames = [...componentNames, ...extraModules]
const importCode = `import { ${componentNames
.sort()
.join(", ")} } from "${packageName}"`
matches.forEach((match) => {
if (exampleCode.includes(importCode)) {
exampleCode = exampleCode.replace(`${match[0]}\r\n`, "")
} else {
exampleCode = exampleCode.replace(match[0], importCode)
}
})
return exampleCode
}
Object.keys(doc).forEach((prop) => doc[prop].forEach((comp, i) => {
const displayName = doc[prop][i].displayName
const exampleFilePathTsx = "./src/docs/examples/" + displayName + ".tsx"
if (fs.existsSync(exampleFilePathTsx)) {
let exampleCode = fs.readFileSync(exampleFilePathTsx, "utf8")
doc[prop][i].exampleCode = prepareExampleCode(exampleCode)
}
// Looking for folder in examples folder named after the component
const exampleFolderPath = "./src/docs/examples/" + displayName
if (fs.existsSync(exampleFolderPath)) {
if (!doc[prop][i].examples) doc[prop][i].examples = {}
const files = fs.readdirSync(exampleFolderPath)
for (const file of files) {
const subExampleFilePathTsx = path.join(exampleFolderPath, file)
let exampleCode = fs.readFileSync(subExampleFilePathTsx, "utf8")
doc[prop][i].examples[file] = prepareExampleCode(exampleCode)
}
}
const exampleFilePathJs = "./src/docs/examples/" + displayName + ".js"
if (fs.existsSync(exampleFilePathJs)) {
const exampleCode = fs.readFileSync(exampleFilePathJs, "utf8")
doc[prop][i].exampleCode = prepareExampleCode(exampleCode)
}
}))
fs.writeFileSync(docPath, JSON.stringify(doc))