UNPKG

@solarity/hardhat-migrate

Version:
127 lines 5.6 kB
"use strict"; 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.Migrator = void 0; const path_1 = require("path"); const fs_1 = require("fs"); const plugins_1 = require("hardhat/plugins"); const constants_1 = require("../constants"); const errors_1 = require("../errors"); const Deployer_1 = require("../deployer/Deployer"); const Linker_1 = require("../deployer/Linker"); const Stats_1 = require("../tools/Stats"); const TransactionRunner_1 = require("../tools/runners/TransactionRunner"); const Reporter_1 = require("../tools/reporters/Reporter"); const NetworkManager_1 = require("../tools/network/NetworkManager"); const MigrateStorage_1 = require("../tools/storage/MigrateStorage"); const ArtifactProcessor_1 = require("../tools/storage/ArtifactProcessor"); const TransactionProcessor_1 = require("../tools/storage/TransactionProcessor"); class Migrator { _hre; _config; _deployer; _migrationFiles; constructor(_hre, _config = _hre.config.migrate) { this._hre = _hre; this._config = _config; this._deployer = new Deployer_1.Deployer(_hre); this._migrationFiles = this._getMigrationFiles(); } async migrate() { Reporter_1.Reporter.reportMigrationBegin(this._migrationFiles); const migrationsDir = this._getMigrationDir(); for (const element of this._migrationFiles) { Stats_1.Stats.currentMigration = this._getMigrationNumber(element); Reporter_1.Reporter.reportMigrationFileBegin(element); try { const migration = await Promise.resolve(`${(0, path_1.join)(migrationsDir, element)}`).then(s => __importStar(require(s))); await migration.default(this._deployer); } catch (e) { if (e instanceof errors_1.MigrateError) { throw new plugins_1.HardhatPluginError(constants_1.pluginName, e.message, e); } throw e; } } TransactionRunner_1.TransactionRunner.summary(); await Reporter_1.Reporter?.completeReport(); } _getMigrationFiles() { const migrationsDir = this._getMigrationDir(); if (!(0, fs_1.existsSync)(migrationsDir)) { throw new plugins_1.HardhatPluginError(constants_1.pluginName, `Migrations directory not found at ${migrationsDir}`); } const directoryContents = (0, fs_1.readdirSync)(migrationsDir); const files = directoryContents .filter((file) => { const migrationNumber = this._getMigrationNumber(file); if (isNaN(migrationNumber) || migrationNumber <= 0 || this._config.filter.from > migrationNumber || (this._config.filter.to < migrationNumber && this._config.filter.to !== -1) || (this._config.filter.only !== migrationNumber && this._config.filter.only !== -1) || this._config.filter.skip === migrationNumber) { return false; } return (0, fs_1.statSync)((0, path_1.join)(migrationsDir, file)).isFile(); }) .sort((a, b) => { return this._getMigrationNumber(a) - this._getMigrationNumber(b); }); if (files.length === 0) { throw new plugins_1.HardhatPluginError(constants_1.pluginName, "No migration files were found."); } return files; } _getMigrationNumber(file) { return parseInt((0, path_1.basename)(file)); } _getMigrationDir() { return (0, path_1.join)(this._hre.config.paths.root, this._config.paths.pathToMigrations, this._config.paths.namespace); } static async buildMigrateTaskDeps(hre) { (0, Linker_1.createLinker)(hre); (0, TransactionProcessor_1.createTransactionProcessor)(hre.config.migrate); (0, NetworkManager_1.buildNetworkDeps)(hre); await (0, Reporter_1.createAndInitReporter)(hre); if (!hre.config.migrate.execution.continue) { (0, MigrateStorage_1.clearAllStorage)(); } await ArtifactProcessor_1.ArtifactProcessor.parseArtifacts(hre); } } exports.Migrator = Migrator; //# sourceMappingURL=Migrator.js.map