mirror of
https://github.com/webrecorder/browsertrix-crawler.git
synced 2025-10-19 14:33:17 +00:00

* support loading custom behaviors from a specified directory via --customBehaviors * call load() for each behavior incrementally, then call selectMainBehavior() (available in browsertrix-behaviors 0.5.1) * tests: add tests for multiple custom behaviors --------- Co-authored-by: Ilya Kreymer <ikreymer@gmail.com>
33 lines
960 B
JavaScript
33 lines
960 B
JavaScript
import fs from "fs";
|
|
import path from "path";
|
|
|
|
const MAX_DEPTH = 2;
|
|
|
|
export function collectAllFileSources(fileOrDir, ext = null, depth = 0) {
|
|
const resolvedPath = path.resolve(fileOrDir);
|
|
|
|
if (depth >= MAX_DEPTH) {
|
|
console.warn(`WARN: MAX_DEPTH of ${MAX_DEPTH} reached traversing "${resolvedPath}"`);
|
|
return [];
|
|
}
|
|
|
|
const stat = fs.statSync(resolvedPath);
|
|
|
|
if (stat.isFile && (ext === null || path.extname(resolvedPath) === ext)) {
|
|
const contents = fs.readFileSync(resolvedPath);
|
|
return [`/* src: ${resolvedPath} */\n\n${contents}`];
|
|
}
|
|
|
|
if (stat.isDirectory) {
|
|
const files = fs.readdirSync(resolvedPath);
|
|
return files.reduce((acc, next) => {
|
|
const nextPath = path.join(fileOrDir, next);
|
|
return [...acc, ...collectAllFileSources(nextPath, ext, depth + 1)];
|
|
}, []);
|
|
}
|
|
|
|
if (depth === 0) {
|
|
console.warn(`WARN: The provided path "${resolvedPath}" is not a .js file or directory.`);
|
|
return [];
|
|
}
|
|
}
|