index.ts 1.1 KB

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