UNPKG

hardhat-deployed-records

Version:
48 lines (47 loc) 2.47 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-migrate", "Migrate 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; // if the deployed folder not exists, throw an error if (!fs_1.default.existsSync(configs.deployedDir)) { throw new plugins_1.HardhatPluginError(constants_1.PLUGIN_NAME, "Please initialize first by executing 'deployed-init' task"); } const { contracts } = await (0, helpers_2.parseArtifacts)(hre, configs.ignoreContracts); // write new `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: "w", }); // migrate `<network>/contracts.json` file with: // 1. contracts changes(no change, added, removed) // 2. has deployed contract addresses fs_1.default.readdirSync(configs.deployedDir, { recursive: false }).forEach((network) => { const manifest = `${configs.deployedDir}/${network}/${constants_1.CONTRACTS_JSON_FILE}`; // NOTICE: `fs.existsSync('scripts/deployed/index.ts/contracts.json')` will be `false` if (fs_1.default.existsSync(manifest)) { // read old `contracts.json` file const content = fs_1.default.readFileSync(manifest, "utf-8"); const existsJson = JSON.parse(content); // write new `contracts.json` file const data = (0, helpers_1.paths2json)(existsJson, contracts, constants_1.DEFAULT_DEPLOYED_ADDRESS); fs_1.default.writeFileSync(manifest, data, { flag: "w", }); } }); });