@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
156 lines (113 loc) • 4.13 kB
JavaScript
module.exports = Object.assign (env, {
options: ['--for', '--mocked'],
flags: ['--keys', '--properties', '--list', '--json', '--raw', '--process-env', '--resolve-bindings', '--sources'],
shortcuts: ['-4', 0, '-k', '-p', '-l', '-j', '-r', '-P', '-b'],
help: `
# SYNOPSIS
*cds env* [<key>] [<options>]
# EXPLANATION
Displays the effective configuration for the given key, or all of the
current environment.
# OPTIONS
*--sources*
Lists the sources from with the current env has been compiled.
*-k* | *--keys*
Prints (top-level) keys of matching properties only
*-p* | *--properties*
*-l* | *--list*
Prints output in .properties format
*-j* | *--json*
Prints output in JSON format
*-r* | *--raw*
Prints output with minimum formatting or decoration
*-4* | *--for* | *--profile* <profile,...>
Load configuration for the specified profile(s).
The *development* profile is used by default.
*-P* | *--process-env*
Show properties from Node.js _process.env_.
*-b* | *--resolve-bindings*
Resolve remote service bindings configured via *cds bind*.
`})
async function env ([cmd, key, value], options={}) {
if (options.for) process.env.CDS_ENV = options.for
const cds = require('../lib/cds')
const path = require('path')
const fn = (
options["process-env"] ? processEnvCommands[cmd] :
options.sources ? commands.sources :
commands[cmd] || ((key = cmd), commands.get)
)
let env = cds.env
// REVISIT: Currently only read in env.test.js
env['_home_cds-dk'] = path.resolve(__dirname, '..') // to help tools find the DK
if (!options.for && options.mocked || options["resolve-bindings"])
await cds.service.bindings
options.colors = cds.utils.colors.enabled === true
return fn (env, key, value, options)
}
const { inspect, format } = require('node:util')
const commands = {
json: (env,k,v,o) => commands.get (env,k,v,{...o,json:true}),
list: (env,k,v,o) => commands.get (env,k,v,{...o,list:true}),
ls: (env,k,v,o) => commands.get (env,k,v,{...o,list:true}),
sources: (..._) => commands.src (..._),
src (conf,k,v,o) {
if (!o.raw) console.log('\nCurrent cds.env was loaded from these sources:')
console.log()
for (let each of [...conf._sources].reverse()) console.log (' ', each)
console.log()
},
get (conf, key, val, options) {
let fmt = x => inspect (x, { depth:22, colors: true })
let obj = (
!key ? conf
: key === 'process.env' ? process.env
: key.startsWith('process.env') ? process.env[key.slice(12)]
: conf.get(key.replace(/^cds\./,''))
)
if (options.colors === false) {
fmt = x => typeof x !== 'object' ? x : JSON.stringify(x,0,2)
}
if (options.json) {
fmt = x => JSON.stringify(x,0,2)
}
if (options.raw) {
fmt = x => typeof x !== 'object' ? x : JSON.stringify(x)
}
if (options.list || options.properties) {
return console.log(), _list(obj,key,fmt), console.log()
}
if (options.keys) {
return console.log (`\n ${Object.keys(obj).map(k=>fmt(k)).join('\n ')}\n`)
}
console.log (fmt(obj))
},
set () {
console.error ('cds env set is not yet implemented, sorry.')
}
}
function _list (o, key='', fmt = format) {
if (o && typeof o === 'object' && !Array.isArray(o)) for (let p of Object.keys(o).sort()) {
const d = Reflect.getOwnPropertyDescriptor (o,p)
if (d.value === undefined && d.get === undefined) continue
if (o[p] !== undefined) _list (o[p], (key ? key+'.' : '')+p, fmt)
} else console.log (key,'=',fmt(o))
}
const processEnvCommands = {
get [undefined]() { return processEnvCommands.list },
get ''() { return processEnvCommands.list },
get ls() { return process.list },
list (_, key) {
if (key) {
processEnvCommands.get(undefined, key)
} else {
for (const name in process.env) {
console.log(`${name}=${process.env[name]}`)
}
}
},
get (_, key) {
console.log (process.env[key] || "")
}
}
/* eslint no-console:0 */