33 lines
933 B
TypeScript
Raw Normal View History

2021-02-21 23:40:10 +01:00
import * as fs from "fs";
import * as path from "path";
const crawlRec = (dir_path: string, paths: string[]) => {
for (const file_name of fs.readdirSync(dir_path)) {
const file_path = path.join(dir_path, file_name);
2021-02-21 23:40:10 +01:00
if (fs.lstatSync(file_path).isDirectory()) {
crawlRec(file_path, paths);
2021-02-21 23:40:10 +01:00
continue;
2021-02-21 23:40:10 +01:00
}
paths.push(file_path);
}
};
/** List all files in a given directory return paths relative to the dir_path */
export function crawl(params: { dirPath: string; returnedPathsType: "absolute" | "relative to dirPath" }): string[] {
const { dirPath, returnedPathsType } = params;
const filePaths: string[] = [];
2021-02-21 23:40:10 +01:00
crawlRec(dirPath, filePaths);
2021-02-21 23:40:10 +01:00
switch (returnedPathsType) {
case "absolute":
return filePaths;
case "relative to dirPath":
return filePaths.map(filePath => path.relative(dirPath, filePath));
}
}