draig-car
Version:
Database REST API interactive generator CLI and REPL OpenAPI3 based JS generator with interactive ORM/ODM REPL
34 lines (31 loc) • 1.06 kB
JavaScript
const util = require('util')
const chalk = require('chalk')
const u = require('../util')
const c = require('./common')
module.exports.newOperationAction = async function(args) {
let api = this.context.api
let argv = this.context.argv
if (!args || args.length < 1) {
console.log('Usage: ' + this.commands.newop.help)
return u.exitOrContinue(this)
}
let name = util.isArray(args) ? args[0] : args.split(' ')[0]
let view = await c.fillTemplate(this, 'op', name)
if (view === null) return u.exitOrContinue(this)
let path = Object.keys(view)[0]
let op = Object.keys(view[path])[0]
// validate operationId collision and references to own schemas
if (
!c.validOperationId(api.paths, view[path][op].operationId) ||
!c.validSchemas(api, view)
)
return u.exitOrContinue(this)
// add op to new or existing path (first key of the view)
api.paths = c.addView(api.paths, view)
console.log(
chalk`Operation {green ${op}} added to {green ${path}}` +
chalk` using template {green ${name}}`
)
u.saveYaml(argv.api, api)
u.exitOrContinue(this)
}