UNPKG

@matterlabs/hardhat-zksync-deploy

Version:
156 lines 6.73 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MorphTs = exports.MorphTsBuilder = void 0; const ts_morph_1 = require("ts-morph"); const fs = __importStar(require("fs")); class MorphTsBuilder { constructor(_filePath) { this._filePath = _filePath; const fileContent = fs.readFileSync(_filePath, 'utf8'); const project = new ts_morph_1.Project(); this._sourceFile = project.createSourceFile(_filePath, fileContent, { overwrite: true }); } intialStep(steps) { return new MorphTs(steps, this._sourceFile, this._filePath); } } exports.MorphTsBuilder = MorphTsBuilder; class MorphTs { constructor(_steps, _sourceFile, _filePath) { this._steps = _steps; this._sourceFile = _sourceFile; this._filePath = _filePath; this._currentStep = undefined; let initialValue; for (const _step of _steps) { if (isMorphBuilderInitialStepType(_step)) { initialValue = _sourceFile.getVariableDeclaration((v) => v .getType() .getText() .includes(_step.initialVariableType)); if (!initialValue || initialValue === undefined) { continue; } const intialStep = initialValue.getInitializer()?.asKindOrThrow(ts_morph_1.SyntaxKind.ObjectLiteralExpression); this._currentStep = intialStep; return; } else if (isMorphBuilderInitialStepModule(_step)) { initialValue = _sourceFile .getStatements() .find((ea) => ea.getText().startsWith(_step.initialModule)) ?.asKind(ts_morph_1.SyntaxKind.ExpressionStatement) ?.getFirstChildByKind(ts_morph_1.SyntaxKind.BinaryExpression) ?.getRight(); if (!initialValue || initialValue === undefined) { continue; } this._currentStep = initialValue; return; } else if (isMorphBuilderInitialStepVariable(_step)) { initialValue = _sourceFile.getVariableDeclaration(_step.initialVariable); if (!initialValue || initialValue === undefined) { continue; } const intialStep = initialValue.getInitializer()?.asKindOrThrow(ts_morph_1.SyntaxKind.ObjectLiteralExpression); this._currentStep = intialStep; return; } else { initialValue = _sourceFile .getExportAssignment(() => true) ?.getExpressionIfKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression); if (!initialValue || initialValue === undefined) { continue; } this._currentStep = initialValue; return; } } if (!this._currentStep || this._currentStep === undefined) { throw new Error(`Initial current step is not found`); } } nextStep(step) { const previousStep = this._currentStep; const presentStep = previousStep ?.asKindOrThrow(ts_morph_1.SyntaxKind.ObjectLiteralExpression) .getProperty(step.propertyName); if (!presentStep) { if (step.isRequired) { throw new Error(`Property ${step.propertyName} not found`); } previousStep ?.asKindOrThrow(ts_morph_1.SyntaxKind.ObjectLiteralExpression) .addPropertyAssignment({ name: step.propertyName, initializer: JSON.stringify({}, null, 2), }) .getParentIfKindOrThrow(ts_morph_1.SyntaxKind.ObjectLiteralExpression); } const newPresentStep = previousStep ?.asKindOrThrow(ts_morph_1.SyntaxKind.ObjectLiteralExpression) .getProperty(step.propertyName) ?.getFirstChildByKindOrThrow(ts_morph_1.SyntaxKind.ObjectLiteralExpression); if (!newPresentStep || newPresentStep === undefined) { throw new Error(`Property ${step.propertyName} not found`); } this._currentStep = newPresentStep; return this; } replaceStep(step) { this.nextStep({ propertyName: step.propertyName }); const previousStep = this._currentStep; const presentStep = previousStep ?.asKindOrThrow(ts_morph_1.SyntaxKind.ObjectLiteralExpression) .getParentIfKindOrThrow(ts_morph_1.SyntaxKind.PropertyAssignment) .setInitializer(JSON.stringify(step.replaceObject, null, 2)) .getParentIfKindOrThrow(ts_morph_1.SyntaxKind.ObjectLiteralExpression) .getProperty(step.propertyName) ?.getFirstChildByKindOrThrow(ts_morph_1.SyntaxKind.ObjectLiteralExpression); if (!presentStep || presentStep === undefined) { throw new Error(`Property ${step.propertyName} not found`); } this._currentStep = presentStep; return this; } save() { const updatedCode = this._sourceFile.getText(); fs.writeFileSync(this._filePath, updatedCode, 'utf8'); } } exports.MorphTs = MorphTs; function isMorphBuilderInitialStepType(object) { return 'initialVariableType' in object; } function isMorphBuilderInitialStepModule(object) { return 'initialModule' in object; } function isMorphBuilderInitialStepVariable(object) { return 'initialVariable' in object; } //# sourceMappingURL=morph-ts-builder.js.map