typera-openapi
Advanced tools
Comparing version 0.5.0 to 0.6.0
@@ -31,2 +31,8 @@ #!/usr/bin/env node | ||
default: false, | ||
}) | ||
.option('check', { | ||
alias: 'c', | ||
description: 'Exit with an error if output files are not up-to-date (useful for CI)', | ||
type: 'boolean', | ||
default: false, | ||
}).argv; | ||
@@ -39,2 +45,3 @@ const outputFileName = (sourceFileName, ext) => sourceFileName.slice(0, -path.extname(sourceFileName).length) + ext; | ||
const results = _1.generate(sourceFiles, { strict: true }, { log }).map((result) => (Object.assign(Object.assign({}, result), { outputFileName: outputFileName(result.fileName, ext) }))); | ||
let success = true; | ||
for (const { outputFileName, paths } of results) { | ||
@@ -45,6 +52,26 @@ let content = args.format === 'ts' ? tsString(paths) : jsonString(paths); | ||
} | ||
console.log('Writing', outputFileName); | ||
fs.writeFileSync(outputFileName, content); | ||
if (args.check) { | ||
if (!checkOutput(outputFileName, content)) | ||
success = false; | ||
} | ||
else { | ||
writeOutput(outputFileName, content); | ||
} | ||
} | ||
if (!success) { | ||
process.exit(1); | ||
} | ||
}); | ||
const checkOutput = (fileName, content) => { | ||
const current = fs.readFileSync(fileName, 'utf-8'); | ||
if (current !== content) { | ||
console.log(`${fileName} is out of date`); | ||
return false; | ||
} | ||
return true; | ||
}; | ||
const writeOutput = (fileName, content) => { | ||
console.log('Writing', fileName); | ||
fs.writeFileSync(fileName, content); | ||
}; | ||
const tsString = (paths) => `\ | ||
@@ -51,0 +78,0 @@ import { OpenAPIV3 } from 'openapi-types' |
@@ -64,2 +64,3 @@ "use strict"; | ||
const summary = getRouteSummary(ctx, symbol); | ||
const tags = getRouteTags(ctx, symbol); | ||
const routeInput = getRouteInput(ctx, symbol); | ||
@@ -83,3 +84,3 @@ if (!routeInput) | ||
{ | ||
[method]: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (summary ? { summary } : undefined)), (description ? { description } : undefined)), (parameters.length > 0 ? { parameters } : undefined)), operationRequestBody(requestBody)), { responses }), | ||
[method]: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (summary ? { summary } : undefined)), (description ? { description } : undefined)), (tags && tags.length > 0 ? { tags } : undefined)), (parameters.length > 0 ? { parameters } : undefined)), operationRequestBody(requestBody)), { responses }), | ||
}, | ||
@@ -97,2 +98,9 @@ ]; | ||
.filter(utils_1.isDefined)[0]; | ||
const getRouteTags = (ctx, symbol) => symbol | ||
.getJsDocTags() | ||
.filter((tag) => tag.name === 'tags') | ||
.map((tag) => { var _a; return (_a = tag.text) === null || _a === void 0 ? void 0 : _a.split(','); }) | ||
.filter(utils_1.isDefined) | ||
.flat() | ||
.map((tag) => tag.trim()); | ||
const operationRequestBody = (contentSchema) => { | ||
@@ -99,0 +107,0 @@ if (!contentSchema) |
{ | ||
"name": "typera-openapi", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Generate OpenAPI spec from typera routes", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/akheron/typera-openapi", |
@@ -25,4 +25,4 @@ # typera-openapi - typera to OpenAPI generator | ||
* | ||
* @tags Tag1,Tag2 | ||
* @summary You can also set a short summary | ||
* | ||
* @response 200 Success response description. | ||
@@ -29,0 +29,0 @@ * @response 400 Another description for a response. This one |
30273
625