🌳
pt0/serverF/fileutilsF/getFilesRecF.mts
1import * as _ from 'lodash-es'
2import { resolve } from 'path'
7import { tsAbsPath, type absFileDirPath } from '../../ptDirF.mts'
9export const getFilesRec = async (rootPath: string, stopAfterLen=Infinity): Promise<absFileDirPath[]> => {
10 if (stopAfterLen <= 0) return []
12 const isDir = await getIsDir(rootPath)
14 if (isDir === null) return []
15 if (isDir === false) return [tsAbsPath(rootPath)]
17 const dirsAndFiles = _.map(await readdir(rootPath), (fileName: string) => {
18 return tsAbsPath(resolve(rootPath, fileName))
19 })
21 const dirs = _.compact(await allPromCalls(dirsAndFiles, async (path) => {
22 if (globallyIgnoredPaths.some(ignored => path.endsWith('/' + ignored))) return false
23 if (await getIsDir(path)) return path
24 }))
26 const directFiles = _.difference(dirsAndFiles, dirs)
28 let retFiles: absFileDirPath[] = [...directFiles]
29 await chainPromiseCalls(dirs, async (dir) => {
30 retFiles = [
31 ...(await getFilesRec(dir, stopAfterLen - retFiles.length)),
32 ...retFiles,
33 ]
34 })
36 return retFiles
39export const globallyIgnoredPaths = [
40 '.DS_Store', '.next', 'node_modules', 'tmp', 'dist',