Skip to content

Commit

Permalink
internal: always load main generators configs
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Oct 1, 2024
1 parent 46e2dd6 commit 1e375ed
Show file tree
Hide file tree
Showing 2 changed files with 17 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
20 changes: 8 additions & 12 deletions lib/command/lookup-commands-configs.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
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 index = await import(pathToFileURL(`${cwd}/${file}`).toString());
const configs: JHipsterConfigs = index.command?.configs ?? {};
for (const [key, value] of Object.entries(configs)) {
if (filter(value)) {
jdlConfigs[key] = value;
}
}
Object.assign(jhipsterConfigs, configs);
}
return jdlConfigs;
return Object.fromEntries(Object.entries(jhipsterConfigs).filter(([_key, value]) => filter(value)));
};

0 comments on commit 1e375ed

Please sign in to comment.