Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a generate-swagger command #567

Merged
merged 4 commits into from
Dec 5, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -13,12 +13,14 @@ const start = require('./start')
const eject = require('./eject')
const generate = require('./generate')
const generatePlugin = require('./generate-plugin')
const generateSwagger = require('./generate-swagger')
const generateReadme = require('./generate-readme')
const printRoutes = require('./print-routes')
commist.register('start', start.cli)
commist.register('eject', eject.cli)
commist.register('generate', generate.cli)
commist.register('generate-plugin', generatePlugin.cli)
commist.register('generate-swagger', generateSwagger.cli)
commist.register('readme', generateReadme.cli)
commist.register('help', help.toStdout)
commist.register('version', function () {
92 changes: 92 additions & 0 deletions generate-swagger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#! /usr/bin/env node

'use strict'

const parseArgs = require('./args')
const log = require('./log')
const {
exit,
requireFastifyForModule,
requireServerPluginFromPath,
showHelpForCommand
} = require('./util')
const fp = require('fastify-plugin')

let Fastify = null

function loadModules (opts) {
try {
Fastify = requireFastifyForModule(opts._[0]).module
} catch (e) {
module.exports.stop(e)
}
}

async function generateSwagger (args) {
const opts = parseArgs(args)
if (opts.help) {
return showHelpForCommand('generate-swagger')
}

if (opts._.length !== 1) {
console.error('Missing the required file parameter\n')
return showHelpForCommand('generate-swagger')
}

// we start crashing on unhandledRejection
require('make-promises-safe')

loadModules(opts)

const fastify = await runFastify(opts)
try {
if (fastify.swagger == null) {
log('error', '@fastify/swagger plugin not installed')
process.exit(1)
}

return JSON.stringify(fastify.swagger(), undefined, 2)
} finally {
fastify.close()
}
}

async function runFastify (opts) {
require('dotenv').config()

let file = null

try {
file = await requireServerPluginFromPath(opts._[0])
} catch (e) {
return module.exports.stop(e)
}

const fastify = Fastify(opts.options)

const pluginOptions = {}
if (opts.prefix) {
pluginOptions.prefix = opts.prefix
}

await fastify.register(fp(file), pluginOptions)
await fastify.ready()

return fastify
}

function stop (message) {
exit(message)
}

function cli (args) {
return generateSwagger(args).then(swagger => {
process.stdout.write(swagger + '\n')
})
}

module.exports = { cli, stop, generateSwagger }

if (require.main === module) {
cli(process.argv.slice(2))
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -48,6 +48,7 @@
"commist": "^3.0.0",
"dotenv": "^16.0.0",
"fastify": "^4.0.0",
"fastify-plugin": "^4.0.0",
"generify": "^4.0.0",
"help-me": "^4.0.1",
"is-docker": "^2.0.0",
@@ -67,7 +68,6 @@
"@types/tap": "^15.0.5",
"concurrently": "^7.0.0",
"del-cli": "^3.0.1",
"fastify-plugin": "^4.0.0",
"fastify-tsconfig": "^1.0.1",
"minimatch": "^5.1.0",
"proxyquire": "^2.1.3",
18 changes: 18 additions & 0 deletions test/generate-swagger.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const path = require('path')
const t = require('tap')
const { test } = t
const { generateSwagger } = require('../generate-swagger')

const swaggerplugindir = path.join(__dirname, 'swaggerplugindir')
const swaggerplugin = path.join(swaggerplugindir, 'plugin.js')

test('should generate swagger', async (t) => {
t.plan(1)

try {
const swagger = JSON.parse(await generateSwagger([swaggerplugin]))
t.equal(swagger.openapi, '3.0.3')
} catch (err) {
t.error(err)
}
})
14 changes: 14 additions & 0 deletions test/swaggerplugindir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "swaggerplugindir",
"version": "1.0.0",
"description": "test for generator-swagger",
"main": "plugin.js",
"scripts": {
"test": ""
},
"dependencies": {
"fastify-plugin": "^1.4.0"
},
"author": "",
"license": "ISC"
}
39 changes: 39 additions & 0 deletions test/swaggerplugindir/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

const fp = require('fastify-plugin')

module.exports = fp(function (fastify, opts, next) {
fastify.decorate('swagger', function () {
return {
openapi: '3.0.3',
info: {
version: '8.1.0',
title: '@fastify/swagger'
},
components: {
schemas: {}
},
paths: {
'/': {
get: {
responses: {
200: {
description: 'Default Response'
}
}
}
},
'/example/': {
get: {
responses: {
200: {
description: 'Default Response'
}
}
}
}
}
}
})
next()
})