-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathgenerate-site-examples.js
359 lines (301 loc) · 10.1 KB
/
generate-site-examples.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* This script goes through all generated type definitions and creates
* playground examples in the site for all code examples that are
* annotated using the `<!-- siteExample(category: string, name: string, prority: int) -->
* comment.
*/
const fs = require('fs')
const path = require('path')
const forEachFile = require('./util/for-each-file')
const _ = require('lodash')
const ESM_PATH = path.join(__dirname, '..', 'dist', 'esm')
const SITE_EXAMPLE_PATH = path.join(__dirname, '..', 'site', 'docs', 'examples')
const SITE_EXAMPLE_START_REGEX = /<!--\s*siteExample\(/
const SITE_EXAMPLE_ANNOTATION_REGEX =
/<!--\s*siteExample\("([^"]+)",\s*"([^"]+)",\s*(\d+)\s*\)\s*-->/
const CODE_BLOCK_START_REGEX = /\*\s*```/
const CODE_BLOCK_END_REGEX = /\*\s*```/
const COMMENT_LINE_REGEX = /\*\s*(.*)/
const CODE_LINE_REGEX = /\*(.*)/
const moreExamplesByCategory = {
select: {
'select method':
'https://kysely-org.github.io/kysely-apidoc/interfaces/SelectQueryBuilder.html#select',
'selectAll method':
'https://kysely-org.github.io/kysely-apidoc/interfaces/SelectQueryBuilder.html#selectAll',
'selectFrom method':
'https://kysely-org.github.io/kysely-apidoc/classes/Kysely.html#selectFrom',
},
where: {
'where method':
'https://kysely-org.github.io/kysely-apidoc/interfaces/SelectQueryBuilder.html#where',
'whereRef method':
'https://kysely-org.github.io/kysely-apidoc/interfaces/SelectQueryBuilder.html#whereRef',
},
join: {
'innerJoin method':
'https://kysely-org.github.io/kysely-apidoc/interfaces/SelectQueryBuilder.html#innerJoin',
'leftJoin method':
'https://kysely-org.github.io/kysely-apidoc/interfaces/SelectQueryBuilder.html#leftJoin',
'rightJoin method':
'https://kysely-org.github.io/kysely-apidoc/interfaces/SelectQueryBuilder.html#rightJoin',
'fullJoin method':
'https://kysely-org.github.io/kysely-apidoc/interfaces/SelectQueryBuilder.html#fullJoin',
},
insert: {
'values method':
'https://kysely-org.github.io/kysely-apidoc/classes/InsertQueryBuilder.html#values',
'onConflict method':
'https://kysely-org.github.io/kysely-apidoc/classes/InsertQueryBuilder.html#onConflict',
'returning method':
'https://kysely-org.github.io/kysely-apidoc/classes/InsertQueryBuilder.html#returning',
'insertInto method':
'https://kysely-org.github.io/kysely-apidoc/classes/Kysely.html#insertInto',
},
update: {
'set method':
'https://kysely-org.github.io/kysely-apidoc/classes/UpdateQueryBuilder.html#set',
'returning method':
'https://kysely-org.github.io/kysely-apidoc/classes/UpdateQueryBuilder.html#returning',
'updateTable method':
'https://kysely-org.github.io/kysely-apidoc/classes/Kysely.html#updateTable',
},
delete: {
'deleteFrom method':
'https://kysely-org.github.io/kysely-apidoc/classes/Kysely.html#deleteFrom',
'returning method':
'https://kysely-org.github.io/kysely-apidoc/classes/DeleteQueryBuilder.html#returning',
},
merge: {
'mergeInto method':
'https://kysely-org.github.io/kysely-apidoc/classes/Kysely.html#mergeInto',
'using method':
'https://kysely-org.github.io/kysely-apidoc/classes/MergeQueryBuilder.html#using',
'whenMatched method':
'https://kysely-org.github.io/kysely-apidoc/classes/WheneableMergeQueryBuilder.html#whenMatched',
'thenUpdateSet method':
'https://kysely-org.github.io/kysely-apidoc/classes/MatchedThenableMergeQueryBuilder.html#thenUpdateSet',
'thenDelete method':
'https://kysely-org.github.io/kysely-apidoc/classes/MatchedThenableMergeQueryBuilder.html#thenDelete',
'thenDoNothing method':
'https://kysely-org.github.io/kysely-apidoc/classes/MatchedThenableMergeQueryBuilder.html#thenDoNothing',
'whenNotMatched method':
'https://kysely-org.github.io/kysely-apidoc/classes/WheneableMergeQueryBuilder.html#whenNotMatched',
'thenInsertValues method':
'https://kysely-org.github.io/kysely-apidoc/classes/NotMatchedThenableMergeQueryBuilder.html#thenInsertValues',
},
transactions: {
'transaction method':
'https://kysely-org.github.io/kysely-apidoc/classes/Kysely.html#transaction',
},
}
function main() {
deleteAllExamples()
forEachFile(ESM_PATH, (filePath) => {
if (!filePath.endsWith('.d.ts')) {
return
}
const lines = readLines(filePath)
const state = {
filePath,
/** @type {string | null} */
line: null,
lineIndex: 0,
annotation: null,
inExample: false,
inCodeBlock: false,
commentLines: [],
codeLines: [],
}
for (let l = 0; l < lines.length; ++l) {
state.line = lines[l]
state.lineIndex = l + 1
if (state.inExample) {
if (state.inCodeBlock) {
if (isCodeBlockEnd(state)) {
writeSiteExample(state)
exitExample(state)
} else {
addCodeLine(state)
}
} else if (isCodeBlockStart(state)) {
enterCodeBlock(state)
} else {
addCommentLine(state)
}
} else if (isExampleStart(state)) {
enterExample(state)
}
}
})
}
function deleteAllExamples() {
for (const category of fs.readdirSync(SITE_EXAMPLE_PATH)) {
const folderPath = path.join(SITE_EXAMPLE_PATH, category)
if (!fs.statSync(folderPath).isFile()) {
for (const file of fs.readdirSync(folderPath)) {
const filePath = path.join(folderPath, file)
if (file.endsWith('.js') || file.endsWith('.mdx')) {
fs.unlinkSync(filePath)
}
}
}
}
}
function readLines(filePath) {
const data = fs.readFileSync(filePath).toString('utf-8')
return data.split('\n')
}
function isCodeBlockEnd(state) {
return CODE_BLOCK_END_REGEX.test(state.line)
}
function writeSiteExample(state) {
const [, category, name, priority] = state.annotation
const code = trimEmptyLines(state.codeLines).join('\n')
const comment = trimEmptyLines(state.commentLines).join('\n')
const codeVariable = _.camelCase(name)
const fileName = `${priority.padStart(4, '0')}-${_.kebabCase(name)}`
const folderPath = path.join(SITE_EXAMPLE_PATH, category)
const filePath = path.join(folderPath, fileName)
const codeFile = `export const ${codeVariable} = \`${deindent(code)
.replaceAll('`', '\\`')
.replaceAll('${', '\\${')}\``
const parts = [
deindent(`
---
title: '${name}'
---
# ${name}
`),
]
if (comment?.trim()) {
parts.push(comment, '')
}
parts.push(
deindent(`
import {
Playground,
exampleSetup,
} from '../../../src/components/Playground'
import {
${codeVariable}
} from './${fileName}'
<div style={{ marginBottom: '1em' }}>
<Playground code={${codeVariable}} setupCode={exampleSetup} />
</div>
`),
)
const moreExamples = buildMoreExamplesMarkdown(category)
if (moreExamples?.trim()) {
parts.push(moreExamples)
}
const exampleFile = parts.join('\n')
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true })
fs.writeFileSync(
path.join(folderPath, '_category_.json'),
`{
"label": "${_.startCase(category)}",
"position": 0, // TODO: set the position of the category.
"link": {
"type": "generated-index",
"description": "Short and simple examples of using the ${category} functionality." // TODO: review this.
}
}`,
)
}
fs.writeFileSync(filePath + '.js', codeFile, {})
fs.writeFileSync(filePath + '.mdx', exampleFile)
}
function buildMoreExamplesMarkdown(category) {
const links = moreExamplesByCategory[category]
if (!links) {
return undefined
}
const lines = [
':::info[More examples]',
'The API documentation is packed with examples. The API docs are hosted [here](https://kysely-org.github.io/kysely-apidoc/),',
'but you can access the same documentation by hovering over functions/methods/classes in your IDE. The examples are always',
'just one hover away!',
'',
'For example, check out these sections:',
]
for (const linkName of Object.keys(links)) {
lines.push(` - [${linkName}](${links[linkName]})`)
}
lines.push(':::\n')
return lines.join('\n')
}
function exitExample(state) {
state.annotation = null
state.inExample = false
state.inCodeBlock = false
state.commentLines = []
state.codeLines = []
}
function addCodeLine(state) {
const code = CODE_LINE_REGEX.exec(state.line)
if (!code) {
console.error(
`found invalid code block in a site example in ${state.filePath}:${state.lineIndex}`,
)
process.exit(1)
}
state.codeLines.push(code[1])
}
function isCodeBlockStart(state) {
return CODE_BLOCK_START_REGEX.test(state.line)
}
function enterCodeBlock(state) {
state.inCodeBlock = true
}
function addCommentLine(state) {
const comment = COMMENT_LINE_REGEX.exec(state.line)
if (!comment) {
console.error(
`found invalid comment in a site example in ${state.filePath}:${state.lineIndex}`,
)
process.exit(1)
}
state.commentLines.push(comment[1])
}
function isExampleStart(state) {
return SITE_EXAMPLE_START_REGEX.test(state.line)
}
function enterExample(state) {
state.annotation = SITE_EXAMPLE_ANNOTATION_REGEX.exec(state.line)
if (!state.annotation) {
console.error(
`found invalid site example annotation in ${state.filePath}:${state.lineIndex}`,
)
process.exit(1)
}
state.inExample = true
}
function deindent(str) {
let lines = str.split('\n')
// Remove empty lines from the beginning.
while (lines[0].trim().length === 0) {
lines = lines.slice(1)
}
let ws = Number.MAX_SAFE_INTEGER
for (const line of lines) {
if (line.trim().length > 0) {
const [, wsExec] = /^(\s*)/.exec(line) || []
if (wsExec != null && wsExec.length < ws) {
ws = wsExec.length
}
}
}
return lines.map((line) => line.substring(ws)).join('\n')
}
function trimEmptyLines(lines) {
while (lines.length && lines[0].trim().length === 0) {
lines = lines.slice(1)
}
while (lines.length && lines[lines.length - 1].trim().length === 0) {
lines = lines.slice(0, lines.length - 1)
}
return lines
}
main()