@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
113 lines (80 loc) • 3.6 kB
JavaScript
const cds = require('../lib/cds'), { fs } = cds.utils
const DEBUG = cds.debug('add')
const { join } = require('path')
const fromDk = fs.readdirSync(join(__dirname, '../lib/init/template'))
const fromPlugins = require('../lib/init/add').registered
const conf = {}
for (const plugin of [...fromDk, ...Object.keys(Object.fromEntries(fromPlugins))]) {
const Plugin = fromPlugins.get(plugin) ?? require(join(__dirname, '../lib/init/template', plugin))
for (const [key, { type, help, short }] of Object.entries(new Plugin().options())) {
conf[plugin] ??= { options: [], flags: [], help: [] }
if (type === 'boolean') conf[plugin].flags.push({ key: '--'+key, short: short ? '-'+short : undefined, help })
else if (type === 'number') conf[plugin].options.push({ key: '--'+key, short: short ? '-'+short : undefined, help })
else if (type === 'string') conf[plugin].options.push({ key: '--'+key, short: short ? '-'+short : undefined, help })
}
}
const byShortcut = (a, b) => (b.short ? 1 : 0) - (a.short ? 1 : 0)
const options = ['--for', '--package', ...Object.values(conf).flatMap(p => p.options).sort(byShortcut).map(o => o.key), '--java:mvn']
const shortcuts = ['-4', '-p', ...Object.values(conf).flatMap(p => p.options).sort(byShortcut).map(o => o.short)]
const flags = ['--force', ...Object.values(conf).flatMap(p => p.flags).map(o => o.key)]
DEBUG?.('cli config ', { options, shortcuts, flags })
module.exports = Object.assign(add, {
handleCompletion,
options,
shortcuts,
flags,
help: `
# SYNOPSIS
*cds add* <feature | comma-separated list of features>
Add one or more features to an existing project - grow as you go.
Pick any of these:
${cds.cli.command !== 'completion' && require('../lib/init').help({ exclude: ['java', 'nodejs'] })}
# OPTIONS
*--for | -4* <profile>
Write configuration data for the given profile.
*--force*
Overwrite all files in case the target files already exist.
*--package | -p* <name>
Pull a package from your configured npm registry.
# FEATURE OPTIONS
${Object.entries(conf).sort(([a], [b]) => a.localeCompare(b)).flatMap(([pluginName, { options, flags }]) => {
const text4 = arr => arr.map(({ key, help, short }) => help && ` ${key}${short ? ' | '+short : ''}\n\n ${help}`).join('\n\n')
const optionsText = text4(options), flagsText = text4(flags)
if (!optionsText && !flagsText) return []
return ` *cds add ${pluginName}*${optionsText?`\n\n${optionsText}`:''}${flagsText?`\n\n${flagsText}`:''}`
}).join('\n\n\n')}
# EXAMPLES
*cds add* sample
*cds add* multitenancy,hana,xsuaa
*cds add* data --filter my.namespace.MyEntity
*cds add* mta
*cds add* helm
# SEE ALSO
*cds init*
# SUMMARY
*cds add* <feature | comma-separated list of features>
`});
async function handleCompletion(currentWord, previousWord, argv, util) {
const allOptionsFlags = [
...options ?? [],
...flags ?? []
].filter(e => !argv.includes(e));
if (currentWord?.startsWith('-')) {
return allOptionsFlags;
}
if (!currentWord && options?.includes(previousWord)) {
return [];
}
const entries = await util.completionFs.readTemplates(argv);
entries.push(...allOptionsFlags);
return entries;
}
async function add(args) {
const cds = require('../lib/cds')
if (!cds.cli.options.dry) console.log()
const CDSGenerator = require('../lib/init')
const generator = new CDSGenerator()
const features = args.map(f => f.split(/[,\s]/)).flat()
await generator.add(features)
if (!cds.cli.options.dry) console.log()
}