Skip to content

Commit

Permalink
Add --bail cli flag, fixes: Surnet#370
Browse files Browse the repository at this point in the history
  • Loading branch information
vidhill committed Jul 14, 2023
1 parent 55c614b commit f0946af
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions bin/swagger-jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ program
'Input swagger definition.'
)
.option('-o, --output [swaggerSpec.json]', 'Output swagger specification.')
.option('-b, --bail', 'Exit with failure status (1) if generation errors.')
.parse(process.argv);

if (!process.argv.slice(2).length) {
program.help();
process.exit();
}

const { definition } = program;
const { definition, bail: bailOnError } = program;
const output = program.output || 'swagger.json';

if (!definition) {
Expand Down Expand Up @@ -71,16 +72,30 @@ if (!program.args.length) {

const format = path.extname(output);

const result = swaggerJsdoc({
swaggerDefinition,
apis: program.args,
format,
});
function doGeneration(failOnErrors) {
const result = swaggerJsdoc({
failOnErrors,
swaggerDefinition,
apis: program.args,
format,
});

if (format === '.json') {
fs.writeFileSync(output, JSON.stringify(result, null, 2));
} else {
fs.writeFileSync(output, result);
if (format === '.json') {
fs.writeFileSync(output, JSON.stringify(result, null, 2));
} else {
fs.writeFileSync(output, result);
}

console.log('Swagger specification is ready.');
}

console.log('Swagger specification is ready.');
if (bailOnError === true) {
try {
doGeneration(bailOnError);
} catch (err) {
console.log('Swagger specification generation failed \n\n', err.message);
process.exit(1);
}
} else {
doGeneration(false);
}

0 comments on commit f0946af

Please sign in to comment.