UNPKG

genezio

Version:

Command line utility to interact with Genezio infrastructure.

74 lines (73 loc) 3.35 kB
import { log } from "../utils/logging.js"; import path from "path"; import { newClassTemplateNode } from "../generateSdk/templates/newProject.js"; import { GenezioTelemetry, TelemetryEventTypes } from "../telemetry/telemetry.js"; import { fileExists, writeToFile } from "../utils/file.js"; import { supportedExtensions } from "../utils/languages.js"; import configIOController from "../projectConfiguration/yaml/v2.js"; import { scanClassesForDecorators } from "../utils/configuration.js"; import { UserError } from "../errors.js"; export async function addClassCommand(classPath, classType) { await GenezioTelemetry.sendEvent({ eventType: TelemetryEventTypes.GENEZIO_ADD_CLASS, }); if (classType === undefined) { classType = "jsonrpc"; } else if (!["http", "jsonrpc"].includes(classType)) { throw new UserError("Invalid class type. Valid class types are 'http' and 'jsonrpc'."); } if (classPath === undefined || classPath === "") { throw new UserError("Please provide a path to the class you want to add."); } const backendConfiguration = (await configIOController.read()).backend; if (!backendConfiguration) { throw new UserError("Please provide a valid backend configuration."); } backendConfiguration.classes = await scanClassesForDecorators(backendConfiguration); const className = classPath.split(path.sep).pop(); if (!className) { throw new UserError("Please provide a valid class path."); } const classExtension = className.split(".").pop(); if (!classExtension || className.split(".").length < 2) { throw new UserError("Please provide a class name with a valid class extension."); } // check if class is supported if (!supportedExtensions.includes(classExtension)) { const supportedExtensionsString = supportedExtensions.slice(0, -1).join(", ") + (supportedExtensions.length > 1 ? " and " : "") + supportedExtensions.slice(-1); throw new UserError(`Class language(${classExtension}) not supported. Currently supporting: ${supportedExtensionsString}`); } // check if class already exists if (backendConfiguration.classes.length > 0) { if (backendConfiguration.classes .map((c) => c.path.split(path.sep).pop()) .includes(className)) { throw new UserError("Class already exists."); } } let classContent = ""; if (["js", "ts"].includes(classExtension)) { let name = className.split(".")[0]; name = (name.charAt(0).toUpperCase() + name.slice(1)) .replaceAll("-", "") .replaceAll("_", "") .replaceAll(" ", "") .replaceAll(".", ""); classContent = newClassTemplateNode(name); } // create the file if it does not exist if (!(await fileExists(classPath))) { await writeToFile(".", path.join(backendConfiguration.path, classPath), classContent, true).catch(async (error) => { log.error(error.toString()); await GenezioTelemetry.sendEvent({ eventType: TelemetryEventTypes.GENEZIO_ADD_CLASS_ERROR, errorTrace: error.toString(), }); throw error; }); } log.info("\x1b[36m%s\x1b[0m", "Class added successfully."); }