UNPKG

gauge-ts

Version:
82 lines (81 loc) 3.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StubImplementationCodeProcessor = void 0; const node_os_1 = require("node:os"); const node_path_1 = require("node:path"); const typescript_1 = require("typescript"); const messages_pb_1 = require("../gen/messages_pb"); const spec_pb_1 = require("../gen/spec_pb"); const Util_1 = require("../utils/Util"); class StubImplementationCodeProcessor { process(req) { const filePath = req.getImplementationfilepath(); const content = req.getCodesList().reduce((acc, cur) => { return acc + node_os_1.EOL + cur; }); const fileDiff = new messages_pb_1.FileDiff(); fileDiff.setFilepath(filePath); const textDiffs = new Array(); if (!Util_1.Util.exists(filePath)) { const filePath = Util_1.Util.getNewTSFileName(Util_1.Util.getImplDirs()[0]); const className = (0, node_path_1.basename)(filePath).replace(typescript_1.Extension.Ts, ""); textDiffs.push(this.diffForImplementationInNewClass(content, className)); } else { textDiffs.push(this.diffForImplementationInExistingClass(filePath, content)); } fileDiff.setTextdiffsList(textDiffs); return fileDiff; } diffForImplementationInExistingClass(filePath, content) { const fileContent = Util_1.Util.readFile(filePath) .toString() .replace("\r\n", "\n"); const source = (0, typescript_1.createSourceFile)(filePath, fileContent, typescript_1.ScriptTarget.Latest); let lastMethod = null; (0, typescript_1.forEachChild)(source, (childNode) => { if ((0, typescript_1.isClassDeclaration)(childNode)) { (0, typescript_1.forEachChild)(childNode, (node) => { if ((0, typescript_1.isMethodDeclaration)(node)) { lastMethod = node; } }); } }); const pos = source.getLineAndCharacterOfPosition(lastMethod.end); const span = new spec_pb_1.Span(); span.setStart(pos.line + 1); span.setEnd(pos.line + 1); span.setStartchar(0); span.setEndchar(0); const textDiff = new messages_pb_1.TextDiff(); textDiff.setSpan(span); textDiff.setContent(content .split(node_os_1.EOL) .map((c) => { return `\t${c}`; }) .join(node_os_1.EOL) + node_os_1.EOL); return textDiff; } diffForImplementationInNewClass(content, className) { const span = new spec_pb_1.Span(); span.setStart(0); span.setEnd(0); span.setStartchar(0); span.setEndchar(0); const textDiff = new messages_pb_1.TextDiff(); textDiff.setSpan(span); textDiff.setContent(this.getContentForNewClass(content, className)); return textDiff; } getContentForNewClass(content, className) { return `import { Step } from "gauge-ts";${node_os_1.EOL}export default class ${className} {${node_os_1.EOL}${content .split(node_os_1.EOL) .map((c) => { return `\t${c}`; }) .join(node_os_1.EOL)}${node_os_1.EOL}}`; } } exports.StubImplementationCodeProcessor = StubImplementationCodeProcessor;