Last active
April 24, 2023 10:52
-
-
Save moshewe/99b54fdac1ab7c6b9f9b292324814beb to your computer and use it in GitHub Desktop.
This script generates the swagger spec for a given NestJS Module. It takes the app's path, version and module name as parameters, with optionals for
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as fs from 'fs'; | |
import { NestFactory } from '@nestjs/core'; | |
import { DocumentBuilder, SwaggerDocument, SwaggerModule } from '@nestjs/swagger'; | |
import * as commander from 'commander'; | |
const program = new commander.Command(); | |
program | |
.name('single-app-swagger-json-generator') | |
.arguments('--app-path <app-path> --app-version <app-version>') | |
.requiredOption('-a, --app-path <path>', 'path to app\'s folder') | |
.requiredOption('-v, --app-version <version>', 'app\'s version') | |
.option('-m, --module-name <module>', 'module name') | |
.option('-n, --app-name <app-name>', 'app\'s name') | |
.option('-o, --output-dir <output-dir>', 'output directory to write the swagger spec to'); | |
program.parse(process.argv); | |
const outputDir = program.outputDir || '.'; | |
const importPath = `${process.cwd()}/${program.appPath}`; | |
(async () => { | |
const importedObject = await import(importPath); | |
const appModule = | |
program.moduleName ? importedObject[program.moduleName] : | |
importedObject.default; | |
const appName = program.appName || appModule.name; | |
const swaggerBaseConfig = new DocumentBuilder() | |
.setSchemes('https') | |
.setTitle(`${appName} API`) | |
.setDescription(`Auto-generated for app ${appName}.`) | |
.addBearerAuth() | |
.setVersion(program.appVersion) | |
.build(); | |
const app = await NestFactory.create(appModule); | |
const document: SwaggerDocument = SwaggerModule.createDocument(app, swaggerBaseConfig); | |
fs.writeFileSync(`${outputDir}/${appName}.json`, JSON.stringify(document)); | |
})(); |
Is there a good way to integrate the swagger CLI plugin as an AST transformer with something like this?
Would like to know that too
Haven't touched upon this in a few years, feel free to experiment and update everyone here...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there a good way to integrate the swagger CLI plugin as an AST transformer with something like this?