tailwind-nextjs-blog/lib/utils/files.ts

24 lines
695 B
TypeScript
Raw Normal View History

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)
const walkDir = (fullPath: string) => {
2022-07-17 21:40:41 +08:00
return fs.statSync(fullPath).isFile() ? fullPath : getAllFilesRecursively(fullPath)
}
const pathJoinPrefix = (prefix: string) => (extraPath: string) => path.join(prefix, extraPath)
2022-07-17 21:40:41 +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