index.ts38 lines · main
1import fs from 'fs'
2import path from 'path'
3import { NextApiRequest, NextApiResponse } from 'next'
4
5function getFilePaths(folderPath: string, baseFolder = ''): string[] {
6 const filepaths: string[] = []
7
8 const files = fs.readdirSync(folderPath)
9 for (const file of files) {
10 if (file === '.DS_Store') {
11 continue
12 }
13
14 const filePath = path.join(folderPath, file)
15 const stats = fs.statSync(filePath)
16
17 if (stats.isDirectory()) {
18 const subFolder = path.join(baseFolder, file)
19 const subFolderPaths = getFilePaths(filePath, subFolder)
20 filepaths.push(...subFolderPaths)
21 } else {
22 const filePathRelativeToBase = path.join(baseFolder, file)
23 if (file !== '.DS_Store') {
24 filepaths.push(filePathRelativeToBase)
25 }
26 filepaths.push(filePathRelativeToBase)
27 }
28 }
29
30 return filepaths
31}
32
33export default async function getFileNames(_req: NextApiRequest, res: NextApiResponse) {
34 const folderPath = path.join(process.cwd(), 'components/interfaces/Home/Connect/content')
35 const filepaths = getFilePaths(folderPath)
36 res.statusCode = 200
37 res.json(filepaths)
38}