UNPKG

@solarity/hardhat-migrate

Version:
154 lines 7.06 kB
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) { function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; } var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value"; var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null; var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {}); var _, done = false; for (var i = decorators.length - 1; i >= 0; i--) { var context = {}; for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p]; for (var p in contextIn.access) context.access[p] = contextIn.access[p]; context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); }; var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context); if (kind === "accessor") { if (result === void 0) continue; if (result === null || typeof result !== "object") throw new TypeError("Object expected"); if (_ = accept(result.get)) descriptor.get = _; if (_ = accept(result.set)) descriptor.set = _; if (_ = accept(result.init)) initializers.unshift(_); } else if (_ = accept(result)) { if (kind === "field") initializers.unshift(_); else descriptor[key] = _; } } if (target) Object.defineProperty(target, contextIn.name, descriptor); done = true; }; var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) { var useValue = arguments.length > 2; for (var i = 0; i < initializers.length; i++) { value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg); } return useValue ? value : void 0; }; import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs"; import { CatchClassError, MigrateError, resolvePathToFile, toJSON } from "../../utils/index.js"; import { StorageNamespaces } from "../../../types/index.js"; let BaseStorage = (() => { let _classDecorators = [CatchClassError]; let _classDescriptor; let _classExtraInitializers = []; let _classThis; var BaseStorage = class { static { _classThis = this; } static { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0; __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers); BaseStorage = _classThis = _classDescriptor.value; if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); __runInitializers(_classThis, _classExtraInitializers); } _namespace; _state; _directory = "cache"; _fileName = ".migrate.storage.json"; constructor(_namespace = StorageNamespaces.Storage) { this._namespace = _namespace; this._state = this.readFullStateFromFile()[this._namespace] || {}; if (!existsSync(this.filePath())) { this._saveStateToFile(); } } deleteStateFile() { if (this.stateExistsInFile()) { rmSync(this.filePath(), { force: true }); } } readFullStateFromFile() { if (!this.stateExistsInFile()) { return {}; } const fileContent = readFileSync(this.filePath(), { encoding: "utf8", }); return JSON.parse(fileContent); } stateExistsInFile() { return existsSync(this.filePath()); } filePath() { return resolvePathToFile("cache", this._fileName); } _saveStateToFile() { const fileSate = this.readFullStateFromFile(); fileSate[this._namespace] = this._state; if (!existsSync(this._directory)) { mkdirSync(this._directory, { recursive: true }); } writeFileSync(this.filePath(), toJSON(fileSate), { flag: "w", encoding: "utf8", }); } }; return BaseStorage = _classThis; })(); let MigrateStorage = (() => { let _classDecorators = [CatchClassError]; let _classDescriptor; let _classExtraInitializers = []; let _classThis; let _classSuper = BaseStorage; var MigrateStorage = class extends _classSuper { static { _classThis = this; } static { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers); MigrateStorage = _classThis = _classDescriptor.value; if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); __runInitializers(_classThis, _classExtraInitializers); } get(key) { return this._state[key]; } getAll() { return this._state; } set(key, value, force = false) { if (!force && this.has(key)) { throw new MigrateError(`Key already exists`); } this._state[key] = value; this._saveStateToFile(); } delete(key, force = false) { if (!force && !this.has(key)) { throw new MigrateError(`Key not found`); } delete this._state[key]; this._saveStateToFile(); } has(key) { return this._state[key] !== undefined; } clear() { this._state = {}; this._saveStateToFile(); } }; return MigrateStorage = _classThis; })(); export { MigrateStorage }; export const DefaultStorage = new BaseStorage(); export const UserStorage = new MigrateStorage(StorageNamespaces.Storage); export const TransactionStorage = new MigrateStorage(StorageNamespaces.Transactions); export const ArtifactStorage = new MigrateStorage(StorageNamespaces.Artifacts); export const VerificationStorage = new MigrateStorage(StorageNamespaces.Verification); export function clearAllStorage() { UserStorage.clear(); TransactionStorage.clear(); ArtifactStorage.clear(); VerificationStorage.clear(); } //# sourceMappingURL=MigrateStorage.js.map