@nomyx/hardhat-adminui
Version:
A comprehensive Hardhat plugin providing a web-based admin UI for deployed smart contracts with Diamond proxy support, contract interaction, event monitoring, and deployment dashboard.
127 lines (126 loc) • 5.41 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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScenarioDiscovery = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class ScenarioDiscovery {
constructor(scenariosPath) {
this.scenariosPath = scenariosPath;
}
async listScenarios() {
const scenarioFiles = this.findScenarioFiles(this.scenariosPath);
return scenarioFiles.map(file => {
const baseName = path.basename(file);
// Remove extensions to get the scenario name
return baseName.replace(/\.(scenario\.json|js|ts)$/, '');
});
}
async loadScenario(name) {
const scenarioFiles = this.findScenarioFiles(this.scenariosPath);
// Look for the scenario file - prefer .scenario.json, fallback to .js/.ts
const scenarioFile = scenarioFiles.find(file => {
const baseName = path.basename(file);
return baseName === `${name}.scenario.json` ||
baseName === `${name}.js` ||
baseName === `${name}.ts` ||
baseName.replace(/\.(scenario\.json|js|ts)$/, '') === name;
});
if (!scenarioFile) {
throw new Error(`Scenario not found: ${name} (looking for ${name}.scenario.json, ${name}.js, or ${name}.ts)`);
}
const content = fs.readFileSync(scenarioFile, "utf-8");
if (scenarioFile.endsWith('.scenario.json')) {
try {
return JSON.parse(content);
}
catch (parseError) {
throw new Error(`Failed to parse scenario file ${scenarioFile}: ${parseError instanceof Error ? parseError.message : 'Invalid JSON'}`);
}
}
else {
// For JS/TS files, try to evaluate and extract the module.exports
try {
// Create a minimal context to evaluate the JS file
const moduleObj = { exports: {} };
const requireFn = (dep) => {
// Basic require implementation for common modules
if (dep === 'path')
return require('path');
if (dep === 'fs')
return require('fs');
throw new Error(`Module not supported in scenario context: ${dep}`);
};
// Use Function constructor to safely evaluate the JS content
const evalFunction = new Function('module', 'exports', 'require', content);
evalFunction(moduleObj, moduleObj.exports, requireFn);
return moduleObj.exports;
}
catch (evalError) {
throw new Error(`Failed to evaluate scenario file ${scenarioFile}: ${evalError instanceof Error ? evalError.message : 'Evaluation error'}`);
}
}
}
async saveScenario(name, content) {
// Ensure scenarios directory exists
if (!fs.existsSync(this.scenariosPath)) {
fs.mkdirSync(this.scenariosPath, { recursive: true });
}
// Save as scenario.json file
const filePath = path.join(this.scenariosPath, `${name}.scenario.json`);
fs.writeFileSync(filePath, content, "utf-8");
}
findScenarioFiles(dir) {
if (!fs.existsSync(dir)) {
return [];
}
let files = [];
const items = fs.readdirSync(dir);
for (const item of items) {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
files = files.concat(this.findScenarioFiles(fullPath));
}
else if (fullPath.endsWith(".scenario.json") ||
(fullPath.endsWith(".js") && item.includes("test")) ||
(fullPath.endsWith(".js") && item.includes("scenario"))) {
files.push(fullPath);
}
}
return files;
}
}
exports.ScenarioDiscovery = ScenarioDiscovery;