UNPKG

@patchworkdev/pdk

Version:

Patchwork Development Kit

110 lines (109 loc) 3.58 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GeneratorService = void 0; const listr2_1 = require("listr2"); const picocolors_1 = __importDefault(require("picocolors")); // import { saveContext } from '../utils/context'; class GeneratorService { // private context: PDKContext; generatorMap; lockFileManager; constructor(lockFileManager) { this.lockFileManager = lockFileManager; // this.context = context; this.generatorMap = new Map(); // Add built-in generators this.addBuiltInGenerators(); // Add plugin generators this.addPluginGenerators(); } /** * Add built-in generators to the map */ addBuiltInGenerators() { this.generatorMap // Contract generation task .set('contracts', { title: picocolors_1.default.bold('Generating contracts'), task: async (ctx, task) => { await new Promise((resolve) => setTimeout(resolve, 1000)); }, }) // Deploy script generation task .set('deploy', { title: picocolors_1.default.bold('Generating deploy scripts'), task: async (ctx, task) => { await new Promise((resolve) => setTimeout(resolve, 600)); }, }) // Build step task .set('artfacts', { title: picocolors_1.default.bold('Compiling contracts and generating artifacts'), task: async (ctx, task) => { await new Promise((resolve) => setTimeout(resolve, 1500)); }, }); } /** * Add plugin generators to the map */ addPluginGenerators() { const ctx = this.lockFileManager.getCtx(); [...ctx.config.plugins].forEach((plugin) => { if (plugin.generate) { this.generatorMap.set(plugin.name.toLowerCase(), { title: picocolors_1.default.bold(`Running ${plugin.name} generator`), task: (ctx, task) => plugin.generate({ context: ctx, task }), }); } }); } /** * Get a generator by key */ getGenerator(key) { return this.generatorMap.get(key.toLowerCase()); } /** * Get all generators in insertion order */ getAllGenerators() { return Array.from(this.generatorMap.values()); } /** * Get generator keys */ getGeneratorKeys() { return Array.from(this.generatorMap.keys()); } /** * Run a specific generator by key */ async runGenerator(key) { const task = this.getGenerator(key); if (!task) { throw new Error(`Unknown generator: ${key}`); } const listr = new listr2_1.Listr([task]); await listr.run(this.lockFileManager.getCtx()).then((ctx) => { this.lockFileManager.updateAndSaveCtx(ctx); }); } /** * Run all generators in insertion order */ async runAllGenerators() { const listr = new listr2_1.Listr(this.getAllGenerators(), { rendererOptions: { collapseSubtasks: true, }, }); await listr.run(this.lockFileManager.getCtx()).then((ctx) => { this.lockFileManager.updateAndSaveCtx(ctx); }); } } exports.GeneratorService = GeneratorService;