1688-logistics-scraper/scripts/run.ts

49 lines
1.4 KiB
TypeScript

#!/usr/bin/env bun
import type { Command } from '../src/index.ts';
import { run } from '../src/index.ts';
function printUsage(): void {
console.error(`Usage:
bun scripts/run.ts [--port=<cdp-port>] <command> [args...] [--dry-run]
Commands:
scrape <1688-url> Scrape logistics data (weight/size) from product page
Examples:
bun scripts/run.ts scrape 'https://detail.1688.com/offer/852504650877.html'
bun scripts/run.ts scrape 'https://detail.1688.com/offer/852504650877.html' --dry-run
bun scripts/run.ts --port=18801 scrape 'https://detail.1688.com/offer/852504650877.html'
`);
}
async function main(): Promise<void> {
const positionals: string[] = [];
let dryRun = false;
let port = 18800;
for (const arg of process.argv.slice(2)) {
if (arg === '--dry-run') {
dryRun = true;
} else if (arg.startsWith('--port=')) {
port = parseInt(arg.slice('--port='.length), 10);
} else if (arg === '-h' || arg === '--help') {
printUsage(); process.exit(0);
} else {
positionals.push(arg);
}
}
if (positionals.length < 1) { printUsage(); process.exit(1); }
const result = await run(positionals[0] as Command, positionals.slice(1), dryRun, port);
console.log(JSON.stringify(result, null, 2));
}
main().catch((err) => {
console.error(JSON.stringify({
status: 'failed',
error: err instanceof Error ? err.message : String(err),
}, null, 2));
process.exit(1);
});