UNPKG

@veecode-platform/safira-cli

Version:

Generate a microservice project from your spec.

53 lines (52 loc) 2.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OpenapiUtils = void 0; const file_exception_1 = require("../exception/file-exception"); const file_system_utils_1 = require("../utils/file-system-utils"); const json_utils_1 = require("../utils/json-utils"); const yaml_utils_1 = require("../utils/yaml-utils"); const string_utils_1 = require("../utils/string-utils"); class OpenapiUtils { static loadSpecFile(filePath) { if (json_utils_1.JsonUtils.isJsonFile(filePath)) { return json_utils_1.JsonUtils.loadJsonFile(filePath); } if (yaml_utils_1.YamlUtils.isYamlFile(filePath)) { return yaml_utils_1.YamlUtils.loadYamlFile(filePath); } throw new file_exception_1.InvalidExtensionFileException("Invalid file extension"); } static writeSpecFile(filePath, newBody) { if (!this.validateFileExtension(filePath)) throw new file_exception_1.InvalidExtensionFileException("Invalid file extension"); const extension = string_utils_1.StringUtils.getFileExtension(filePath); file_system_utils_1.FileSystemUtils.writeFile(filePath, extension === "json" ? json_utils_1.JsonUtils.stringify(newBody, true) : yaml_utils_1.YamlUtils.stringify(newBody)); } static validateFileExtension(filePath) { return !json_utils_1.JsonUtils.isJsonFile(filePath) || !yaml_utils_1.YamlUtils.isYamlFile(filePath); } static async syncOpenApiSpec(openApiSpecPath, destinyFilePath) { if (openApiSpecPath && file_system_utils_1.FileSystemUtils.isFile(openApiSpecPath)) { const fileName = string_utils_1.StringUtils.getFullFileName(openApiSpecPath); const fileExtension = string_utils_1.StringUtils.getFileExtension(fileName); if (fileExtension === "json") { json_utils_1.JsonUtils.parseToYaml(openApiSpecPath, destinyFilePath); } else { file_system_utils_1.FileSystemUtils.copyFileSync(openApiSpecPath, destinyFilePath); } } } static prependPathsWithContext(openApiSpecPath, context, writeFilePath) { const json = this.loadSpecFile(openApiSpecPath); if (context && !context.startsWith("/")) context = `/${context}`; const paths = json.paths; for (const key of Object.keys(paths)) { paths[`${context}${key}`] = paths[key]; delete paths[key]; } this.writeSpecFile(writeFilePath || openApiSpecPath, json); } } exports.OpenapiUtils = OpenapiUtils;