|
| 1 | +--- |
| 2 | +order: 12 |
| 3 | +title: 命令行工具(自定义模块) |
| 4 | +type: 开发指南 |
| 5 | +--- |
| 6 | + |
| 7 | +## 模块化功能 (Module System) |
| 8 | + |
| 9 | +从 v2.0.0 版本开始,CLI 支持模块化扩展,允许开发者自定义命令和工作流。 |
| 10 | + |
| 11 | +### 核心概念 |
| 12 | + |
| 13 | +- **CLI Module**: 包含自定义命令和工作流的模块 |
| 14 | +- **Command**: 单个可执行的命令 |
| 15 | +- **Workflow**: 由多个步骤组成的复杂工作流 |
| 16 | +- **Module Manager**: 管理模块注册和执行的核心组件 |
| 17 | + |
| 18 | +### 模块注册 |
| 19 | + |
| 20 | +```typescript |
| 21 | +import { moduleManager } from 'react-native-update-cli'; |
| 22 | +import { myCustomModule } from './my-custom-module'; |
| 23 | + |
| 24 | +// 注册自定义模块 |
| 25 | +moduleManager.registerModule(myCustomModule); |
| 26 | +``` |
| 27 | + |
| 28 | +### 创建自定义模块 |
| 29 | + |
| 30 | +```typescript |
| 31 | +import type { CLIModule, CommandContext, CommandResult } from 'react-native-update-cli'; |
| 32 | + |
| 33 | +export const myCustomModule: CLIModule = { |
| 34 | + name: 'my-module', |
| 35 | + version: '1.0.0', |
| 36 | + commands: [ |
| 37 | + { |
| 38 | + name: 'my-command', |
| 39 | + description: '我的自定义命令', |
| 40 | + handler: async (context: CommandContext): Promise<CommandResult> => { |
| 41 | + // 命令执行逻辑 |
| 42 | + return { success: true, data: 'Hello from custom command!' }; |
| 43 | + }, |
| 44 | + options: { |
| 45 | + flag: { |
| 46 | + hasValue: true, |
| 47 | + description: '自定义参数' |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + ], |
| 52 | + workflows: [ |
| 53 | + { |
| 54 | + name: 'my-workflow', |
| 55 | + description: '我的自定义工作流', |
| 56 | + steps: [ |
| 57 | + { |
| 58 | + name: 'step1', |
| 59 | + description: '第一步', |
| 60 | + execute: async (context: CommandContext) => { |
| 61 | + console.log('执行第一步...'); |
| 62 | + return { step1Result: 'completed' }; |
| 63 | + } |
| 64 | + }, |
| 65 | + { |
| 66 | + name: 'step2', |
| 67 | + description: '第二步', |
| 68 | + execute: async (context: CommandContext, previousResult: any) => { |
| 69 | + console.log('执行第二步,前一步结果:', previousResult); |
| 70 | + return { ...previousResult, step2Result: 'completed' }; |
| 71 | + } |
| 72 | + } |
| 73 | + ] |
| 74 | + } |
| 75 | + ], |
| 76 | + init: (provider) => { |
| 77 | + console.log('模块初始化完成'); |
| 78 | + } |
| 79 | +}; |
| 80 | +``` |
| 81 | + |
| 82 | +### 查看已注册模块命令 |
| 83 | + |
| 84 | +```bash |
| 85 | +# 查看所有可用模块命令 |
| 86 | +pushy list |
| 87 | +``` |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## 工作流系统 (Workflow System) |
| 92 | + |
| 93 | +工作流是由多个步骤组成的复杂任务执行流程,支持条件执行、错误处理和结果传递。 |
| 94 | + |
| 95 | +### 内置工作流 |
| 96 | + |
| 97 | +#### 1. 用户认证工作流 |
| 98 | + |
| 99 | +##### pushy auth-check |
| 100 | +检查用户认证状态 |
| 101 | + |
| 102 | +```bash |
| 103 | +pushy workflow auth-check |
| 104 | +``` |
| 105 | + |
| 106 | +##### pushy login-flow |
| 107 | +完整的用户登录流程 |
| 108 | + |
| 109 | +```bash |
| 110 | +pushy workflow login-flow |
| 111 | +``` |
| 112 | + |
| 113 | +#### 2. 应用管理工作流 |
| 114 | + |
| 115 | +##### pushy workflow multi-platform-app-management |
| 116 | +跨平台应用统一管理 |
| 117 | + |
| 118 | +```bash |
| 119 | +pushy workflow multi-platform-app-management |
| 120 | +``` |
| 121 | + |
| 122 | +##### pushy workflow incremental-build |
| 123 | +增量构建工作流,生成差异更新包 |
| 124 | + |
| 125 | +```bash |
| 126 | +pushy workflow incremental-build |
| 127 | +``` |
| 128 | + |
| 129 | +### 工作流管理命令 |
| 130 | + |
| 131 | +列出所有可用工作流 |
| 132 | + |
| 133 | +```bash |
| 134 | +pushy list |
| 135 | +``` |
| 136 | + |
| 137 | +##### pushy workflow |
| 138 | +执行指定工作流 |
| 139 | + |
| 140 | +```bash |
| 141 | +pushy workflow <workflowName> |
| 142 | +``` |
| 143 | + |
| 144 | +##### pushy list |
| 145 | +列出所有已注册的模块 |
| 146 | + |
| 147 | +```bash |
| 148 | +pushy list |
| 149 | +``` |
| 150 | + |
| 151 | +### 工作流特性 |
| 152 | + |
| 153 | +1. **步骤化执行**: 工作流由多个步骤组成,按顺序执行 |
| 154 | +2. **结果传递**: 每个步骤的结果可以传递给下一个步骤 |
| 155 | +3. **条件执行**: 支持根据条件决定是否执行某个步骤 |
| 156 | +4. **错误处理**: 内置错误处理和回滚机制 |
| 157 | +5. **进度反馈**: 实时显示执行进度和状态 |
| 158 | +6. **参数验证**: 执行前自动验证必需参数 |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +## 示例项目 |
| 163 | + |
| 164 | +在项目的 `example/` 目录中提供了完整的模块和工作流使用示例: |
| 165 | + |
| 166 | +- `example/modules/` - 自定义模块示例 |
| 167 | +- `example/workflows/` - 自定义工作流示例 |
| 168 | +- `example/scripts/` - 使用脚本示例 |
| 169 | + |
| 170 | +运行示例: |
| 171 | + |
| 172 | +```bash |
| 173 | +# 模块注册和执行示例 |
| 174 | +npx ts-node example/scripts/register-modules.ts |
| 175 | + |
| 176 | +# 工作流演示 |
| 177 | +npx ts-node example/scripts/workflow-demo.ts |
| 178 | + |
| 179 | +# 增强工作流演示 |
| 180 | +npx ts-node example/scripts/enhanced-workflow-demo.ts |
| 181 | +``` |
0 commit comments