1 import { statSync, readdirSync } from "fs"
2 import path from "path"
3 import { getFileSlug } from "docs-utils"
4
5 const monoRepoPath = path.resolve("..", "..", "..")
6
7 /**
8 *
9 * @param {string} dir - The directory to search in
10 * @returns {Promise<import("types").SlugChange[]>}
11 */
12 export default async function getSlugs(options = {}) {
13 let { dir, basePath = path.resolve("app"), baseSlug = basePath } = options
14 if (!dir) {
15 dir = basePath
16 }
17 /**
18 * @type {import("types").SlugChange[]}
19 */
20 const slugs = []
21
22 // ignore general reference files
23 if (dir.endsWith("/references/medusa") || dir.endsWith("/references/types")) {
24 return slugs
25 }
26 const files = readdirSync(dir)
27
28 for (const file of files) {
29 const filePath = path.join(dir, file)
30 const fileBasename = path.basename(file)
31 if (fileBasename !== "page.mdx") {
32 const fileStat = statSync(filePath)
33 if (fileStat.isDirectory()) {
34 slugs.push(
35 ...(await getSlugs({
36 dir: filePath,
37 basePath,
38 baseSlug,
39 }))
40 )
41 }
42 continue
43 }
44
45 const newSlug = await getFileSlug(filePath)
46
47 if (newSlug) {
48 slugs.push({
49 origSlug: filePath
50 .replace(baseSlug, "")
51 .replace(fileBasename, "")
52 .replace(/\/$/, ""),
53 newSlug,
54 filePath: filePath.replace(monoRepoPath, ""),
55 })
56 }
57 }
58
59 return slugs
60 }