hardhat-deployed-records
Version:
Hardhat plugin for deploying records
67 lines (60 loc) • 1.62 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.indexDotTSFile = exports.path2json = void 0;
function path2json(paths, defaultValue) {
const result = {};
for (const path of paths) {
const arr = path.split("/");
if (arr.length == 1) {
result[path] = defaultValue;
}
else {
let current = result;
for (let i = 0; i < arr.length; i++) {
const path = arr[i];
if (i == arr.length - 1) {
current[path] = defaultValue;
}
else {
current[path] = current[path] == null ? {} : current[path];
current = current[path];
}
}
}
}
return JSON.stringify(result, null, 4);
}
exports.path2json = path2json;
function indexDotTSFile(types) {
return `import hre from "hardhat";
import fs from "fs";
import path from "path";
const network = hre.network.name;
const manifest = path.join(__dirname, network, "contracts.json");
const data = readSync();
function readSync() {
try {
const content = fs.readFileSync(manifest, "utf-8");
return JSON.parse(content);
} catch (err) {
console.error(err);
}
}
function saveSync() {
const content = JSON.stringify(data, null, 4);
try {
fs.writeFileSync(manifest, content);
} catch (err) {
console.error(err);
}
}
const deployed = {
contracts: data,
setContract: function (name: ${types}, addr: string) {
this.contracts[name] = addr;
saveSync();
},
};
export default deployed;`;
}
exports.indexDotTSFile = indexDotTSFile;