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:
Ilya Kreymer 2023-11-09 11:27:11 -08:00 committed by GitHub
parent 877d9f5b44
commit af1e0860e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 2446 additions and 1406 deletions

34
src/util/file_reader.ts Normal file
View 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 [];
}