Files
opencli/src/registry.ts
T
jackwener 7555f14369 refactor: deep code review improvements
- Add *.log to .gitignore, remove debug.log from tracking
- Fix dev-mode FS scan to discover .ts adapter files (not just .js)
- Deduplicate CONNECT_TIMEOUT: browser.ts now uses runtime.ts constant
- Fix CSV output: escape newlines in field values per RFC 4180
- Add proper type interfaces for validate/verify (remove any types)
- Remove unused hadOuterQuotes variable in snapshotFormatter
- Derive CliOptions from CliCommand via Omit+Partial to reduce duplication
- Expand dense one-liner action callbacks in main.ts for readability
2026-03-17 01:38:23 +08:00

88 lines
2.1 KiB
TypeScript

/**
* Core registry: Strategy enum, Arg/CliCommand interfaces, cli() registration.
*/
import type { IPage } from './types.js';
export enum Strategy {
PUBLIC = 'public',
COOKIE = 'cookie',
HEADER = 'header',
INTERCEPT = 'intercept',
UI = 'ui',
}
export interface Arg {
name: string;
type?: string;
default?: any;
required?: boolean;
positional?: boolean;
help?: string;
choices?: string[];
}
export interface CliCommand {
site: string;
name: string;
description: string;
domain?: string;
strategy?: Strategy;
browser?: boolean;
args: Arg[];
columns?: string[];
func?: (page: IPage, kwargs: Record<string, any>, debug?: boolean) => Promise<any>;
pipeline?: any[];
timeoutSeconds?: number;
source?: string;
}
/** Internal extension for lazy-loaded TS modules (not exposed in public API) */
export interface InternalCliCommand extends CliCommand {
_lazy?: boolean;
_modulePath?: string;
}
export interface CliOptions extends Partial<Omit<CliCommand, 'args' | 'description'>> {
site: string;
name: string;
description?: string;
args?: Arg[];
}
const _registry = new Map<string, CliCommand>();
export function cli(opts: CliOptions): CliCommand {
const cmd: CliCommand = {
site: opts.site,
name: opts.name,
description: opts.description ?? '',
domain: opts.domain,
strategy: opts.strategy ?? (opts.browser === false ? Strategy.PUBLIC : Strategy.COOKIE),
browser: opts.browser ?? (opts.strategy === Strategy.PUBLIC ? false : true),
args: opts.args ?? [],
columns: opts.columns,
func: opts.func,
pipeline: opts.pipeline,
timeoutSeconds: opts.timeoutSeconds,
};
const key = fullName(cmd);
_registry.set(key, cmd);
return cmd;
}
export function getRegistry(): Map<string, CliCommand> {
return _registry;
}
export function fullName(cmd: CliCommand): string {
return `${cmd.site}/${cmd.name}`;
}
export function strategyLabel(cmd: CliCommand): string {
return cmd.strategy ?? 'public';
}
export function registerCommand(cmd: CliCommand): void {
_registry.set(fullName(cmd), cmd);
}