2022-07-17 21:40:41 +08:00
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
|
|
|
|
const pipe =
|
|
|
|
(...fns) =>
|
|
|
|
(x) =>
|
|
|
|
fns.reduce((v, f) => f(v), x)
|
|
|
|
|
|
|
|
const flattenArray = (input) =>
|
|
|
|
input.reduce((acc, item) => [...acc, ...(Array.isArray(item) ? item : [item])], [])
|
|
|
|
|
|
|
|
const map = (fn) => (input) => input.map(fn)
|
|
|
|
|
2022-10-07 13:55:39 +08:00
|
|
|
const walkDir = (fullPath: string) => {
|
2022-07-17 21:40:41 +08:00
|
|
|
return fs.statSync(fullPath).isFile() ? fullPath : getAllFilesRecursively(fullPath)
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:55:39 +08:00
|
|
|
const pathJoinPrefix = (prefix: string) => (extraPath: string) => path.join(prefix, extraPath)
|
2022-07-17 21:40:41 +08:00
|
|
|
|
2022-10-07 13:55:39 +08:00
|
|
|
const getAllFilesRecursively = (folder: string): string[] =>
|
2022-07-17 21:40:41 +08:00
|
|
|
pipe(fs.readdirSync, map(pipe(pathJoinPrefix(folder), walkDir)), flattenArray)(folder)
|
|
|
|
|
|
|
|
export default getAllFilesRecursively
|