forked from suhank/zea-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildApiDocs.js
143 lines (125 loc) · 4.08 KB
/
buildApiDocs.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* eslint-disable require-jsdoc */
const fs = require('fs')
path = require('path')
const jsdoc2md = require('jsdoc-to-markdown')
// var documentation = require('documentation');
const renderSourceFileToMarkdown = (filepath, tgtDir) => {
const outPath = path
.format({
dir: tgtDir,
name: path.basename(filepath, '.js'),
ext: '.md',
})
.split('\\')
.join('/')
return new Promise((resolve, reject) => {
jsdoc2md
.render({
files: filepath,
})
.then((data) => {
resolve({ outPath, data })
})
})
}
const renderSourceFolderToMarkdown = (dir, tgtDir) => {
const promise = new Promise((resolve) => {
const fileRenders = []
const files = fs.readdirSync(dir)
files.forEach((file) => {
const filepath = path.join(dir, file)
fileRenders.push(
new Promise((resolve) => {
fs.stat(filepath, (err, stats) => {
if (stats.isDirectory()) {
renderSourceFolderToMarkdown(
filepath,
path.join(tgtDir, file)
).then((data) => {
resolve(data)
})
} else if (stats.isFile()) {
renderSourceFileToMarkdown(filepath, tgtDir).then((data) => {
resolve(data)
})
}
})
})
)
})
Promise.all(fileRenders).then((datas) => {
const READMEFilestxt = []
const READMEFoldertxt = []
const fullOutFolder = path.join('docs', tgtDir)
if (!fs.existsSync(fullOutFolder)) {
fs.mkdirSync(fullOutFolder, { recursive: true })
}
datas.forEach((fileRender) => {
if (fileRender.type == 'folder') {
if (fileRender.outPath) {
const parts = fileRender.outPath.split('\\')
const folderName = parts[parts.length - 2]
READMEFoldertxt.push(`### [${folderName}](${parts.join('/')})`)
}
} else {
if (fileRender.data) {
READMEFilestxt.push(
`### [${path.basename(fileRender.outPath, '.md')}](${
fileRender.outPath
})`
)
const data = fileRender.data
.replace(/\#{3} ([\w\d ]+\.)?([\w\d ]+).+/gim, '### $2')
.replace(
/\* \[[\.]?([\w\d ]+)(\(.*?\))?\]\(.*\)(.*)?/gm,
(a, b, c, d) => {
const cleanedLink = b ? b.replace(' ', '-') : ''
return `* [${b}${c || ''}${d || ''}](#${cleanedLink})`
}
)
const fullOutPath = path.join('docs', fileRender.outPath)
fs.writeFileSync(fullOutPath, data)
}
}
})
const result = {
type: 'folder',
files: datas,
}
if (READMEFilestxt.length > 0 || READMEFoldertxt.length > 0) {
let READMEtxt = `# ${path.basename(tgtDir, '.js')}\n`.toUpperCase()
if (READMEFilestxt.length > 0) {
READMEtxt = READMEtxt + `## Classes\n${READMEFilestxt.join('\n')}\n\n`
}
if (READMEFoldertxt.length > 0) {
// eslint-disable-next-line prettier/prettier
READMEtxt = `${READMEtxt} ## Modules {docsify-ignore} \n${READMEFoldertxt.join('\n')}\n\n`
}
const outPath = path.join(tgtDir, 'README.md')
const fullOutPath = path.join('docs', outPath)
fs.writeFileSync(fullOutPath, READMEtxt)
result.outPath = outPath
}
resolve(result)
})
})
return promise
}
// ////////////////////////////////
// Entry
fs.rmdirSync('docs/api', { recursive: true })
renderSourceFolderToMarkdown('src', 'api').then((data) => {
console.log('writing : searchToc.json')
const searchToc = []
data.files.forEach((file) => {
if (!file.outPath) return
if (file.outPath.endsWith('README.md')) {
const parts = file.outPath.split('\\')
parts.pop()
searchToc.push(parts.join('/') + '/')
} else {
searchToc.push(path.basename(file.outPath, '.md').split('\\').join('/'))
}
})
fs.writeFileSync('docs/searchToc.json', JSON.stringify({ searchToc }))
})