@patchworkdev/pdk
Version:
Patchwork Development Kit
160 lines (159 loc) • 6.73 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findConfig = findConfig;
exports.findPonderSchema = findPonderSchema;
exports.loadPonderSchema = loadPonderSchema;
exports.importPatchworkConfig = importPatchworkConfig;
exports.importABIFiles = importABIFiles;
exports.getFragmentRelationships = getFragmentRelationships;
const promises_1 = __importDefault(require("fs/promises"));
const path = __importStar(require("path"));
const error_1 = require("./error");
const logger_1 = require("./logger");
const project_1 = require("./project");
const tsLoader_1 = require("./tsLoader");
async function findFileUpwards(directory, filename) {
const filePath = path.join(directory, filename);
try {
await promises_1.default.access(filePath);
return filePath;
}
catch {
// File does not exist
}
const parentDirectory = path.dirname(directory);
if (parentDirectory === directory) {
return null;
}
return findFileUpwards(parentDirectory, filename);
}
async function findConfig() {
const configFileName = 'patchwork.config.ts';
const currentDirectory = process.cwd();
return findFileUpwards(currentDirectory, configFileName);
}
async function findPonderSchema() {
const schemaFileName = 'ponder.schema.ts';
const currentDirectory = process.cwd();
return findFileUpwards(currentDirectory, schemaFileName);
}
async function loadPonderSchema(ponderSchema) {
const mock = path.resolve(__dirname, './ponderSchemaMock');
try {
return await (0, tsLoader_1.tsLoader)(ponderSchema, {
compilerOptions: {
strict: false,
noImplicitAny: false,
},
moduleOverrides: {
'@ponder/core': mock,
},
});
}
catch (error) {
logger_1.logger.error(error);
if (error instanceof TypeError && error.message.includes('is not a function')) {
logger_1.logger.error('Error: It seems a method is missing from our mock implementation.');
logger_1.logger.error('Full error:', error);
logger_1.logger.error('Please add this method to the mockSchemaBuilder in ponderSchemaMocks.ts');
}
throw new error_1.PDKError(error_1.ErrorCode.MOCK_NOT_FOUND, `Missing mock implementation in ponderSchemaMocks.ts`);
}
}
// Imports a ProjectConfig ts file and returns a fully loaded PatchworkProject
async function importPatchworkConfig(config) {
try {
// Resolve the full path
const fullPath = path.isAbsolute(config) ? config : path.resolve(process.cwd(), config);
let loadedConfig = (await (0, tsLoader_1.tsLoader)(fullPath, {
moduleOverrides: {
'@patchworkdev/pdk/plugins': path.resolve(__dirname, '..', '..', 'plugins'),
},
})).default;
// TODO how can we check that this is a ProjectConfig and not a PatchworkProject?
let project = (0, project_1.importProjectConfig)(loadedConfig);
return project;
}
catch (error) {
if (error instanceof Error) {
throw new error_1.PDKError(error_1.ErrorCode.PROJECT_CONFIG_ERROR, error.message);
}
else {
throw new error_1.PDKError(error_1.ErrorCode.PROJECT_CONFIG_ERROR, `Error importing ProjectConfig at ${config}`, error);
}
}
}
async function importABIFiles(abiDir) {
try {
await promises_1.default.access(abiDir);
}
catch (error) {
logger_1.logger.error(`- ABI directory not found: ${abiDir}`);
throw new error_1.PDKError(error_1.ErrorCode.DIR_NOT_FOUND, `ABI directory not found at ${abiDir}`);
}
const abiObjects = {};
try {
// Read the directory
const abiFiles = (await promises_1.default.readdir(abiDir)).filter((file) => file.endsWith('.abi.ts'));
// Dynamically import all ABI files
// -- NOTE: Seems like we should prune this? There's a LOT of artifacts
// -- NOTE: Should consider only importing ABIs for project's contracts + PatchworkProtocol
await Promise.all(abiFiles.map(async (file) => {
const filePath = path.join(abiDir, file);
const module = await (0, tsLoader_1.tsLoader)(filePath);
const baseName = path.basename(file, '.abi.ts');
abiObjects[baseName] = module[baseName];
// Return the exported constant
return { name: baseName, abi: module[baseName] };
}));
}
catch (error) {
logger_1.logger.error('Error importing ABI files:', error);
throw new error_1.PDKError(error_1.ErrorCode.ABI_IMPORT_ERROR, `Error importing ABIs at ${abiDir}`);
}
if (Object.keys(abiObjects).length === 0) {
logger_1.logger.error(`Error: No ABI files found in ${abiDir}`);
throw new error_1.PDKError(error_1.ErrorCode.ABI_IMPORT_ERROR, `Error: No ABI files found in ${abiDir}`);
}
return abiObjects;
}
function getFragmentRelationships(projectConfig) {
const fragmentRelationships = {};
Object.entries(projectConfig.contracts).forEach(([contractName, contractConfig]) => {
if (typeof contractConfig !== 'string') {
contractConfig.fragments?.forEach((fragment) => {
if (!fragmentRelationships[fragment]) {
fragmentRelationships[fragment] = [];
}
fragmentRelationships[fragment].push(contractName);
});
}
});
return fragmentRelationships;
}