@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
94 lines (78 loc) • 2.95 kB
JavaScript
const { join } = require('path')
const cds = require('../cds')
const { readdir } = cds.utils
module.exports = class Plugin {
/**
* Provides command-line options for the plugin.
* @returns {Array.<{option: string, flag: string, shortcut?: string, help: string}>}
*/
options() {
return []
}
/**
* Gets the required templates.
* @returns {string[]} An array of strings representing required plugins.
* Returns an empty array by default.
* In general, avoid such dependencies if possible.
*/
requires() { return [] }
/**
* Determines whether a plugin can be run.
* @returns {Promise<boolean>} A promise that resolves to `true` if the template can be run.
*/
async canRun() { return true }
/**
* Gets a brief help text for the plugin.
* Displayed when all plugins are listed with `cds add`.
* @returns {string} The help text.
*/
static help() { return '' }
/**
* Determines if the plugin has already been added to the project.
* @param {import('@sap/cds').env} env - The env for the 'production' profile.
* @returns {boolean} - Returns `true` if the template has been added, otherwise `false`.
*/
static hasInProduction(env) { // eslint-disable-line no-unused-vars
return true
}
/**
* Runs the plugin.
*/
async run() { }
/**
* Runs the combine operations for interoperability with other plugins.
*/
async combine() { }
/* ––––––––––––
Yeoman Generator
–––––––––––– */
/**
* Runs the finalize operations.
*/
async finalize() { }
/* ––––––––––––
Private APIs
–––––––––––– */
async combineSupported() {
const DEBUG = cds.log('cli|add')._debug
DEBUG && console.log()
DEBUG && console.log('plugins active for production:')
const fromDk = await readdir(join(__dirname, './template'))
const { registered } = require('./add')
const { env4 } = require('./projectReader')
const processPlugins = async (plugins) => {
for (const plugin of plugins) {
const Plugin = registered.get(plugin) ?? require('./template/' + plugin)
const template = new Plugin()
const inProduction = Plugin.hasInProduction(env4('production'))
DEBUG && console.log(inProduction ? '✅' : '❌', plugin)
if (inProduction) await template.combine()
}
}
if (DEBUG) console.log(`\n ${fromDk.length} from @sap/cds-dk:`)
await processPlugins(fromDk)
if (DEBUG) console.log(`\n ${registered.size} from plugins:`)
await processPlugins(registered.keys())
DEBUG && console.log()
}
}