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
Show file tree
Hide file tree
Changes from all 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
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Fastify command line interface, available commands are:
* eject turns your application into a standalone executable with a server.(js|ts) file being added
* generate generate a new project
* generate-plugin generate a new plugin project
* generate-swagger generate Swagger/OpenAPI schema for a project using @fastify/swagger
* readme generate a README.md for the plugin
* print-routes prints the representation of the internal radix tree used by the router, useful for debugging.
* version the current fastify-cli version
Expand Down Expand Up @@ -316,6 +317,12 @@ Finally, there will be a new `README.md` file, which provides internal informati
* Encapsulation semantics
* Compatible Fastify version

### generate-swagger

if your project uses `@fastify/swagger`, `fastify-cli` can generate and write out the resulting Swagger/OpenAPI schema for you.

`fastify generate-swagger app.js`

### linting

`fastify-cli` is unopinionated on the choice of linter. We recommend you to add a linter, like so:
Expand Down Expand Up @@ -370,8 +377,8 @@ test('test my application', async t => {
})
```

Log output is consumed by tap. If log messages should be logged to the console
the logger needs to be configured to output to stderr instead of stdout.
Log output is consumed by tap. If log messages should be logged to the console
the logger needs to be configured to output to stderr instead of stdout.

```js
const logger = {
Expand Down
2 changes: 2 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
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))
}
3 changes: 3 additions & 0 deletions help/generate-swagger.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Usage: fastify generate-plugin [opts] <file> [--] [<plugin-options>]

Generate Swagger/OpenAPI schema for a project using @fastify/cli.
1 change: 1 addition & 0 deletions help/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Fastify command line interface available commands are:
* eject-ts turns your application into a standalone executable with a server.ts file being added.
* generate generate a new project
* generate-plugin generate a new plugin project
* generate-swagger generate Swagger/OpenAPI schema for a project using @fastify/swagger
* readme generate a README.md for the plugin
* print-routes prints the representation of the internal radix tree used by the router, useful for debugging.
* version the current fastify-cli version
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
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()
})