UNPKG

trapi

Version:

The main package and contains bundled functionality of all other package and a CLI to interact with them.

108 lines 4.55 kB
"use strict"; /* * Copyright (c) 2021. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadConfig = exports.getConfig = void 0; const fs_1 = require("fs"); const path_1 = require("path"); const yamljs_1 = require("yamljs"); const utils_1 = require("@trapi/utils"); const swagger_1 = require("@trapi/swagger"); const validator_1 = require("./validator"); /** * Return parsed configuration file from given file location. * * @param workingDir * @param fileName */ function getConfig(workingDir, fileName = 'swagger.json') { return __awaiter(this, void 0, void 0, function* () { const data = yield loadConfig(workingDir, fileName); const config = yield (0, validator_1.parseConfig)(data); if (typeof config.decorator !== 'undefined') { (0, swagger_1.extendSwaggerConfig)(workingDir, config.swagger); } return config; }); } exports.getConfig = getConfig; /** * Load raw configuration file from given file location. * * @param workingDir * @param fileName */ function loadConfig(workingDir, fileName = 'swagger.json') { return __awaiter(this, void 0, void 0, function* () { const filePath = (0, path_1.join)(workingDir, fileName); const fileExtension = filePath.split('.').pop(); if (fileExtension === 'js') { try { const data = yield Promise.resolve().then(() => __importStar(require(filePath))); if ((0, utils_1.hasOwnProperty)(data, 'default')) { return data.default(); } return data; } catch (e) { throw new Error('Config file content could not be parsed...'); } } return new Promise((resolve, reject) => { return (0, fs_1.readFile)(filePath, { encoding: 'utf-8' }, (err, data) => { if (err) { return reject(err); } const content = data.toString('utf-8'); try { switch (fileExtension) { case 'yml': case 'yaml': return resolve((0, yamljs_1.parse)(content)); case 'json': return resolve(JSON.parse(content)); default: return reject(new Error('Invalid config file extension...')); } } catch (e) { return reject(new Error('Config file content could not be parsed...')); } }); }); }); } exports.loadConfig = loadConfig; //# sourceMappingURL=index.js.map