@dedel.alex/adonis6-swagger
Version:
Swagger provider for AdonisJS 6
36 lines (35 loc) • 1.22 kB
JavaScript
import { writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import swaggerJSDoc from 'swagger-jsdoc';
import { BaseCommand } from '@adonisjs/core/ace';
import buildJsDocConfig from '../src/utils/build_js_doc_config.js';
export default class GenerateSwaggerFile extends BaseCommand {
/**
* The name of the command
*/
static commandName = 'swagger:generate';
/**
* The command description to show on the help
* screen
*/
static description = 'Generate swagger file';
/**
* Configuration options accepted by the command
*/
static options = {
loadApp: true,
startApp: true,
};
/**
* Execute command
*/
async run() {
const config = this.app.config;
const swaggerFileContent = swaggerJSDoc(buildJsDocConfig(config.get('swagger.options', {})));
if (!config.get('swagger.specFilePath')) {
throw new Error("Config option 'swagger.specFilePath' should be specified for using this command");
}
const filePath = join(this.app.appRoot.pathname, config.get('swagger.specFilePath'));
await writeFile(filePath, JSON.stringify(swaggerFileContent));
}
}