@patchworkdev/pdk
Version:
Patchwork Development Kit
82 lines (81 loc) • 4.07 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateABIs = generateABIs;
const promises_1 = __importDefault(require("fs/promises"));
const path_1 = __importDefault(require("path"));
const error_1 = require("../common/helpers/error");
const logger_1 = require("../common/helpers/logger");
async function getAbiJsonFiles(directory) {
const files = [];
try {
const entries = await promises_1.default.readdir(directory, { withFileTypes: true });
for (const entry of entries) {
const filePath = path_1.default.join(directory, entry.name);
if (entry.isDirectory()) {
logger_1.logger.debug('Scanning directory:', filePath);
const subFiles = await getAbiJsonFiles(filePath);
files.push(...subFiles);
}
else if (entry.isFile() && entry.name.endsWith('.abi.json')) {
logger_1.logger.debug('Found ABI file:', entry.name);
files.push(filePath);
}
}
return files;
}
catch (error) {
logger_1.logger.error(`Error scanning directory ${directory}:`, error);
throw new error_1.PDKError(error_1.ErrorCode.DIR_NOT_FOUND, `Failed to scan directory ${directory}`);
}
}
async function generateABIs(configPath) {
const buildOutDir = path_1.default.join(path_1.default.dirname(configPath), 'contracts', 'out');
const abiDir = path_1.default.join(path_1.default.dirname(configPath), 'ponder', 'abis');
logger_1.logger.debug('Build output directory:', buildOutDir);
logger_1.logger.debug('ABI output directory:', abiDir);
try {
// Ensure ABI directory exists
await promises_1.default.mkdir(abiDir, { recursive: true });
// Get all ABI JSON files
const files = await getAbiJsonFiles(buildOutDir);
logger_1.logger.debug('Found ABI files:', files.length);
// Clean existing ABI directory
const outFiles = await promises_1.default.readdir(abiDir);
logger_1.logger.debug('Cleaning existing ABI files:', outFiles.length);
for (const file of outFiles) {
const filePath = path_1.default.join(abiDir, file);
await promises_1.default.unlink(filePath);
logger_1.logger.debug('Removed file:', file);
}
// Generate new ABI files
let indexContent = '';
for (const file of files) {
const baseName = path_1.default.basename(file, '.abi.json');
try {
// Read the content of the .abi.json file
const fileContent = await promises_1.default.readFile(file, { encoding: 'utf8' });
// Generate the content for the .abi.ts file
const tsContent = `export const ${baseName} = ${fileContent} as const;\n`;
indexContent += `export * from './${baseName}.abi';\n`;
// Write the .abi.ts file
const outputPath = path_1.default.join(abiDir, `${baseName}.abi.ts`);
await promises_1.default.writeFile(outputPath, tsContent);
logger_1.logger.debug(`Generated: ${baseName}.abi.ts`);
}
catch (error) {
logger_1.logger.error(`Failed to process ABI file ${baseName}:`, error);
throw new error_1.PDKError(error_1.ErrorCode.PDK_ERROR, `Failed to process ABI file ${baseName}`);
}
}
// Write the index file
await promises_1.default.writeFile(path_1.default.join(abiDir, 'index.ts'), indexContent);
logger_1.logger.info('Successfully generated all TypeScript ABIs');
}
catch (error) {
logger_1.logger.error('Failed to generate TypeScript ABIs:', error);
throw error instanceof error_1.PDKError ? error : new error_1.PDKError(error_1.ErrorCode.UNKNOWN_ERROR, 'Failed to generate TypeScript ABIs');
}
}