feat: 初始化 TypeScript CLI 模板项目
建立完整的 CLI 产品基础骨架,支持多种演进方向 - 添加项目基础配置(package.json、tsconfig、pnpm-lock.yaml、.gitignore、.npmignore) - 实现运行时基础设施(RuntimeServices 统一注入、日志、输出模式、配置存储、插件加载、任务注册表) - 采用 command/use-case/presenter 三层架构组织业务模块(demo、config、task) - 添加 UI 渲染层(banner、panel、table、主题样式) - 实现插件机制,支持运行时扫描加载外部模块和任务 - 支持 text/json 双输出模式,便于集成与调试 - 添加统一错误模型 AppError 和 CLI 错误边界处理 - 基于 node:test 建立测试基座,包含示例测试用例 - 配置 npm 发布脚本和包元数据 - 编写完整 README 文档,包含快速开始、开发约定和演进方向
This commit is contained in:
102
src/runtime/types.ts
Normal file
102
src/runtime/types.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import type { Command } from "commander";
|
||||
|
||||
export type OutputMode = "text" | "json";
|
||||
|
||||
export interface Logger {
|
||||
debugEnabled: boolean;
|
||||
setDebug: (enabled: boolean) => void;
|
||||
debug: (message: string) => void;
|
||||
info: (message: string) => void;
|
||||
error: (message: string) => void;
|
||||
}
|
||||
|
||||
export interface SelectOption {
|
||||
value: string;
|
||||
label: string;
|
||||
}
|
||||
|
||||
export interface SpinnerController {
|
||||
start: () => void;
|
||||
stop: (message: string) => void;
|
||||
}
|
||||
|
||||
export interface UiAdapter {
|
||||
isInteractive: boolean;
|
||||
writeLine: (message?: string) => void;
|
||||
promptText: (message: string, placeholder?: string) => Promise<string>;
|
||||
promptConfirm: (message: string, initialValue?: boolean) => Promise<boolean>;
|
||||
promptSelect: (message: string, options: SelectOption[]) => Promise<string>;
|
||||
createSpinner: (label: string) => SpinnerController;
|
||||
}
|
||||
|
||||
export interface OutputWriter {
|
||||
mode: OutputMode;
|
||||
setMode: (mode: OutputMode) => void;
|
||||
writeText: (message?: string) => void;
|
||||
writeData: (value: unknown) => void;
|
||||
}
|
||||
|
||||
export interface ConfigStore<TConfig extends object = Record<string, unknown>> {
|
||||
path: string;
|
||||
read: () => Promise<Partial<TConfig>>;
|
||||
write: (value: TConfig) => Promise<void>;
|
||||
patch: (value: Partial<TConfig>) => Promise<TConfig>;
|
||||
}
|
||||
|
||||
export interface PluginTask<Result = unknown> {
|
||||
id: string;
|
||||
title: string;
|
||||
run: (services: RuntimeServices) => Promise<Result>;
|
||||
}
|
||||
|
||||
export interface TaskRegistry {
|
||||
list: () => PluginTask[];
|
||||
find: (id: string) => PluginTask | undefined;
|
||||
}
|
||||
|
||||
export interface RuntimeServices {
|
||||
cwd: string;
|
||||
env: NodeJS.ProcessEnv;
|
||||
logger: Logger;
|
||||
ui: UiAdapter;
|
||||
output: OutputWriter;
|
||||
configStore: ConfigStore<AppConfig>;
|
||||
taskRegistry: TaskRegistry;
|
||||
loadedPlugins: string[];
|
||||
}
|
||||
|
||||
export interface CommandModule {
|
||||
register: (program: Command, services: RuntimeServices) => void;
|
||||
}
|
||||
|
||||
export interface AppConfig {
|
||||
projectName: string;
|
||||
persona: string;
|
||||
verbose: boolean;
|
||||
}
|
||||
|
||||
export interface ModuleDefinition {
|
||||
id: string;
|
||||
create: () => CommandModule;
|
||||
}
|
||||
|
||||
export interface TaskDefinition {
|
||||
id: string;
|
||||
create: () => PluginTask;
|
||||
}
|
||||
|
||||
export interface CliPlugin {
|
||||
name: string;
|
||||
modules?: ModuleDefinition[];
|
||||
tasks?: TaskDefinition[];
|
||||
}
|
||||
|
||||
export interface PluginLoadResult {
|
||||
plugins: CliPlugin[];
|
||||
failures: Array<{ path: string; reason: string }>;
|
||||
}
|
||||
|
||||
export interface RuntimeBootstrap {
|
||||
services: RuntimeServices;
|
||||
moduleDefinitions: ModuleDefinition[];
|
||||
}
|
||||
Reference in New Issue
Block a user