boats
Version:
Beautiful Open / Async Template System - Write less yaml with BOATS and Nunjucks.
104 lines (103 loc) • 5.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const upath_1 = tslib_1.__importDefault(require("upath"));
const bundlerSwaggerParse_1 = tslib_1.__importDefault(require("./bundlerSwaggerParse"));
const commander_1 = tslib_1.__importDefault(require("./commander"));
const convertToNunjucksOrYaml_1 = tslib_1.__importDefault(require("./convertToNunjucksOrYaml"));
const Template_1 = tslib_1.__importDefault(require("./Template"));
const npm_tool_version_check_1 = tslib_1.__importDefault(require("npm-tool-version-check"));
const package_json_1 = tslib_1.__importDefault(require("../package.json"));
const GetCheckCorrectBoatsRc_1 = tslib_1.__importDefault(require("./GetCheckCorrectBoatsRc"));
const Snippets_1 = tslib_1.__importDefault(require("./Snippets"));
const init_1 = require("./init");
const tmpFolderRemove_1 = tslib_1.__importDefault(require("./tmpFolderRemove"));
const constants_1 = require("./constants");
const dotenvFilePath = upath_1.default.join(process.cwd(), '.env');
// If a .env file exists call dotenv package to set into the env vars
if (fs_extra_1.default.pathExistsSync(dotenvFilePath)) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
require('dotenv').config({ path: dotenvFilePath });
}
// eslint-disable-next-line max-lines-per-function
const parseCli = async () => {
const program = (0, commander_1.default)(process.argv);
if (program.yes) {
process.env.npm_tool_version_check__quiet = 'true';
}
if (!(program.noVersionCheck || process.env.NODE_ENV === 'test')) {
await (0, npm_tool_version_check_1.default)(package_json_1.default.version, package_json_1.default.name, 'BOATS').catch(catchHandle);
}
if (program.init) {
return await (0, init_1.init)();
}
// Fetch the boatsrc here and not before the init, as the init may inject a specific file based on the type initialised
const boatsRc = GetCheckCorrectBoatsRc_1.default.getBoatsConfig();
if (program.convert_to_njk) {
// Convert files to njk
(0, convertToNunjucksOrYaml_1.default)(program.convert_to_njk, 'njk');
}
else if (program.convert_to_yml) {
// Convert files to yaml
(0, convertToNunjucksOrYaml_1.default)(program.convert_to_yml, 'yml');
}
else if (program.injectSnippet) {
// Snippets
new Snippets_1.default(program);
}
else {
// parse the directory then validate and bundle with swagger-parser
const returnFile = Template_1.default.directoryParse(program.input, program.output, program.indentation, program.strip_value, program.variables, program.functions, boatsRc, program.oneFileOutput);
const pathWrittenTo = await (0, bundlerSwaggerParse_1.default)({
inputFile: returnFile,
outputFile: program.output,
boatsRc,
dereference: program.dereference,
doNotValidate: program.dontValidateOutput,
excludeVersion: program.exclude_version,
indentation: program.indentation
});
// gracefully remove the tmp file that might have been created
(0, tmpFolderRemove_1.default)(program.input);
console.log(constants_1.LOG_GREEN, 'Completed, the files were rendered, validated and bundled to: ');
console.log(constants_1.LOG_GREEN_BOLD, pathWrittenTo);
}
};
const catchHandle = (error) => {
const line = '----------------------';
const printed = error.stack || error.details || error.name || undefined;
if (error.stack) {
console.error('');
console.error(constants_1.LOG_RED, line);
console.error(constants_1.LOG_RED_BOLD, 'ERROR.STACK: ');
console.error(constants_1.LOG_RED, line);
console.error(constants_1.LOG_RED, error.stack);
console.error('');
if (String(error.stack).includes('Cannot read property \'stack\' of undefined') ||
String(error.stack).includes('There were errors validating the AsyncAPI document')) {
console.log(constants_1.LOG_RED, 'TIP: An non-helpful error message from SwaggerParser or @asyncapi/parser is typically caused by invalid openapi or async api syntax not handled by them; it can often be invalid use of the combine keywords such as "allOf".');
}
console.error('');
console.error('');
}
if (error.details) {
console.error(constants_1.LOG_RED, line);
console.error(constants_1.LOG_RED_BOLD, 'ERROR.DETAILS: ');
console.error(line);
console.error(constants_1.LOG_RED, JSON.stringify(error.details, null, 2));
console.error('');
console.error('');
console.error('');
}
if (error.name) {
console.error(constants_1.LOG_RED_BOLD, 'ERROR.NAME: ');
console.log(constants_1.LOG_RED, JSON.stringify(error.name));
}
console.error(constants_1.LOG_RED, 'Tip: Scroll up in this terminal window for more errors and hints');
if (printed === undefined) {
console.trace(error);
}
process.exit(1);
};
parseCli().catch(catchHandle);