mirror of
https://github.com/webrecorder/browsertrix-crawler.git
synced 2025-12-08 06:09:48 +00:00
TypeScript Conversion (#425)
Follows #424. Converts the upcoming 1.0.0 branch based on native browser-based traffic capture and recording to TypeScript. Fixes #426 --------- Co-authored-by: Tessa Walsh <tessa@bitarchivist.net> Co-authored-by: emma <hi@emma.cafe>
This commit is contained in:
parent
877d9f5b44
commit
af1e0860e4
36 changed files with 2446 additions and 1406 deletions
34
src/util/file_reader.ts
Normal file
34
src/util/file_reader.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
const MAX_DEPTH = 2;
|
||||
|
||||
export function collectAllFileSources(fileOrDir: string, ext?: string, depth = 0) : string[] {
|
||||
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: string[], next: string) => {
|
||||
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 [];
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue