Skip to content

Commit

Permalink
Merge pull request #27466 from mshima/configs
Browse files Browse the repository at this point in the history
internal: always load main generators configs
  • Loading branch information
DanielFran authored Oct 1, 2024
2 parents cf58c6e + 7aab2c3 commit 6350457
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
10 changes: 9 additions & 1 deletion generators/bootstrap-application-base/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand Down
21 changes: 9 additions & 12 deletions lib/command/lookup-commands-configs.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
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';

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<JHipsterConfigs> => {
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)));
};

0 comments on commit 6350457

Please sign in to comment.