draig-car
Version:
Database REST API interactive generator CLI and REPL OpenAPI3 based JS generator with interactive ORM/ODM REPL
38 lines (35 loc) • 1.05 kB
JavaScript
const chalk = require('chalk')
const u = require('../util')
const c = require('./common')
module.exports.editAction = async function editAction(args) {
let ctx = this.context
if (!args || !args.length) {
console.log('Usage: ' + this.commands.edit.help)
return u.exitOrContinue(this)
}
let [value, hashKey, path] = c.apiValue(ctx, args)
if (typeof value === 'undefined' || typeof value === 'object') {
console.error(
chalk`Sorry, can't edit non-existent or object entry at {green ${args}}`
)
return u.exitOrContinue(this)
}
let newval = await c.question(
this,
[],
chalk`Edit value for {green ${args}} `,
value
)
newval = isNaN(Number(newval)) ? newval : Number(newval)
console.log(
chalk`New value: {green ${newval}} (type {green ${typeof newval}})`
)
if(typeof newval === 'string') newval = newval.trim()
if(c.apiSet(ctx.api, path, newval))
u.saveYaml(ctx.argv.api, ctx.api)
else
console.error(
chalk`Edit failed to set {green ${newval}} for {green ${path.join(' ')}})`
)
u.exitOrContinue(this)
}