2023-07-06 16:09:48 -04:00
|
|
|
import fs from "fs";
|
2024-03-18 14:24:48 -07:00
|
|
|
import fsp from "fs/promises";
|
2023-07-06 16:09:48 -04:00
|
|
|
import path from "path";
|
|
|
|
|
|
|
|
const MAX_DEPTH = 2;
|
|
|
|
|
2023-11-09 19:11:11 -05:00
|
|
|
export function collectAllFileSources(
|
|
|
|
fileOrDir: string,
|
|
|
|
ext?: string,
|
|
|
|
depth = 0,
|
2023-12-13 12:14:53 -08:00
|
|
|
): { path: string; contents: string }[] {
|
2023-07-06 16:09:48 -04:00
|
|
|
const resolvedPath = path.resolve(fileOrDir);
|
|
|
|
|
|
|
|
if (depth >= MAX_DEPTH) {
|
2023-11-09 19:11:11 -05:00
|
|
|
console.warn(
|
|
|
|
`WARN: MAX_DEPTH of ${MAX_DEPTH} reached traversing "${resolvedPath}"`,
|
|
|
|
);
|
2023-07-06 16:09:48 -04:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const stat = fs.statSync(resolvedPath);
|
|
|
|
|
2023-11-09 11:27:11 -08:00
|
|
|
if (stat.isFile() && (ext === null || path.extname(resolvedPath) === ext)) {
|
2023-07-06 16:09:48 -04:00
|
|
|
const contents = fs.readFileSync(resolvedPath);
|
2023-12-13 12:14:53 -08:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
path: resolvedPath,
|
|
|
|
contents: `/* src: ${resolvedPath} */\n\n${contents}`,
|
|
|
|
},
|
|
|
|
];
|
2023-07-06 16:09:48 -04:00
|
|
|
}
|
|
|
|
|
2023-11-09 11:27:11 -08:00
|
|
|
if (stat.isDirectory()) {
|
2023-07-06 16:09:48 -04:00
|
|
|
const files = fs.readdirSync(resolvedPath);
|
2023-12-13 12:14:53 -08:00
|
|
|
return files.reduce(
|
|
|
|
(acc: { path: string; contents: string }[], next: string) => {
|
|
|
|
const nextPath = path.join(fileOrDir, next);
|
|
|
|
return [...acc, ...collectAllFileSources(nextPath, ext, depth + 1)];
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
2023-07-06 16:09:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (depth === 0) {
|
2023-11-09 19:11:11 -05:00
|
|
|
console.warn(
|
|
|
|
`WARN: The provided path "${resolvedPath}" is not a .js file or directory.`,
|
|
|
|
);
|
2023-07-06 16:09:48 -04:00
|
|
|
}
|
2023-11-09 11:27:11 -08:00
|
|
|
|
|
|
|
return [];
|
2023-07-06 16:09:48 -04:00
|
|
|
}
|
2024-03-18 14:24:48 -07:00
|
|
|
|
|
|
|
export async function getInfoString() {
|
|
|
|
const packageFileJSON = JSON.parse(
|
|
|
|
await fsp.readFile(new URL("../../package.json", import.meta.url), {
|
|
|
|
encoding: "utf-8",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
const warcioPackageJSON = JSON.parse(
|
|
|
|
await fsp.readFile(
|
|
|
|
new URL("../../node_modules/warcio/package.json", import.meta.url),
|
|
|
|
{ encoding: "utf-8" },
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return `Browsertrix-Crawler ${packageFileJSON.version} (with warcio.js ${warcioPackageJSON.version})`;
|
|
|
|
}
|