UNPKG

openapi-modifier

Version:

This package allows you to automate the process of modifying OpenAPI specifications by applying a set of predefined rules

90 lines (89 loc) 4.27 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const yargs_parser_1 = __importDefault(require("yargs-parser")); const factory_1 = require("../logger/factory"); const config_1 = require("../config"); const index_1 = require("../index"); const fs_1 = __importDefault(require("fs")); const DEFAULT_CONFIG_PATHS = ['openapi-modifier.config.js', 'openapi-modifier.config.ts', 'openapi-modifier.config.json', 'openapi-modifier.config.yaml', 'openapi-modifier.config.yml']; const cli = (params) => __awaiter(void 0, void 0, void 0, function* () { const logger = factory_1.LoggerFactory.createLogger({ name: 'openapi-modifier-cli', minLevel: factory_1.LoggerFactory.typeLevelMap["not-important-warning"], verbose: !!(params === null || params === void 0 ? void 0 : params.verbose), }); try { const configPath = params.config; let config = null; if (configPath) { logger.trace(`Trying find config file... ${configPath}`); config = yield (0, config_1.findConfigFile)(logger, configPath); } else { for (const defaultConfigPath of DEFAULT_CONFIG_PATHS) { if (config) { // the configuration file has already been found continue; } const absoluteDefaultConfigPath = (0, config_1.getAbsoluteConfigPath)(defaultConfigPath); if (fs_1.default.existsSync(absoluteDefaultConfigPath)) { logger.trace(`Trying find config file... ${absoluteDefaultConfigPath}`); config = yield (0, config_1.findConfigFile)(logger, defaultConfigPath); } } } if (!config) { throw new Error(`The configuration file was not found, set the path to it (via the --config parameter) or create one of the configuration files: ${DEFAULT_CONFIG_PATHS.join(', ')}`); } const inputPath = (params === null || params === void 0 ? void 0 : params.input) || null; if (!inputPath) { throw new Error('Required --input param!'); } const outputPath = (params === null || params === void 0 ? void 0 : params.output) || null; if (!outputPath) { throw new Error('Required --output param!'); } const loggerConfig = {}; const verbose = (params === null || params === void 0 ? void 0 : params.verbose) || null; if (verbose) { loggerConfig.verbose = true; } const minLevel = (params === null || params === void 0 ? void 0 : params.minLevel) || null; if (minLevel) { loggerConfig.minLevel = parseInt(minLevel, 10); } logger.trace('Merging config with cli params...'); const finalConfig = (0, config_1.mergeConfigs)(logger, config, { input: inputPath, output: outputPath, logger: loggerConfig }); logger.trace(`Final CLI config`, finalConfig); logger.trace('Trying run openapi modifier...'); yield (0, index_1.openapiModifier)(finalConfig, logger); } finally { logger.helpInfo(logger.getHelpInfo()); } }); const parsedArguments = (0, yargs_parser_1.default)(process.argv); cli(parsedArguments) .then(() => { process.exit(0); }) .catch((error) => { console.error(error); process.exit(1); });