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

34 lines
743 B
TypeScript
Raw Permalink Normal View History

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