@solarity/hardhat-migrate
Version:
The simplest way to deploy smart contracts
109 lines • 4.37 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VerificationStorage = exports.ArtifactStorage = exports.TransactionStorage = exports.UserStorage = exports.DefaultStorage = exports.MigrateStorage = void 0;
exports.clearAllStorage = clearAllStorage;
const fs_1 = require("fs");
const plugins_1 = require("hardhat/plugins");
const errors_1 = require("../../errors");
const utils_1 = require("../../utils");
const tools_1 = require("../../types/tools");
let BaseStorage = class BaseStorage {
_hre;
_namespace;
_directory = "cache";
_fileName = ".migrate.storage.json";
_state;
constructor(_hre, _namespace = tools_1.StorageNamespaces.Storage) {
this._hre = _hre;
this._namespace = _namespace;
this._state = this.readFullStateFromFile()[this._namespace] || {};
if (!(0, fs_1.existsSync)(this.filePath())) {
this._saveStateToFile();
}
}
deleteStateFile() {
if (this.stateExistsInFile()) {
(0, fs_1.rmSync)(this.filePath(), { force: true });
}
}
readFullStateFromFile() {
if (!this.stateExistsInFile()) {
return {};
}
const fileContent = (0, fs_1.readFileSync)(this.filePath(), {
encoding: "utf8",
});
return JSON.parse(fileContent);
}
stateExistsInFile() {
return (0, fs_1.existsSync)(this.filePath());
}
filePath() {
return (0, utils_1.resolvePathToFile)(this._hre, "cache", this._fileName);
}
_saveStateToFile() {
const fileSate = this.readFullStateFromFile();
fileSate[this._namespace] = this._state;
if (!(0, fs_1.existsSync)(this._directory)) {
(0, fs_1.mkdirSync)(this._directory, { recursive: true });
}
(0, fs_1.writeFileSync)(this.filePath(), (0, utils_1.toJSON)(fileSate), {
flag: "w",
encoding: "utf8",
});
}
};
BaseStorage = __decorate([
utils_1.catchError
], BaseStorage);
let MigrateStorage = class MigrateStorage extends BaseStorage {
get(key) {
return this._state[key];
}
getAll() {
return this._state;
}
set(key, value, force = false) {
if (!force && this.has(key)) {
throw new errors_1.MigrateError(`Key already exists`);
}
this._state[key] = value;
this._saveStateToFile();
}
delete(key, force = false) {
if (!force && !this.has(key)) {
throw new errors_1.MigrateError(`Key not found`);
}
delete this._state[key];
this._saveStateToFile();
}
has(key) {
return this._state[key] !== undefined;
}
clear() {
this._state = {};
this._saveStateToFile();
}
};
exports.MigrateStorage = MigrateStorage;
exports.MigrateStorage = MigrateStorage = __decorate([
utils_1.catchError
], MigrateStorage);
exports.DefaultStorage = (0, plugins_1.lazyObject)(() => new BaseStorage(require("hardhat")));
exports.UserStorage = (0, plugins_1.lazyObject)(() => new MigrateStorage(require("hardhat"), tools_1.StorageNamespaces.Storage));
exports.TransactionStorage = (0, plugins_1.lazyObject)(() => new MigrateStorage(require("hardhat"), tools_1.StorageNamespaces.Transactions));
exports.ArtifactStorage = (0, plugins_1.lazyObject)(() => new MigrateStorage(require("hardhat"), tools_1.StorageNamespaces.Artifacts));
exports.VerificationStorage = (0, plugins_1.lazyObject)(() => new MigrateStorage(require("hardhat"), tools_1.StorageNamespaces.Verification));
function clearAllStorage() {
exports.UserStorage.clear();
exports.TransactionStorage.clear();
exports.ArtifactStorage.clear();
exports.VerificationStorage.clear();
}
//# sourceMappingURL=MigrateStorage.js.map