UNPKG

openapi-modifier

Version:

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

93 lines (92 loc) 4.33 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 config_1 = require("../config"); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const factory_1 = require("../logger/factory"); const DEFAULT_CONFIG_PATHS = [ 'simple-text-file-modifier.config.js', 'simple-text-file-modifier.config.ts', 'simple-text-file-modifier.config.json', 'simple-text-file-modifier.config.yaml', 'simple-text-file-modifier.config.yml', ]; const cli = (params) => __awaiter(void 0, void 0, void 0, function* () { const logger = factory_1.LoggerFactory.createLogger({ name: 'simple-text-file-modifier', 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!'); } let fileContent = fs_1.default.readFileSync(inputPath).toString(); if (config === null || config === void 0 ? void 0 : config.replace) { config.replace.forEach((item) => { fileContent = fileContent.replace(item.searchValue, item.replaceValue); }); } if (config === null || config === void 0 ? void 0 : config.addBefore) { fileContent = config.addBefore + fileContent; } if (config === null || config === void 0 ? void 0 : config.addAfter) { fileContent = fileContent + config.addAfter; } fs_1.default.mkdirSync(path_1.default.dirname(outputPath), { recursive: true }); fs_1.default.writeFileSync(outputPath, fileContent); logger.success('OK'); } 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); });