@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
42 lines (35 loc) • 1.22 kB
JavaScript
// @ts-check
const cds = require('../cds')
const { isdir, fs, path } = cds.utils
const DEBUG = cds.debug('cli|build')
/** @typedef {typeof import('./plugins/plugin')} BuildPlugin */
module.exports = new (class Plugins {
/** @type {Map<string, BuildPlugin>} */
#plugins = new Map()
#registeredDefaults = false
get all() {
return this.#plugins
}
/**
* @param {string} id - unique ID of the plugin
* @param {BuildPlugin} plugin - build plugin implementation
*/
register(id, plugin) {
if (!id || !plugin) {
throw new Error(`Invalid parameter passed, build plugin id: ${id}`)
}
if (this.#plugins.has(id)) {
throw new Error(`CDS build plugin ${id} already registered`)
}
DEBUG?.(`registering build plugin ${id}`)
this.#plugins.set(id, plugin)
}
registerDefaults() {
if (this.#registeredDefaults) return
const plugins = fs.readdirSync(path.join(__dirname, 'plugins'))
for (const plugin of plugins) if (isdir(__dirname, 'plugins', plugin)) {
this.register(plugin, require(`./plugins/${plugin}`))
}
this.#registeredDefaults = true
}
})