Skip to content

Commit 4f0cf60

Browse files
committed
updated files
Signed-off-by: ItsRoy69 <[email protected]>
1 parent 90899d8 commit 4f0cf60

File tree

3 files changed

+57
-46
lines changed

3 files changed

+57
-46
lines changed

apps/generator/lib/generator.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const { isAsyncAPIDocument } = require('@asyncapi/parser/cjs/document');
1414
const { configureReact, renderReact, saveRenderedReactContent } = require('./renderer/react');
1515
const { configureNunjucks, renderNunjucks } = require('./renderer/nunjucks');
1616
const { validateTemplateConfig } = require('./templateConfig/validator');
17-
const { loadTemplateConfig } = require('./templateConfig/loader');
17+
const { loadTemplateConfig, loadDefaultValues } = require('./templateConfig/loader');
1818
const { isGenerationConditionMet } = require('./conditionalGeneration');
1919
const {
2020
convertMapToObject,
@@ -1005,10 +1005,19 @@ class Generator {
10051005
* Loads the template configuration.
10061006
* @private
10071007
*/
1008-
loadTemplateConfig() {
1009-
return loadTemplateConfig.call(this);
1008+
async loadTemplateConfig() {
1009+
this.templateConfig = await loadTemplateConfig(this.templateDir);
1010+
await this.loadDefaultValues();
10101011
}
10111012

1013+
/**
1014+
* Loads default values of parameters from template config using the external loader.
1015+
* @private
1016+
*/
1017+
async loadDefaultValues() {
1018+
loadDefaultValues(this.templateConfig, this.templateParams);
1019+
}
1020+
10121021
/**
10131022
* Launches all the hooks registered at a given hook point/name.
10141023
*

apps/generator/lib/templateConfig/loader.js

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,66 @@
11
const path = require('path');
2-
const log = require('loglevel');
32
const { readFile } = require('../utils');
3+
const log = require('loglevel');
44

55
const CONFIG_FILENAME = 'package.json';
66

77
/**
8-
* Loads the template configuration.
9-
* @private
8+
* Loads template configuration from either .ageneratorrc or package.json
9+
* @param {string} templateDir - Path to the template directory
10+
* @returns {Promise<Object>} Template configuration object
1011
*/
11-
async function loadTemplateConfig() {
12-
this.templateConfig = {};
12+
async function loadTemplateConfig(templateDir) {
13+
let templateConfig = {};
1314

14-
// Try to load config from .ageneratorrc
15-
try {
16-
const rcConfigPath = path.resolve(this.templateDir, '.ageneratorrc');
17-
const yaml = await readFile(rcConfigPath, { encoding: 'utf8' });
18-
const yamlConfig = require('js-yaml').load(yaml);
19-
this.templateConfig = yamlConfig || {};
15+
// Try to load config from .ageneratorrc
16+
try {
17+
const rcConfigPath = path.resolve(templateDir, '.ageneratorrc');
18+
const yaml = await readFile(rcConfigPath, { encoding: 'utf8' });
19+
const yamlConfig = require('js-yaml').load(yaml);
20+
templateConfig = yamlConfig || {};
2021

21-
await this.loadDefaultValues();
22-
return;
23-
} catch (rcError) {
24-
// console.error('Could not load .ageneratorrc file:', rcError);
25-
log.debug('Could not load .ageneratorrc file:', rcError);
26-
// Continue to try package.json if .ageneratorrc fails
27-
}
22+
log.debug('Template configuration loaded from .ageneratorrc');
23+
return templateConfig;
24+
} catch (rcError) {
25+
log.debug('Could not load .ageneratorrc file:', rcError);
26+
// Continue to try package.json if .ageneratorrc fails
27+
}
2828

29-
// Try to load config from package.json
30-
try {
31-
const configPath = path.resolve(this.templateDir, CONFIG_FILENAME);
32-
const json = await readFile(configPath, { encoding: 'utf8' });
33-
const generatorProp = JSON.parse(json).generator;
34-
this.templateConfig = generatorProp || {};
35-
} catch (packageError) {
36-
// console.error('Could not load generator config from package.json:', packageError);
37-
log.debug('Could not load generator config from package.json:', packageError);
38-
}
29+
// Try to load config from package.json
30+
try {
31+
const configPath = path.resolve(templateDir, CONFIG_FILENAME);
32+
const json = await readFile(configPath, { encoding: 'utf8' });
33+
const generatorProp = JSON.parse(json).generator;
34+
templateConfig = generatorProp || {};
3935

40-
await this.loadDefaultValues();
36+
log.debug('Template configuration loaded from package.json');
37+
} catch (packageError) {
38+
log.debug('Could not load generator config from package.json:', packageError);
4139
}
4240

43-
/**
44-
* Loads default values of parameters from template config. If value was already set as parameter it will not be
45-
* overriden.
46-
* @private
47-
*/
48-
async function loadDefaultValues() {
49-
const parameters = this.templateConfig.parameters;
50-
const defaultValues = Object.keys(parameters || {}).filter(key => parameters[key].default);
41+
return templateConfig;
42+
}
5143

52-
defaultValues.filter(dv => this.templateParams[dv] === undefined).forEach(dv =>
53-
Object.defineProperty(this.templateParams, dv, {
44+
/**
45+
* Loads default values of parameters from template config
46+
* @param {Object} templateConfig - The template configuration object
47+
* @param {Object} templateParams - The template parameters object to modify
48+
*/
49+
function loadDefaultValues(templateConfig, templateParams) {
50+
const parameters = templateConfig.parameters;
51+
const defaultValues = Object.keys(parameters || {}).filter(key => parameters[key].default);
52+
53+
defaultValues
54+
.filter(dv => templateParams[dv] === undefined)
55+
.forEach(dv => {
56+
Object.defineProperty(templateParams, dv, {
5457
enumerable: true,
5558
get() {
5659
return parameters[dv].default;
5760
}
58-
})
59-
);
60-
}
61+
});
62+
});
63+
}
6164

6265
module.exports = {
6366
loadTemplateConfig,

apps/generator/test/generator.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const Generator = require('../lib/generator');
55
const log = require('loglevel');
66
const unixify = require('unixify');
77
const dummyYAML = fs.readFileSync(path.resolve(__dirname, './docs/dummy.yml'), 'utf8');
8-
const { loadDefaultValues } = require('../lib/templateConfig/loader');
98

109
const logMessage = require('./../lib/logMessages.js');
1110

0 commit comments

Comments
 (0)