2021-02-21 23:40:10 +01:00
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "path";
|
|
|
|
|
2023-06-21 18:23:12 +02:00
|
|
|
const crawlRec = (dir_path: string, paths: string[]) => {
|
|
|
|
for (const file_name of fs.readdirSync(dir_path)) {
|
2023-06-21 03:54:43 +02:00
|
|
|
const file_path = path.join(dir_path, file_name);
|
2021-02-21 23:40:10 +01:00
|
|
|
|
2023-06-21 03:54:43 +02:00
|
|
|
if (fs.lstatSync(file_path).isDirectory()) {
|
|
|
|
crawlRec(file_path, paths);
|
2021-02-21 23:40:10 +01:00
|
|
|
|
2023-06-21 03:54:43 +02:00
|
|
|
continue;
|
2021-02-21 23:40:10 +01:00
|
|
|
}
|
|
|
|
|
2023-06-21 03:54:43 +02: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
|
|
|
|
2023-06-21 03:54:43 +02:00
|
|
|
crawlRec(dirPath, filePaths);
|
2021-02-21 23:40:10 +01:00
|
|
|
|
2023-06-21 03:54:43 +02:00
|
|
|
switch (returnedPathsType) {
|
|
|
|
case "absolute":
|
|
|
|
return filePaths;
|
|
|
|
case "relative to dirPath":
|
|
|
|
return filePaths.map(filePath => path.relative(dirPath, filePath));
|
|
|
|
}
|
|
|
|
}
|