boats
Version:
Beautiful Open / Async Template System - Write less yaml with BOATS and Nunjucks.
55 lines (54 loc) • 2.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const upath_1 = tslib_1.__importDefault(require("upath"));
const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const getOutputName_1 = tslib_1.__importDefault(require("./getOutputName"));
const validate_1 = tslib_1.__importDefault(require("./validate"));
const json_schema_ref_parser_1 = tslib_1.__importDefault(require("@apidevtools/json-schema-ref-parser"));
const generatePermissionsSchema_1 = tslib_1.__importDefault(require("./generatePermissionsSchema"));
const isOpenAPI_1 = tslib_1.__importDefault(require("./utils/isOpenAPI"));
const SortAttributes_1 = tslib_1.__importDefault(require("./SortAttributes"));
/**
* Bundles many files together and returns the final output path
*/
exports.default = async (input) => {
var _a;
const { excludeVersion, dereference, inputFile, outputFile, boatsRc } = input;
const doNotValidate = input.doNotValidate || false;
const indentation = input.indentation || 2;
let bundled;
try {
bundled = await json_schema_ref_parser_1.default.bundle(inputFile, boatsRc.jsonSchemaRefParserBundleOpts);
if (dereference) {
// @ts-ignore
bundled = await json_schema_ref_parser_1.default.dereference(bundled);
}
bundled = (0, generatePermissionsSchema_1.default)(bundled, (_a = boatsRc.permissionConfig) === null || _a === void 0 ? void 0 : _a.generateSchemaNamed);
const thisIsOpenAPI = (0, isOpenAPI_1.default)(bundled);
bundled = (thisIsOpenAPI) ? SortAttributes_1.default.forOpenAPI(bundled) : SortAttributes_1.default.forAsyncAPI(bundled);
if (doNotValidate) {
console.warn('Bypassing validation as dontValidateOutput flag seen');
}
else {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
thisIsOpenAPI ? await validate_1.default.openapi(bundled) : await validate_1.default.asyncapi(JSON.stringify(bundled));
}
let contents;
if (upath_1.default.extname(outputFile) === '.json') {
contents = JSON.stringify(bundled, null, indentation);
}
else {
contents = js_yaml_1.default.dump(bundled, { indent: indentation, lineWidth: 1000 });
}
fs_extra_1.default.ensureDirSync(upath_1.default.dirname(outputFile));
const pathToWriteTo = (0, getOutputName_1.default)(outputFile, bundled, excludeVersion);
fs_extra_1.default.writeFileSync(pathToWriteTo, contents);
return pathToWriteTo;
}
catch (e) {
console.error(JSON.stringify(bundled, undefined, 2));
throw e;
}
};