-
Notifications
You must be signed in to change notification settings - Fork 6
/
generateTypedoc.mjs
194 lines (161 loc) · 5.4 KB
/
generateTypedoc.mjs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env node
import * as fs from 'fs'
import * as path from 'path'
import { createRequire } from 'module' // built-in module
const require = createRequire(import.meta.url) // construct the require function for this ES module
const branchPrefix = process.argv[2]
const repoName = process.argv[3]
import { dirname } from 'path' // Importing dirname
import { fileURLToPath } from 'url' // Importing fileURLToPath
// Retrieve the current file's absolute path
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
if (!branchPrefix) {
console.error('Please provide branch name as argument.')
process.exit(1)
}
if (!repoName) {
console.error('Please provide repo name as argument.')
process.exit(1)
}
const docsPath = path.resolve(`./src/pages/docs/${repoName}/reference`)
async function main() {
console.log(`Switching to branch ${branchPrefix} ...`)
console.log('Generating typedoc ...')
await generateTypedoc(branchPrefix)
const navigation = await generateNavigation([branchPrefix])
await fs.promises.writeFile(
`./src/navigation/${repoName}-jsreference.js`,
'export const navigation = ' + navigation
)
// Move default-values.md to the correct location, so we don't overwrite it with reference generation
const sourcePath = `${__dirname}/src/navigation/customPages/default-values.md`
const destPath = `${__dirname}/src/pages/docs/${repoName}/reference/default-values.md`
await fs.promises.copyFile(sourcePath, destPath)
}
const util = require('util')
const glob = util.promisify(require('glob'))
async function generateTypedoc(branchPrefix) {
const outputPath = `./src/pages/docs/${repoName}/reference`
// Once typedoc is done, start looking for .md files and remove ".md" mentions.
const files = await glob(outputPath + '/**/*.md')
console.log("Starting to remove '.md' mentions from files.")
await Promise.all(
files.map(async (file) => {
let data = await fs.promises.readFile(file, 'utf8')
const result = data.replace(/\.md/g, '')
await fs.promises.writeFile(file, result, 'utf8')
})
)
}
async function generateNavigation(versions) {
try {
const navigation = await Promise.all(
versions.map(async (version) => {
const title = `${version}`
const childrenDirs = getChildrenDirectories(docsPath)
const links = await Promise.all([
{
title: 'Content overview',
href: `/docs/${repoName}/reference/overview`,
},
{
title: 'Default values',
href: `/docs/${repoName}/reference/default-values`,
},
...childrenDirs.map(async (item) => {
const hrefPrefix = `/docs/${repoName}/reference/${item}`
return {
title: item,
children: await getMarkdownTitles(
path.join(docsPath, item),
hrefPrefix
),
}
}),
])
return {
title,
links,
}
})
)
return stringifyArray(navigation)
} catch (error) {
console.error('Error in generateNavigation:', error)
}
}
function getChildrenDirectories(parentDir) {
try {
const entries = fs.readdirSync(parentDir, { withFileTypes: true })
return entries
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name)
} catch (error) {
console.error('Error in getChildrenDirectories:', error)
}
}
async function getMarkdownTitles(dirPath, hrefPrefix) {
try {
const entries = await fs.promises.readdir(dirPath, { withFileTypes: true })
const directlyNestedMarkdownFiles = entries.filter(
(entry) => entry.isFile() && entry.name.endsWith('.md')
)
return directlyNestedMarkdownFiles.map((file) => {
let title = getTitleFromFile(file.name)
// If title contains a ".", return the part after it.
// Otherwise, leave the title as it is.
const splitTitle = title.split('.')
if (splitTitle.length > 1) {
title = splitTitle.slice(1).join('.')
}
const href = path.join(hrefPrefix, file.name.replace('.md', ''))
return {
title,
href,
}
})
} catch (error) {
console.error('Error in getMarkdownTitles:', error)
}
}
function getTitleFromFile(filename) {
try {
return filename
.replace(/-/g, ' ')
.replace('.md', '')
.split(' ')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ')
} catch (error) {
console.error('Error in getTitleFromFile:', error)
}
}
function stringifyObject(obj, indent = ' ') {
const entries = Object.entries(obj)
.map(([key, value]) => {
const formattedValue =
value.constructor === Object
? stringifyObject(value, indent + ' ')
: Array.isArray(value)
? stringifyArray(value, indent + ' ')
: JSON.stringify(value)
return `${indent}${key}: ${formattedValue}`
})
.join(',\n')
return `{\n${entries}\n${indent.slice(2)}}`
}
function stringifyArray(array, indent = ' ') {
const entries = array
.map((entry) => {
return Array.isArray(entry)
? stringifyArray(entry, indent + ' ')
: entry.constructor === Object
? stringifyObject(entry, indent + ' ')
: JSON.stringify(entry)
})
.join(',\n')
return `[\n${entries}\n${indent.slice(2)}]`
}
main().catch((error) => console.error(error))
export default undefined