UNPKG

@xec-sh/cli

Version:

Xec: The Universal Shell for TypeScript

158 lines 5.26 kB
import { TaskManager } from '../config/task-manager.js'; import { TargetResolver } from '../config/target-resolver.js'; import { ConfigurationManager } from '../config/configuration-manager.js'; export class TaskAPI { constructor() { this.configManager = new ConfigurationManager(); } async initialize() { if (!this.manager) { await this.configManager.load(); this.targetResolver = new TargetResolver(this.configManager.getConfig()); this.manager = new TaskManager({ configManager: this.configManager }); } } async list(filter) { await this.initialize(); const config = this.configManager.getConfig(); const tasks = config.tasks || {}; let taskList = Object.entries(tasks).map(([name, def]) => { const taskDef = typeof def === 'string' ? { command: def } : def; return { name, ...taskDef }; }); if (filter) { const pattern = new RegExp(filter, 'i'); taskList = taskList.filter(task => pattern.test(task.name)); } return taskList; } async get(name) { await this.initialize(); const config = this.configManager.getConfig(); const taskDef = config.tasks?.[name]; if (!taskDef) { return undefined; } const definition = typeof taskDef === 'string' ? { command: taskDef } : taskDef; return { name, ...definition }; } async run(name, params = {}, options = {}) { await this.initialize(); const taskDef = await this.get(name); if (!taskDef) { throw new Error(`Task '${name}' not found`); } const result = await this.manager.run(name, params, { target: options.target, timeout: options.timeout, env: options.env }); return { success: result.success, error: result.error, duration: result.duration, outputs: {}, steps: result.steps?.map(step => ({ name: step.name || 'unnamed', success: step.success, output: step.output, error: step.error, duration: step.duration })) }; } async create(name, definition) { await this.initialize(); const config = this.configManager.getConfig(); if (!config.tasks) { config.tasks = {}; } const { name: _, ...taskDef } = definition; config.tasks[name] = taskDef; await this.configManager.save(); } async update(name, definition) { await this.initialize(); const config = this.configManager.getConfig(); if (!config.tasks?.[name]) { throw new Error(`Task '${name}' not found`); } const existing = config.tasks[name]; const updated = typeof existing === 'string' ? { command: existing, ...definition } : { ...existing, ...definition }; const { name: _, ...taskDef } = updated; config.tasks[name] = taskDef; await this.configManager.save(); } async delete(name) { await this.initialize(); const config = this.configManager.getConfig(); if (!config.tasks?.[name]) { throw new Error(`Task '${name}' not found`); } delete config.tasks[name]; await this.configManager.save(); } async exists(name) { const task = await this.get(name); return task !== undefined; } async getHistory(name, limit = 10) { return []; } async runSequence(taskNames, params = {}, options = {}) { const results = []; for (const taskName of taskNames) { const result = await this.run(taskName, params, options); results.push(result); if (!result.success && !options.parallel) { break; } } return results; } async runParallel(taskNames, params = {}, options = {}) { const promises = taskNames.map(taskName => this.run(taskName, params, { ...options, parallel: true })); return Promise.all(promises); } async dryRun(name, params = {}) { await this.initialize(); const task = await this.get(name); if (!task) { throw new Error(`Task '${name}' not found`); } const commands = []; if (task.command) { commands.push(task.command); } if (task.steps) { for (const step of task.steps) { if (typeof step === 'string') { commands.push(step); } else if (step.command) { commands.push(step.command); } else if (step.task) { commands.push(`[Task: ${step.task}]`); } } } return commands; } } export const tasks = new TaskAPI(); //# sourceMappingURL=task-api.js.map