1688-product-master/scripts/run.ts

108 lines
3.1 KiB
TypeScript
Raw Normal View History

2026-03-11 23:36:59 +00:00
#!/usr/bin/env bun
import type { Command } from '../src/types.js';
import { run1688 } from '../src/index.js';
/**
* v2.0 .env.local
* ~/.openclaw/.env
*
* skill skill
*
*
* cp ~/.openclaw/.env.example ~/.openclaw/.env
* vi ~/.openclaw/.env # CLIENT_KEY
*/
function printUsage(): void {
console.error(`Usage:
bun run scripts/run.ts [--client-key=<key>] [--auth-base=<url>] [--ecom-base=<url>] <command> [args...] [--dry-run]
Commands:
session
scrape-url <1688-url> [translate]
scrape-payload <payload-json>
Examples:
bun run scripts/run.ts scrape-url 'https://detail.1688.com/offer/852504650877.html'
bun run scripts/run.ts scrape-url 'https://detail.1688.com/offer/852504650877.html' true
bun run scripts/run.ts scrape-url 'https://detail.1688.com/offer/852504650877.html' --dry-run
bun run scripts/run.ts scrape-payload '{"url":"https://detail.1688.com/offer/852504650877.html"}'
:
~/.openclaw/.env
`);
}
type CliArgs = {
command: Command;
args: string[];
dryRun: boolean;
clientKey?: string;
authBase?: string;
ecomBase?: string;
};
function parseArgs(argv: string[]): CliArgs | null {
const positionals: string[] = [];
let dryRun = false;
let clientKey: string | undefined;
let authBase: string | undefined;
let ecomBase: string | undefined;
for (const arg of argv) {
if (arg === '--dry-run') {
dryRun = true;
} else if (arg.startsWith('--client-key=')) {
clientKey = arg.slice('--client-key='.length).trim();
} else if (arg.startsWith('--auth-base=')) {
authBase = arg.slice('--auth-base='.length).trim().replace(/\/$/, '');
} else if (arg.startsWith('--ecom-base=')) {
ecomBase = arg.slice('--ecom-base='.length).trim().replace(/\/$/, '');
} else if (arg === '-h' || arg === '--help') {
printUsage();
process.exit(0);
} else {
positionals.push(arg);
}
}
if (positionals.length < 1) {
return null;
}
const command = positionals[0] as Command;
const args = positionals.slice(1);
return { command, args, dryRun, clientKey, authBase, ecomBase };
}
async function main(): Promise<void> {
// 不再加载 .env.local直接使用全局配置 ~/.openclaw/.env
// auth-runtime 会自动加载全局配置
const parsed = parseArgs(process.argv.slice(2));
if (!parsed) {
printUsage();
process.exit(1);
}
// 命令行参数覆盖全局配置
if (parsed.clientKey) process.env.CLIENT_KEY = parsed.clientKey;
if (parsed.authBase) process.env.AUTH_BASE = parsed.authBase;
if (parsed.ecomBase) process.env.ECOM_BASE = parsed.ecomBase;
const result = await run1688(parsed.command, parsed.args, parsed.dryRun);
console.log(JSON.stringify(result, null, 2));
}
main().catch((error) => {
console.error(JSON.stringify({
status: 'failed',
error: error instanceof Error ? error.message : String(error),
command: '',
dryRun: false,
}, null, 2));
process.exit(1);
});