@patchworkdev/pdk
Version:
Patchwork Development Kit
52 lines (51 loc) • 2.03 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaskService = void 0;
const path_1 = __importDefault(require("path"));
const logger_1 = require("../../../common/helpers/logger");
const tsLoader_1 = require("../../../common/helpers/tsLoader");
class TaskService {
configPath;
constructor(configPath) {
this.configPath = configPath;
}
async loadTasks() {
try {
const configDir = path_1.default.dirname(path_1.default.isAbsolute(this.configPath) ? this.configPath : path_1.default.resolve(process.cwd(), this.configPath));
const tasksPath = path_1.default.join(configDir, 'tasks/index.ts');
logger_1.logger.debug(`Loading tasks from ${tasksPath}`);
const tasksModule = await (0, tsLoader_1.tsLoader)(tasksPath, {
moduleOverrides: {
'@patchworkdev/pdk/utils': '../../exports',
},
});
return tasksModule.tasks || [];
}
catch (error) {
logger_1.logger.warn(`No custom tasks found: ${error}`);
return [];
}
}
async runTasks(params) {
const tasks = await this.loadTasks();
if (!tasks.length) {
return logger_1.logger.info('No tasks to execute.');
}
const enabledTasks = tasks.filter((task) => task.enabled);
for (const task of enabledTasks) {
logger_1.logger.info(`Executing task: ${task.name} - ${task.description}`);
try {
await task.execute(params);
logger_1.logger.info(`Successfully completed task: ${task.name}`);
}
catch (error) {
logger_1.logger.error(`Failed to execute task ${task.name}:`, error);
throw error;
}
}
}
}
exports.TaskService = TaskService;