|
| 1 | +import { |
| 2 | + BatchConfig, |
| 3 | + BatchCommand, |
| 4 | + BatchSettings, |
| 5 | + NonInteractiveDefaults, |
| 6 | + ErrorHandlingStrategy, |
| 7 | + OutputFormat, |
| 8 | + JSONBatchFile, |
| 9 | + YAMLBatchFile, |
| 10 | +} from "../types/batch-types" |
| 11 | +import { JSONBatchParser } from "./JSONBatchParser" |
| 12 | +import { YAMLBatchParser } from "./YAMLBatchParser" |
| 13 | +import { TextBatchParser } from "./TextBatchParser" |
| 14 | +import * as fs from "fs/promises" |
| 15 | +import * as path from "path" |
| 16 | + |
| 17 | +export class BatchFileParser { |
| 18 | + private jsonParser: JSONBatchParser |
| 19 | + private yamlParser: YAMLBatchParser |
| 20 | + private textParser: TextBatchParser |
| 21 | + |
| 22 | + constructor() { |
| 23 | + this.jsonParser = new JSONBatchParser() |
| 24 | + this.yamlParser = new YAMLBatchParser() |
| 25 | + this.textParser = new TextBatchParser() |
| 26 | + } |
| 27 | + |
| 28 | + async parseFile(filePath: string): Promise<BatchConfig> { |
| 29 | + const content = await fs.readFile(filePath, "utf-8") |
| 30 | + const extension = path.extname(filePath).toLowerCase() |
| 31 | + |
| 32 | + switch (extension) { |
| 33 | + case ".json": |
| 34 | + return this.parseJSON(JSON.parse(content)) |
| 35 | + case ".yaml": |
| 36 | + case ".yml": |
| 37 | + return this.parseYAML(content) |
| 38 | + case ".txt": |
| 39 | + default: |
| 40 | + return this.parseText(content) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + parseJSON(data: any): BatchConfig { |
| 45 | + return this.jsonParser.parse(data) |
| 46 | + } |
| 47 | + |
| 48 | + parseYAML(content: string): BatchConfig { |
| 49 | + return this.yamlParser.parse(content) |
| 50 | + } |
| 51 | + |
| 52 | + parseText(content: string): BatchConfig { |
| 53 | + return this.textParser.parse(content) |
| 54 | + } |
| 55 | + |
| 56 | + async validateBatchFile(filePath: string): Promise<{ |
| 57 | + valid: boolean |
| 58 | + errors: string[] |
| 59 | + warnings: string[] |
| 60 | + }> { |
| 61 | + try { |
| 62 | + const config = await this.parseFile(filePath) |
| 63 | + return this.validateBatchConfig(config) |
| 64 | + } catch (error) { |
| 65 | + return { |
| 66 | + valid: false, |
| 67 | + errors: [error instanceof Error ? error.message : String(error)], |
| 68 | + warnings: [], |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private validateBatchConfig(config: BatchConfig): { |
| 74 | + valid: boolean |
| 75 | + errors: string[] |
| 76 | + warnings: string[] |
| 77 | + } { |
| 78 | + const errors: string[] = [] |
| 79 | + const warnings: string[] = [] |
| 80 | + |
| 81 | + // Validate commands |
| 82 | + if (!config.commands || config.commands.length === 0) { |
| 83 | + errors.push("At least one command is required") |
| 84 | + } |
| 85 | + |
| 86 | + config.commands.forEach((cmd, index) => { |
| 87 | + if (!cmd.id) { |
| 88 | + errors.push(`Command at index ${index} is missing required 'id' field`) |
| 89 | + } |
| 90 | + if (!cmd.command) { |
| 91 | + errors.push(`Command '${cmd.id}' is missing required 'command' field`) |
| 92 | + } |
| 93 | + |
| 94 | + // Validate dependencies |
| 95 | + if (cmd.dependsOn) { |
| 96 | + const invalidDeps = cmd.dependsOn.filter((dep) => !config.commands.some((c) => c.id === dep)) |
| 97 | + if (invalidDeps.length > 0) { |
| 98 | + errors.push(`Command '${cmd.id}' has invalid dependencies: ${invalidDeps.join(", ")}`) |
| 99 | + } |
| 100 | + |
| 101 | + // Check for circular dependencies |
| 102 | + if (this.hasCircularDependency(config.commands, cmd.id)) { |
| 103 | + errors.push(`Circular dependency detected for command '${cmd.id}'`) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // Validate timeout |
| 108 | + if (cmd.timeout && cmd.timeout <= 0) { |
| 109 | + warnings.push(`Command '${cmd.id}' has invalid timeout value: ${cmd.timeout}`) |
| 110 | + } |
| 111 | + |
| 112 | + // Validate retries |
| 113 | + if (cmd.retries && cmd.retries < 0) { |
| 114 | + warnings.push(`Command '${cmd.id}' has invalid retries value: ${cmd.retries}`) |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + // Validate settings |
| 119 | + if (config.settings.maxConcurrency && config.settings.maxConcurrency <= 0) { |
| 120 | + errors.push("maxConcurrency must be greater than 0") |
| 121 | + } |
| 122 | + |
| 123 | + return { |
| 124 | + valid: errors.length === 0, |
| 125 | + errors, |
| 126 | + warnings, |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private hasCircularDependency( |
| 131 | + commands: BatchCommand[], |
| 132 | + commandId: string, |
| 133 | + visited: Set<string> = new Set(), |
| 134 | + ): boolean { |
| 135 | + if (visited.has(commandId)) { |
| 136 | + return true |
| 137 | + } |
| 138 | + |
| 139 | + const command = commands.find((c) => c.id === commandId) |
| 140 | + if (!command || !command.dependsOn) { |
| 141 | + return false |
| 142 | + } |
| 143 | + |
| 144 | + visited.add(commandId) |
| 145 | + |
| 146 | + for (const depId of command.dependsOn) { |
| 147 | + if (this.hasCircularDependency(commands, depId, new Set(visited))) { |
| 148 | + return true |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return false |
| 153 | + } |
| 154 | + |
| 155 | + getDefaultBatchConfig(): BatchConfig { |
| 156 | + return { |
| 157 | + commands: [], |
| 158 | + settings: { |
| 159 | + parallel: false, |
| 160 | + maxConcurrency: 1, |
| 161 | + continueOnError: false, |
| 162 | + verbose: false, |
| 163 | + dryRun: false, |
| 164 | + outputFormat: OutputFormat.TEXT, |
| 165 | + }, |
| 166 | + defaults: { |
| 167 | + confirmations: false, |
| 168 | + fileOverwrite: false, |
| 169 | + createDirectories: true, |
| 170 | + timeout: 300000, // 5 minutes |
| 171 | + retryCount: 3, |
| 172 | + }, |
| 173 | + errorHandling: ErrorHandlingStrategy.FAIL_FAST, |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + async generateSampleBatchFile(filePath: string, format: "json" | "yaml" | "text" = "json"): Promise<void> { |
| 178 | + const sampleConfig = this.createSampleConfig() |
| 179 | + |
| 180 | + let content: string |
| 181 | + |
| 182 | + const batchConfig: BatchConfig = { |
| 183 | + commands: sampleConfig.commands, |
| 184 | + settings: sampleConfig.settings, |
| 185 | + defaults: sampleConfig.defaults, |
| 186 | + errorHandling: ErrorHandlingStrategy.FAIL_FAST, |
| 187 | + } |
| 188 | + |
| 189 | + switch (format) { |
| 190 | + case "json": |
| 191 | + content = JSON.stringify(sampleConfig, null, 2) |
| 192 | + break |
| 193 | + case "yaml": |
| 194 | + content = this.yamlParser.stringify(batchConfig) |
| 195 | + break |
| 196 | + case "text": |
| 197 | + content = this.textParser.stringify(batchConfig) |
| 198 | + break |
| 199 | + default: |
| 200 | + throw new Error(`Unsupported format: ${format}`) |
| 201 | + } |
| 202 | + |
| 203 | + // Ensure directory exists |
| 204 | + const dir = path.dirname(filePath) |
| 205 | + await fs.mkdir(dir, { recursive: true }) |
| 206 | + |
| 207 | + // Write sample file |
| 208 | + await fs.writeFile(filePath, content, "utf-8") |
| 209 | + } |
| 210 | + |
| 211 | + private createSampleConfig(): JSONBatchFile { |
| 212 | + return { |
| 213 | + version: "1.0", |
| 214 | + settings: { |
| 215 | + parallel: false, |
| 216 | + maxConcurrency: 3, |
| 217 | + continueOnError: false, |
| 218 | + verbose: true, |
| 219 | + dryRun: false, |
| 220 | + outputFormat: OutputFormat.JSON, |
| 221 | + }, |
| 222 | + defaults: { |
| 223 | + confirmations: false, |
| 224 | + fileOverwrite: false, |
| 225 | + createDirectories: true, |
| 226 | + timeout: 300000, |
| 227 | + retryCount: 3, |
| 228 | + }, |
| 229 | + commands: [ |
| 230 | + { |
| 231 | + id: "setup", |
| 232 | + command: "echo", |
| 233 | + args: ["Setting up environment"], |
| 234 | + environment: { |
| 235 | + NODE_ENV: "development", |
| 236 | + }, |
| 237 | + timeout: 30000, |
| 238 | + }, |
| 239 | + { |
| 240 | + id: "install", |
| 241 | + command: "npm", |
| 242 | + args: ["install"], |
| 243 | + dependsOn: ["setup"], |
| 244 | + retries: 2, |
| 245 | + }, |
| 246 | + { |
| 247 | + id: "test", |
| 248 | + command: "npm", |
| 249 | + args: ["test"], |
| 250 | + dependsOn: ["install"], |
| 251 | + condition: { |
| 252 | + type: "file_exists", |
| 253 | + value: "package.json", |
| 254 | + }, |
| 255 | + }, |
| 256 | + ], |
| 257 | + } |
| 258 | + } |
| 259 | +} |
0 commit comments