1 import { findPageTitle, oasFileToPath } from "docs-utils"
2 import { readFile } from "fs/promises"
3 import path from "path"
4 import pkg from "slugify"
5 import YAML from "yaml"
6
7 const slugify = pkg.default
8
9 const monorepoRoot = path.resolve(process.cwd(), "..", "..", "..")
10 const referencesRoot = path.join(monorepoRoot, "www", "apps", "resources")
11 const slugsPath = path.join(
12 monorepoRoot,
13 "www",
14 "apps",
15 "resources",
16 "generated",
17 "slug-changes.mjs"
18 )
19 const slugsFileContentPrefix = `export const slugChanges = `
20
21 export type CustomLlmsGenerator<T = Record<string, unknown>> = (
22 files: string[],
23 options?: T
24 ) => Promise<string>
25
26 type CommonOptions = {
27 baseUrl?: string
28 }
29
30 export const workflowsLlmsGenerator: CustomLlmsGenerator<
31 CommonOptions
32 > = async (files, options) => {
33 return generateListForReferenceFiles({
34 files,
35 title: "Workflows",
36 itemContent: (title, fileSlug) =>
37 `[${title.replace(/ - .+/, "")}](${fileSlug})`,
38 options,
39 })
40 }
41
42 export const stepsLlmsGenerator: CustomLlmsGenerator<CommonOptions> = async (
43 files,
44 options
45 ) => {
46 return generateListForReferenceFiles({
47 files,
48 title: "Steps",
49 itemContent: (title, fileSlug) =>
50 `[${title.replace(/ - .+/, "")}](${fileSlug})`,
51 options,
52 })
53 }
54
55 export const jsSdkLlmsGenerator: CustomLlmsGenerator<
56 CommonOptions & {
57 type: "Admin" | "Store" | "Auth"
58 }
59 > = async (files, options) => {
60 return generateListForReferenceFiles({
61 files,
62 title: `JS SDK ${options?.type}`,
63 itemContent: (title, fileSlug) =>
64 `[${title.replace(/ - .+/, "")}](${fileSlug})`,
65 options,
66 })
67 }
68
69 export const apiRefLlmsGenerator: CustomLlmsGenerator<
70 CommonOptions & {
71 type: "Admin" | "Store"
72 }
73 > = async (files, options) => {
74 let content = `## ${options?.type} API Reference\n\n`
75
76 for (const file of files) {
77 const baseName = path.basename(file)
78 const fileYaml = YAML.parse(await readFile(file, "utf-8"))
79
80 const oasPath = oasFileToPath(baseName)
81
82 Object.entries(fileYaml).forEach(
83 ([httpMethod, operation]: [string, any]) => {
84 const hash =
85 `${slugify(operation.tags[0])}_${slugify(operation.operationId)}`.toLowerCase()
86
87 content += `- [${httpMethod.toUpperCase()} ${oasPath}](${options?.baseUrl}#${hash})\n`
88 }
89 )
90 }
91
92 return content
93 }
94
95 /**
96 * Helpers
97 */
98 export const generateListForReferenceFiles = async ({
99 files,
100 title,
101 itemContent,
102 options,
103 }: {
104 files: string[]
105 title: string
106 itemContent: (title: string, fileSlug: string) => string
107 options?: CommonOptions
108 }) => {
109 const slugChanges = JSON.parse(
110 (await readFile(slugsPath, "utf-8")).replace(slugsFileContentPrefix, "")
111 )
112 let content = `## ${title}\n\n`
113 for (const file of files) {
114 const relativeFilePath = file.replace(monorepoRoot, "")
115 const fileSlug = `${options?.baseUrl}${
116 slugChanges.find(
117 (slugChange: any) => slugChange.filePath === relativeFilePath
118 )?.newSlug ||
119 file.replace(referencesRoot, "").replace(/\/page\.mdx?$/, "")
120 }/index.html.md`
121
122 const itemTitle = (findPageTitle(file) || "").replace(/ - .+/, "")
123
124 content += `- ${itemContent(itemTitle, fileSlug)}\n`
125 }
126
127 return content
128 }