|
1
|
import { getCleanMd, GetCleanMdOptions } from "docs-utils" |
|
2
|
import { fdir } from "fdir" |
|
3
|
import { writeFile } from "fs/promises" |
|
4
|
import path from "path" |
|
5
|
import { |
|
6
|
apiRefLlmsGenerator, |
|
7
|
CustomLlmsGenerator, |
|
8
|
jsSdkLlmsGenerator, |
|
9
|
stepsLlmsGenerator, |
|
10
|
workflowsLlmsGenerator, |
|
11
|
} from "./utils/custom-llms-generators.js" |
|
12
|
|
|
13
|
type FileExt = "md" | "yaml" |
|
14
|
|
|
15
|
type Options = { |
|
16
|
outputPath: string |
|
17
|
scanDirs: { |
|
18
|
dir: string |
|
19
|
options?: Omit<GetCleanMdOptions, "file" | "type"> |
|
20
|
allowedFilesPatterns?: RegExp[] |
|
21
|
generator?: { |
|
22
|
name: "workflows" | "steps" | "jsSdk" | "apiRef" |
|
23
|
options: Record<string, unknown> |
|
24
|
} |
|
25
|
ext?: FileExt |
|
26
|
}[] |
|
27
|
introText?: string |
|
28
|
plugins?: GetCleanMdOptions["plugins"] |
|
29
|
} |
|
30
|
|
|
31
|
const generators: Record<string, CustomLlmsGenerator<any>> = { |
|
32
|
workflows: workflowsLlmsGenerator, |
|
33
|
steps: stepsLlmsGenerator, |
|
34
|
jsSdk: jsSdkLlmsGenerator, |
|
35
|
apiRef: apiRefLlmsGenerator, |
|
36
|
} |
|
37
|
|
|
38
|
const isExtAllowed = (fileName: string, allowedExt: FileExt) => { |
|
39
|
switch (allowedExt) { |
|
40
|
case "md": |
|
41
|
return fileName.endsWith(".md") || fileName.endsWith(".mdx") |
|
42
|
case "yaml": |
|
43
|
return fileName.endsWith(".yaml") || fileName.endsWith(".yml") |
|
44
|
} |
|
45
|
} |
|
46
|
|
|
47
|
const getContentFromDir = async ({ |
|
48
|
dir, |
|
49
|
options = {}, |
|
50
|
allowedFilesPatterns, |
|
51
|
generator, |
|
52
|
ext = "md", |
|
53
|
}: Options["scanDirs"][0]): Promise<string> => { |
|
54
|
const files = ( |
|
55
|
await new fdir() |
|
56
|
.withFullPaths() |
|
57
|
.filter((file) => { |
|
58
|
const baseName = path.basename(file) |
|
59
|
|
|
60
|
return isExtAllowed(baseName, ext) && !baseName.startsWith("_") |
|
61
|
}) |
|
62
|
.filter( |
|
63
|
(file) => |
|
64
|
!allowedFilesPatterns?.length || |
|
65
|
allowedFilesPatterns.some((pattern) => file.match(pattern)) |
|
66
|
) |
|
67
|
.crawl(dir) |
|
68
|
.withPromise() |
|
69
|
).sort() |
|
70
|
|
|
71
|
const content: string[] = |
|
72
|
generator?.name && generators[generator?.name] |
|
73
|
? [await generators[generator?.name](files, generator.options)] |
|
74
|
: [] |
|
75
|
|
|
76
|
if (content.length) { |
|
77
|
return await getCleanMd({ |
|
78
|
file: content.join("\n\n"), |
|
79
|
...options, |
|
80
|
type: "content", |
|
81
|
}) |
|
82
|
} |
|
83
|
|
|
84
|
for (const file of files) { |
|
85
|
content.push( |
|
86
|
await getCleanMd({ |
|
87
|
file, |
|
88
|
...options, |
|
89
|
}) |
|
90
|
) |
|
91
|
} |
|
92
|
|
|
93
|
return content.join("\n\n") |
|
94
|
} |
|
95
|
|
|
96
|
export const generateLlmsFull = async ({ |
|
97
|
outputPath, |
|
98
|
scanDirs, |
|
99
|
introText = "", |
|
100
|
plugins, |
|
101
|
}: Options) => { |
|
102
|
const text: string[] = [introText] |
|
103
|
|
|
104
|
for (const scanDir of scanDirs) { |
|
105
|
text.push( |
|
106
|
await getContentFromDir({ |
|
107
|
...scanDir, |
|
108
|
options: { |
|
109
|
plugins, |
|
110
|
...scanDir.options, |
|
111
|
}, |
|
112
|
}) |
|
113
|
) |
|
114
|
} |
|
115
|
|
|
116
|
await writeFile(outputPath, text.join("\n\n")) |
|
117
|
} |