1688-product-master/scripts/run.ts

108 lines
3.1 KiB
TypeScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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);
});