UNPKG

@puls-atlas/cli

Version:

The Puls Atlas CLI tool for managing Atlas projects

53 lines 1.77 kB
import { isFunction, isString } from 'es-toolkit/compat'; import { logger } from './logger.js'; const normalizeOperation = operation => { if (!operation || !isFunction(operation.run)) { return null; } return { run: operation.run, name: isString(operation.name) && operation.name.trim().length > 0 ? operation.name.trim() : 'operation' }; }; const executeOperation = async (operation, loggerImpl = logger) => { const startedAt = new Date().toISOString(); loggerImpl.info(`Starting background operation: ${operation.name}`); try { const result = await operation.run(); const finishedAt = new Date().toISOString(); loggerImpl.info(`Completed background operation: ${operation.name}`); return { finishedAt, name: operation.name, result, startedAt, status: 'completed' }; } catch (error) { const finishedAt = new Date().toISOString(); loggerImpl.warning(`Background operation failed: ${operation.name}. ${error.message}`); return { error, finishedAt, name: operation.name, startedAt, status: 'failed' }; } }; export const runBackgroundOperations = async (operations = [], options = {}) => { const loggerImpl = options.loggerImpl ?? logger; const runInBackground = options.runInBackground !== false; const normalizedOperations = operations.map(normalizeOperation).filter(Boolean); if (normalizedOperations.length === 0) { return []; } if (runInBackground) { return Promise.all(normalizedOperations.map(operation => executeOperation(operation, loggerImpl))); } const outcomes = []; for (const operation of normalizedOperations) { outcomes.push(await executeOperation(operation, loggerImpl)); } return outcomes; };