33 lines
991 B
TypeScript
Raw Normal View History

2021-02-21 23:40:10 +01:00
import * as fs from "fs";
import { join as pathJoin, relative as pathRelative } from "path";
2021-02-21 23:40:10 +01:00
const crawlRec = (dirPath: string, filePaths: string[]) => {
for (const basename of fs.readdirSync(dirPath)) {
const fileOrDirPath = pathJoin(dirPath, basename);
2021-02-21 23:40:10 +01:00
if (fs.lstatSync(fileOrDirPath).isDirectory()) {
crawlRec(fileOrDirPath, filePaths);
2021-02-21 23:40:10 +01:00
continue;
2021-02-21 23:40:10 +01:00
}
filePaths.push(fileOrDirPath);
}
};
/** 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 => pathRelative(dirPath, filePath));
}
}