draig-car
Version:
Database REST API interactive generator CLI and REPL OpenAPI3 based JS generator with interactive ORM/ODM REPL
36 lines (33 loc) • 1.11 kB
JavaScript
const util = require('util')
const chalk = require('chalk')
const yaml = require('js-yaml')
const u = require('../util')
const c = require('./common')
module.exports.rmAction = async function(args) {
// working vars
let ctx = this.context
let argv = this.context.argv
if (!args) {
console.log('Usage: ' + this.commands.rm.help)
return u.exitOrContinue(this)
}
let [value, hashKey, path] = c.apiValue(ctx, args)
if (typeof value === 'undefined') {
console.log(chalk`Sorry, value of element {yellow ${args}} is undefined`)
return u.exitOrContinue(this)
}
u.printYaml(yaml.safeDump(value))
let confirmed = await c.question(this, 'yesno', 'Are you sure? ')
if (!confirmed || confirmed !== 'yes')
console.log('Aborted - object not removed')
else {
let item = path.pop()
let container = path.reduce((i, e) => i + "['" + e + "']", '')
if (util.isArray(eval('ctx.api' + container)))
eval('ctx.api' + container + '.splice(' + item + ', 1)')
else eval('delete ctx.api' + hashKey)
c.validSchemas(ctx.api, ctx.api.paths)
u.saveYaml(argv.api, ctx.api)
}
u.exitOrContinue(this)
}