starter-blog/lib/utils/files.js
Timothy 735c954e72
Feat/sub-route (#38)
* blog subroute support
* docs: update readme and blog

Co-authored-by: mrhut10 <ahut10@gmail.com>
2021-05-08 13:58:57 +08:00

20 lines
647 B
JavaScript

import fs from 'fs'
import path from 'path'
const pipe = (...fns) => (x) => fns.reduce((v, f) => f(v), x)
const flatternArray = (input) =>
input.reduce((acc, item) => [...acc, ...(Array.isArray(item) ? item : [item])], [])
const map = (fn) => (input) => input.map(fn)
const walkDir = (fullPath) => {
return fs.statSync(fullPath).isFile() ? fullPath : getAllFilesRecursively(fullPath)
}
const pathJoinPrefix = (prefix) => (extraPath) => path.join(prefix, extraPath)
const getAllFilesRecursively = (folder) =>
pipe(fs.readdirSync, map(pipe(pathJoinPrefix(folder), walkDir)), flatternArray)(folder)
export default getAllFilesRecursively