UNPKG

genezio

Version:

Command line utility to interact with Genezio infrastructure.

86 lines (85 loc) 4.21 kB
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _GoDecoratorExtractor_instances, _GoDecoratorExtractor_compileGenezioGoParser; import path from "path"; import fs from "fs"; import { DecoratorExtractor } from "./baseDecoratorExtractor.js"; import { createTemporaryFolder } from "../file.js"; import { runNewProcess } from "../process.js"; import { UserError } from "../../errors.js"; import os from "os"; import { checkIfGoIsInstalled } from "../go.js"; import { $ } from "execa"; import { log } from "../logging.js"; const releaseTag = "v0.1.0"; const binaryName = `genezio_go_parser_${releaseTag}`; export class GoDecoratorExtractor extends DecoratorExtractor { constructor() { super(...arguments); _GoDecoratorExtractor_instances.add(this); } async getDecoratorsFromFile(file, cwd) { checkIfGoIsInstalled(); const goParserPath = path.join(os.homedir(), ".genezio", ".genezio_go_parser", binaryName); if (!fs.existsSync(goParserPath)) { await __classPrivateFieldGet(this, _GoDecoratorExtractor_instances, "m", _GoDecoratorExtractor_compileGenezioGoParser).call(this); } const classAbsolutePath = path.resolve(file); const result = await $({ cwd: cwd, }) `${goParserPath} ${classAbsolutePath}`.catch((error) => { log.error(error); throw new UserError("Error: Failed to parse Go file for class" + file); }); const classes = []; const response = JSON.parse(result.stdout); if (response.classes) { for (const c of response.classes) { const classInfo = DecoratorExtractor.createGenezioClassInfo(c.name, file, c.comment); classes.push(classInfo); } } if (response.methods) { for (const m of response.methods) { const classInfo = classes.find((c) => c.name === m.className); if (classInfo) { const methodInfo = DecoratorExtractor.createGenezioMethodInfo(m.name, m.comment); if (methodInfo) { classInfo.methods.push(methodInfo); } } } } return classes; } fileFilter(cwd) { return (file) => { const folderPath = path.join(cwd, file.path); return (file.extension === ".go" && !file.path.includes("node_modules") && !file.path.includes(".git") && !fs.lstatSync(folderPath).isDirectory()); }; } } _GoDecoratorExtractor_instances = new WeakSet(), _GoDecoratorExtractor_compileGenezioGoParser = async function _GoDecoratorExtractor_compileGenezioGoParser() { const folder = await createTemporaryFolder(); const parserClone = await runNewProcess("git clone --quiet https://github.com/Genez-io/go-parser.git .", folder); if (!parserClone) { throw new UserError("Error: Failed to clone Go parser repository to " + folder + " temporary folder!"); } await runNewProcess(`git checkout --quiet tags/${releaseTag}`, folder); const goBuildSuccess = await runNewProcess(`go build -o ${binaryName} cmd/main.go`, folder); if (!goBuildSuccess) { throw new UserError("Error: Failed to build Go parser in " + folder + " temporary folder!"); } if (!fs.existsSync(path.join(os.homedir(), ".genezio", ".genezio_go_parser"))) { fs.mkdirSync(path.join(os.homedir(), ".genezio", ".genezio_go_parser")); } const goParserPath = path.join(folder, binaryName); const goParserPathInHome = path.join(os.homedir(), ".genezio", ".genezio_go_parser", binaryName); fs.copyFileSync(goParserPath, goParserPathInHome); };