UNPKG

draig-car

Version:

Database REST API interactive generator CLI and REPL OpenAPI3 based JS generator with interactive ORM/ODM REPL

33 lines (30 loc) 1.07 kB
const util = require('util') const chalk = require('chalk') const mustache = require('mustache') const u = require('../util') const c = require('./common') module.exports.addArrayElemAction = async function(args) { let ctx = this.context let argv = ctx.argv if (!args || !args.length) { console.log('Usage: ' + this.commands.addelem.help) return u.exitOrContinue(this) } // get value and path of object to be modified let [value, hashKey, path] = c.apiValue(ctx, args) if (!util.isArray(value)) { console.error(chalk`Object at {yellow ${args}} not found or not an array`) return u.exitOrContinue(this) } let schemaPath, workSchema if(path.length > 1) { schemaPath = [].concat(path).slice(0,-1) workSchema = eval(`ctx.api.${schemaPath.join('.')}`) } let view = await c.fillTemplate(this, 'parts', path.pop(), workSchema) if (null === view) return u.exitOrContinue(this) // use substitution to modify current api definition eval('view.forEach(e => ctx.api' + hashKey + '.push(e))') u.saveYaml(argv.api, ctx.api) u.exitOrContinue(this) }