diff --git a/generators/bootstrap-application-base/generator.ts b/generators/bootstrap-application-base/generator.ts index e30643d2c50c..59d9baf17dc7 100644 --- a/generators/bootstrap-application-base/generator.ts +++ b/generators/bootstrap-application-base/generator.ts @@ -441,9 +441,17 @@ export default class BootstrapApplicationBase extends BaseApplicationGenerator { * Avoid having undefined keys in the application object when redering ejs templates */ async loadApplicationKeys({ application }) { + if (this.options.commandsConfigs) { + // Load keys passed from cli + loadCommandConfigsKeysIntoTemplatesContext({ + templatesContext: application, + commandsConfigs: this.options.commandsConfigs, + }); + } + // Load keys from main generators loadCommandConfigsKeysIntoTemplatesContext({ templatesContext: application, - commandsConfigs: this.options.commandsConfigs ?? (await lookupCommandsConfigs()), + commandsConfigs: await lookupCommandsConfigs(), }); }, task({ application }) { diff --git a/lib/command/lookup-commands-configs.ts b/lib/command/lookup-commands-configs.ts index 43af04bf135e..6c0b8901ec14 100644 --- a/lib/command/lookup-commands-configs.ts +++ b/lib/command/lookup-commands-configs.ts @@ -1,5 +1,5 @@ import { dirname, join } from 'path'; -import { fileURLToPath } from 'url'; +import { fileURLToPath, pathToFileURL } from 'url'; import { glob } from 'glob'; import type { JHipsterConfig, JHipsterConfigs } from './types.js'; @@ -7,23 +7,20 @@ const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const cwd = join(__dirname, '../..'); -let jdlConfigs: JHipsterConfigs; +let jhipsterConfigs: JHipsterConfigs; export const lookupCommandsConfigs = async (options?: { filter: (config: JHipsterConfig) => boolean }): Promise => { - if (jdlConfigs) { - return jdlConfigs; + if (jhipsterConfigs) { + return jhipsterConfigs; } const { filter = () => true } = options ?? {}; - jdlConfigs = {}; + jhipsterConfigs = {}; const files = [...(await glob('generators/*/index.{j,t}s', { cwd })), ...(await glob('generators/*/generators/*/index.{j,t}s', { cwd }))]; for (const file of files) { - const index = await import(`${cwd}/${file}`); - const configs: JHipsterConfigs = index.command?.configs ?? {}; - for (const [key, value] of Object.entries(configs)) { - if (filter(value)) { - jdlConfigs[key] = value; - } + const index = await import(pathToFileURL(`${cwd}/${file}`).toString()); + if (index.command?.configs) { + Object.assign(jhipsterConfigs, index.command?.configs); } } - return jdlConfigs; + return Object.fromEntries(Object.entries(jhipsterConfigs).filter(([_key, value]) => filter(value))); };