UNPKG

@patchworkdev/pdk

Version:

Patchwork Development Kit

160 lines (159 loc) 7.22 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 crypto_1 = __importDefault(require("crypto")); const path_1 = __importDefault(require("path")); const generate_1 = require("../../generate"); class GeneratorService { lockFile; configPath; generators; generatorOrder; constructor(configPath, lockFile) { this.configPath = configPath; this.lockFile = lockFile; this.generators = this.initializeGenerators(); this.generatorOrder = ['contracts', 'deployScripts', 'buildContracts', 'abis', 'schema', 'eventHooks', 'ponderConfig', 'api', 'reactHooks']; } async runGenerateContracts() { const targetDir = path_1.default.dirname(this.configPath); await this.generateContracts(targetDir, false, this.configPath); } async runGenerateDeployScripts() { const targetDir = path_1.default.dirname(this.configPath); await this.generateDeployScripts(targetDir, false, this.configPath); } async runGenerateAPI() { const schemaPath = path_1.default.join(path_1.default.dirname(this.configPath), 'ponder', 'ponder.schema.ts'); const apiOutputDir = path_1.default.join(path_1.default.dirname(this.configPath), 'ponder', 'src', 'generated'); await (0, generate_1.generateAPI)(schemaPath, apiOutputDir); } async generateContracts(targetDir, useLocalPackages, configPath) { const outputDir = './contracts/src'; const pdkCommand = useLocalPackages ? 'pdk' : path_1.default.join(targetDir, 'node_modules', '.bin', 'pdk'); const { execa } = await import('execa'); const { oraPromise } = await import('ora'); await oraPromise(execa(pdkCommand, ['generate', 'contracts', configPath, '-o', outputDir], { cwd: targetDir, }), { text: `Generating contracts`, failText: 'Failed to generate contracts', successText: `Contracts generated successfully`, }); } async generateDeployScripts(targetDir, useLocalPackages, configPath) { const outputDir = './contracts/script'; const pdkCommand = useLocalPackages ? 'pdk' : path_1.default.join(targetDir, 'node_modules', '.bin', 'pdk'); const { execa } = await import('execa'); const { oraPromise } = await import('ora'); await oraPromise(execa(pdkCommand, ['generate', 'deployScripts', configPath, '-o', outputDir, '-c', '../src'], { cwd: targetDir, }), { text: `Generating deploy scripts`, failText: 'Failed to generate deploy scripts', successText: `Deploy scripts generated successfully`, }); } async buildContracts() { const targetDir = path_1.default.dirname(this.configPath); const pdkCommand = path_1.default.join(targetDir, 'node_modules', '.bin', 'pdk'); const { execa } = await import('execa'); const { oraPromise } = await import('ora'); await oraPromise(execa(pdkCommand, ['generate', 'contractBuild', this.configPath], { cwd: targetDir, }), { text: 'Building contracts', failText: 'Failed to build contracts', successText: 'Contracts built successfully', }); } initializeGenerators() { return { contracts: { inputs: ['patchwork.config.ts'], outputs: ['contracts/src/**/*.sol'], run: () => this.runGenerateContracts(), }, deployScripts: { inputs: ['patchwork.config.ts', 'contracts/src/**/*.sol'], outputs: ['contracts/script/**/*.sol'], run: () => this.runGenerateDeployScripts(), }, buildContracts: { inputs: ['contracts/src/**/*.sol', 'contracts/script/**/*.sol'], outputs: ['contracts/out/**/*.json'], run: () => this.buildContracts(), }, abis: { inputs: ['contracts/out/**/*.abi.json'], outputs: ['ponder/abis/**/*.ts'], run: () => (0, generate_1.generateABIs)(this.configPath), }, schema: { inputs: ['ponder/abis/**/*.ts', 'patchwork.config.ts'], outputs: ['ponder/ponder.schema.ts'], run: () => (0, generate_1.generateSchema)(this.configPath), }, eventHooks: { inputs: ['ponder/abis/**/*.ts', 'ponder/ponder.schema.ts', 'patchwork.config.ts'], outputs: ['ponder/src/generated/events.ts'], run: () => (0, generate_1.generateEventHooks)(this.configPath), }, ponderConfig: { inputs: ['ponder/abis/**/*.ts', 'ponder/ponder.schema.ts'], outputs: ['ponder/ponder.config.ts'], run: () => (0, generate_1.generatePonderConfig)(this.configPath), }, api: { inputs: ['ponder/ponder.schema.ts'], outputs: ['ponder/src/generated/api.ts'], run: () => this.runGenerateAPI(), }, reactHooks: { inputs: ['ponder/src/generated/api.ts'], outputs: ['www/generated/hooks/index.ts'], run: () => (0, generate_1.generateReactHooks)(this.configPath), }, }; } async calculateFilesHash(patterns) { const hash = crypto_1.default.createHash('sha256'); for (const pattern of patterns) { const files = await this.lockFile.getMatchingFiles(pattern); const sortedFiles = files.sort(); for (const file of sortedFiles) { const content = await this.lockFile.calculateFileHash(file); hash.update(`${file}:${content}`); } } return hash.digest('hex'); } getGeneratorStateKey(generator) { return `generator:${generator}`; } async hasInputsChanged(generator) { const config = this.generators[generator]; const stateKey = this.getGeneratorStateKey(generator); const previousHash = this.lockFile.getFileHash(stateKey); if (!previousHash) return true; const currentInputHash = await this.calculateFilesHash(config.inputs); return currentInputHash !== previousHash; } async processGenerators() { for (const generator of this.generatorOrder) { if (await this.hasInputsChanged(generator)) { console.info(`Running generator: ${generator}`); const config = this.generators[generator]; await config.run(this.configPath); const inputHash = await this.calculateFilesHash(config.inputs); const stateKey = this.getGeneratorStateKey(generator); this.lockFile.updateFileHash(stateKey, inputHash); } } } } exports.GeneratorService = GeneratorService;