|
| 1 | +import yargs from "yargs"; |
| 2 | +import { hideBin } from "yargs/helpers"; |
| 3 | +import path from "path"; |
| 4 | +import fs from "fs"; |
| 5 | + |
| 6 | +import { CrawlConfig } from "./crawler.js"; |
| 7 | + |
| 8 | +const defaultBrowserCmds = ["chrome", "chromium", "google-chrome"]; |
| 9 | + |
| 10 | +function isExecutable(filePath: string) { |
| 11 | + try { |
| 12 | + fs.accessSync(filePath, fs.constants.X_OK); |
| 13 | + return true; |
| 14 | + } catch { |
| 15 | + return false; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +function isInPath(cmd: string): boolean { |
| 20 | + const exts = process.platform === "win32" ? ["", ".exe"] : [""]; |
| 21 | + const dirs = process.env.PATH?.split(path.delimiter) ?? []; |
| 22 | + for (const dir of dirs) { |
| 23 | + for (const ext of exts) { |
| 24 | + const fullPath = path.join(dir, cmd + ext); |
| 25 | + if (fs.existsSync(fullPath) && isExecutable(fullPath)) { |
| 26 | + return true; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + return false; |
| 31 | +} |
| 32 | + |
| 33 | +function validateWidths(breakpoints: number[], deviceWidths: number[]) { |
| 34 | + if (deviceWidths.length !== breakpoints.length + 1) { |
| 35 | + console.error( |
| 36 | + `Error: device-widths must have exactly one more entry than breakpoint-widths.\n` + |
| 37 | + `Got ${deviceWidths.length} device widths and ${breakpoints.length} breakpoints.`, |
| 38 | + ); |
| 39 | + process.exit(1); |
| 40 | + } |
| 41 | + for (let i = 0; i < breakpoints.length; i++) { |
| 42 | + if ( |
| 43 | + !( |
| 44 | + deviceWidths[i] < breakpoints[i] && breakpoints[i] < deviceWidths[i + 1] |
| 45 | + ) |
| 46 | + ) { |
| 47 | + console.error( |
| 48 | + `Error: deviceWidths and breakpoints must interleave: deviceWidths[${i}] < breakpoints[${i}] < deviceWidths[${i + 1}].\n` + |
| 49 | + `Got deviceWidths[${i}]=${deviceWidths[i]}, breakpoints[${i}]=${breakpoints[i]}, deviceWidths[${i + 1}]=${deviceWidths[i + 1]}.`, |
| 50 | + ); |
| 51 | + process.exit(1); |
| 52 | + } |
| 53 | + if (deviceWidths[i] <= 0) { |
| 54 | + console.error( |
| 55 | + `Error: deviceWidths must be positive numbers.\n` + |
| 56 | + `Got deviceWidths[${i}]=${deviceWidths[i]}.`, |
| 57 | + ); |
| 58 | + process.exit(1); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function toNumList(arg: string): number[] { |
| 64 | + return arg |
| 65 | + ? arg |
| 66 | + .split(",") |
| 67 | + .map((s) => parseInt(s.trim())) |
| 68 | + .filter((n) => !isNaN(n)) |
| 69 | + : []; |
| 70 | +} |
| 71 | + |
| 72 | +export function argsToConfig(): CrawlConfig { |
| 73 | + // --no- is reserved for negation |
| 74 | + const argv = yargs(hideBin(process.argv)) |
| 75 | + .scriptName("crawler") |
| 76 | + .option("url", { |
| 77 | + type: "string", |
| 78 | + demandOption: true, |
| 79 | + desc: "Page URL to crawl", |
| 80 | + }) |
| 81 | + .option("out-dir", { |
| 82 | + alias: "o", |
| 83 | + type: "string", |
| 84 | + default: "./out", |
| 85 | + desc: "Output directory", |
| 86 | + }) |
| 87 | + .option("browser-path", { |
| 88 | + alias: "b", |
| 89 | + type: "string", |
| 90 | + desc: `Chromium/Chrome executable path, default will try ${defaultBrowserCmds.join(", ")}`, |
| 91 | + }) |
| 92 | + .option("disable-headless", { |
| 93 | + type: "boolean", |
| 94 | + default: false, |
| 95 | + desc: "Run browser with headful mode", |
| 96 | + }) |
| 97 | + .option("device-widths", { |
| 98 | + type: "string", |
| 99 | + default: "1024", |
| 100 | + desc: "Comma-separated device widths for crawling", |
| 101 | + }) |
| 102 | + .option("breakpoint-widths", { |
| 103 | + type: "string", |
| 104 | + default: "", |
| 105 | + desc: "Comma-separated widths for @media condition, must interleave device-widths", |
| 106 | + }) |
| 107 | + .option("device-scale-factor", { |
| 108 | + type: "number", |
| 109 | + default: 1, |
| 110 | + desc: "Device scale factor", |
| 111 | + }) |
| 112 | + .option("screen-height", { |
| 113 | + type: "number", |
| 114 | + default: 800, |
| 115 | + desc: "Viewport height for screens", |
| 116 | + }) |
| 117 | + .option("recursive", { |
| 118 | + alias: "r", |
| 119 | + type: "boolean", |
| 120 | + default: false, |
| 121 | + desc: "Recursively crawl same-site links", |
| 122 | + }) |
| 123 | + .option("browser-scan", { |
| 124 | + type: "boolean", |
| 125 | + default: false, |
| 126 | + desc: "Use browser for link extraction (slower, handling JS)", |
| 127 | + }) |
| 128 | + .option("max-pages", { |
| 129 | + type: "number", |
| 130 | + default: 0, |
| 131 | + desc: "Maximum number of pages to crawl (0 = no limit)", |
| 132 | + }) |
| 133 | + .option("delay-after-nav", { |
| 134 | + type: "number", |
| 135 | + default: 1000, |
| 136 | + desc: "Delay ms after navigation before processing", |
| 137 | + }) |
| 138 | + .help() |
| 139 | + .parseSync(); |
| 140 | + |
| 141 | + const breakpoints = toNumList(argv["breakpoint-widths"]); |
| 142 | + const deviceWidths = toNumList(argv["device-widths"]); |
| 143 | + |
| 144 | + validateWidths(breakpoints, deviceWidths); |
| 145 | + |
| 146 | + var browserPath: string | undefined = argv["browser-path"]; |
| 147 | + if (!browserPath) { |
| 148 | + for (const cmd of defaultBrowserCmds) { |
| 149 | + if (isInPath(cmd)) { |
| 150 | + browserPath = cmd; |
| 151 | + console.log(`Using browser executable: ${cmd}`); |
| 152 | + break; |
| 153 | + } |
| 154 | + } |
| 155 | + if (!browserPath) { |
| 156 | + console.error(`Error: Chrome/Chromium executable not found in PATH. |
| 157 | +Please specify the path with --browser-path. |
| 158 | +Tried: ${defaultBrowserCmds.join(", ")}`); |
| 159 | + process.exit(1); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return { |
| 164 | + url: argv.url, |
| 165 | + outDir: argv["out-dir"], |
| 166 | + browserPath: browserPath, |
| 167 | + headless: !argv["disable-headless"], |
| 168 | + screenHeight: argv["screen-height"], |
| 169 | + breakpoints, |
| 170 | + deviceWidths, |
| 171 | + deviceScaleFactor: argv["device-scale-factor"], |
| 172 | + recursive: argv["recursive"], |
| 173 | + browserScan: argv["browser-scan"], |
| 174 | + maxPages: argv["max-pages"], |
| 175 | + delayAfterNavigateMs: argv["delay-after-nav"], |
| 176 | + }; |
| 177 | +} |
0 commit comments