1 import { existsSync, readdirSync } from "fs"
2 import path from "path"
3 import { getSidebarItemLink, ItemsToAdd } from "../index.js"
4
5 export default async function getCoreFlowsRefSidebarChildren(): Promise<
6 ItemsToAdd[]
7 > {
8 const projPath = path.resolve()
9 const basePath = path.join(projPath, "references", "core_flows")
10
11 const directories = readdirSync(basePath, {
12 withFileTypes: true,
13 })
14
15 const sidebarItems: ItemsToAdd[] = []
16
17 sidebarItems.push(
18 {
19 type: "link",
20 title: "Overview",
21 path: "/medusa-workflows-reference",
22 loaded: true,
23 },
24 {
25 type: "separator",
26 }
27 )
28
29 for (const directory of directories) {
30 if (
31 !directory.isDirectory() ||
32 directory.name.startsWith("core_flows.") ||
33 directory.name === "types" ||
34 directory.name === "interfaces"
35 ) {
36 continue
37 }
38
39 const workflowItems: ItemsToAdd[] = []
40 const stepItems: ItemsToAdd[] = []
41
42 const readItemsFromChildDirs = async (type: "Steps" | "Workflows") => {
43 const childNamespaceBasePath = path.join(
44 basePath,
45 directory.name,
46 `${type}_${directory.name}`,
47 "functions"
48 )
49
50 if (!existsSync(childNamespaceBasePath)) {
51 return
52 }
53 const childDirs = readdirSync(childNamespaceBasePath, {
54 withFileTypes: true,
55 })
56
57 for (const childDir of childDirs) {
58 if (!childDir.isDirectory()) {
59 continue
60 }
61
62 const childDirPath = path.join(childNamespaceBasePath, childDir.name)
63 const childFile = readdirSync(childDirPath)
64
65 if (!childFile.length) {
66 continue
67 }
68
69 const sidebarItem = await getSidebarItemLink({
70 filePath: path.join(childDirPath, childFile[0]),
71 basePath: projPath,
72 fileBasename: childFile[0],
73 })
74
75 if (sidebarItem) {
76 if (childDir.name.endsWith("Workflow")) {
77 workflowItems.push(sidebarItem)
78 } else {
79 stepItems.push(sidebarItem)
80 }
81 }
82 }
83 }
84
85 await readItemsFromChildDirs("Steps")
86 await readItemsFromChildDirs("Workflows")
87
88 if (workflowItems.length || stepItems.length) {
89 const item: ItemsToAdd = {
90 type: "category",
91 title: directory.name.replaceAll("_", " "),
92 children: [],
93 loaded: true,
94 }
95
96 if (workflowItems.length) {
97 item.children!.push({
98 type: "sub-category",
99 title: "Workflows",
100 children: workflowItems,
101 loaded: true,
102 })
103 }
104
105 if (stepItems.length) {
106 item.children!.push({
107 type: "sub-category",
108 title: "Steps",
109 children: stepItems,
110 loaded: true,
111 })
112 }
113
114 sidebarItems.push(item)
115 }
116 }
117
118 return sidebarItems
119 }