draig-car
Version:
Database REST API interactive generator CLI and REPL OpenAPI3 based JS generator with interactive ORM/ODM REPL
46 lines (43 loc) • 1.38 kB
JavaScript
const util = require('util')
const chalk = require('chalk')
const mustache = require('mustache')
const u = require('../util')
const c = require('./common')
module.exports.addPropertyAction = async function(args) {
let api = this.context.api
let argv = this.context.argv
if (!args || !args.length) {
console.log('Usage: ' + this.commands.addprop.help)
return u.exitOrContinue(this)
}
// check if schema exists
let schema = util.isArray(args) ? args[0] : args.split(' ')[0]
if (!schema in api.components.schemas) {
console.error(chalk`Schema {yellow ${schema}} not found`)
return u.exitOrContinue(this)
}
let sch = api.components.schemas[schema]
let schemaObj =
sch.type && sch.type === 'object'
? sch
: sch.allOf
? sch.allOf.find(o => o.type === 'object')
: undefined
if (!schemaObj) {
console.error(
chalk`Schema {yellow ${schema}} is unsupported (allowed: object, allOf)`
)
return u.exitOrContinue(this)
}
let view = await c.fillTemplate(this, 'parts', 'property')
if (null === view) return u.exitOrContinue(this)
// extract propname from view
let propname = Object.keys(view)[0]
// add property to existing schema (or allOf object)
schemaObj.properties[propname] = view[propname]
console.error(
chalk`Property {green ${propname}} added to schema {green ${schema}}`
)
u.saveYaml(argv.api, api)
u.exitOrContinue(this)
}