UNPKG

hardhat-deployed-records

Version:
44 lines (43 loc) 2.21 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const config_1 = require("hardhat/config"); const plugins_1 = require("hardhat/plugins"); const task_names_1 = require("hardhat/builtin-tasks/task-names"); const fs_1 = __importDefault(require("fs")); const mustache_1 = __importDefault(require("mustache")); const constants_1 = require("../constants"); const helpers_1 = require("./helpers"); const helpers_2 = require("./helpers"); const templates_1 = require("./templates"); (0, config_1.task)("deployed-init", "Initializes the deployed folder").setAction(async (taskArgs, hre) => { // run `compile` task before all operations await hre.run(task_names_1.TASK_COMPILE); // read configs from `hardhat.config.ts` const configs = hre.config.deployedRecords; // get network name const network = hre.network.name; // default value is `hardhat` // if the `deployed` folder already exists, throw an error if (fs_1.default.existsSync(configs.deployedDir)) { throw new plugins_1.HardhatPluginError(constants_1.PLUGIN_NAME, "Already initialized, don't initialize again"); } // sourceNames: ["PepeForkToken", "mock/MockERC20"] // contracts: [{contractName: "MockERC20", attrs: ["mock", "MockERC2"]}, ...] const { contracts } = await (0, helpers_2.parseArtifacts)(hre, configs.ignoreContracts); // create `deployed` folder fs_1.default.mkdirSync(configs.deployedDir); // write `deployed/index.ts` file const code = mustache_1.default.render(templates_1.indexFile, { contracts }); fs_1.default.writeFileSync(`${configs.deployedDir}/${constants_1.INDEX_TS_FILE}`, code, { flag: "a+", }); // create <network> folder fs_1.default.mkdirSync(`${configs.deployedDir}/${network}`); // write `<network>/contracts.json` file const data = (0, helpers_1.paths2json)({}, contracts, constants_1.DEFAULT_DEPLOYED_ADDRESS); fs_1.default.writeFileSync(`${configs.deployedDir}/${network}/${constants_1.CONTRACTS_JSON_FILE}`, data, { flag: "a+", }); });