Skip to content

refactor: move out the loadTemplateConfig from generator.js #1584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
087054d
refactor: move out the loadTemplateConfig from Gigantic generator.js
ItsRoy69 May 29, 2025
b188524
Merge branch 'master' into fix-template
ItsRoy69 May 29, 2025
c08825f
fixed validator passing in test files
ItsRoy69 May 30, 2025
0060d24
fix path of template validator
ItsRoy69 May 30, 2025
870e622
fixed linting errors
ItsRoy69 May 30, 2025
e745610
Merge branch 'master' into fix-template
ItsRoy69 May 30, 2025
74ab002
fixed and tested all errors
ItsRoy69 May 30, 2025
cfffd89
Merge branch 'fix-template' of https://github.com/ItsRoy69/generator …
ItsRoy69 May 30, 2025
aac2d7e
Merge branch 'master' into fix-template
ItsRoy69 May 30, 2025
7c1f77f
Merge branch 'master' into fix-template
ItsRoy69 May 30, 2025
b21eb76
fixed the coderabbit issue
ItsRoy69 May 30, 2025
b62c242
Merge branch 'fix-template' of https://github.com/ItsRoy69/generator …
ItsRoy69 May 30, 2025
aa60016
fixed the coderabbit issue
ItsRoy69 May 30, 2025
5d29ed6
Merge branch 'master' into fix-template
ItsRoy69 Jun 2, 2025
35aeb51
updated mr
ItsRoy69 Jun 3, 2025
82678fc
Merge branch 'master' into fix-template
ItsRoy69 Jun 3, 2025
a5d2a12
Merge remote-tracking branch 'origin' into fix-template
ItsRoy69 Jun 3, 2025
0cd2567
fixed testing of calling properly, removing aasync/await
ItsRoy69 Jun 3, 2025
234359a
fixed testing of calling properly, removing aasync/await
ItsRoy69 Jun 4, 2025
d10fbab
Merge branch 'master' into fix-template
ItsRoy69 Jun 5, 2025
90899d8
refactoring this keyword
ItsRoy69 Jun 6, 2025
4f0cf60
updated files
ItsRoy69 Jun 6, 2025
6cc19c7
updated files
ItsRoy69 Jun 6, 2025
e0c584e
updated files
ItsRoy69 Jun 6, 2025
240bdde
updated files
ItsRoy69 Jun 6, 2025
fe42537
removed entirely the loader and templateconfig from generator.js
ItsRoy69 Jun 9, 2025
cc07481
reverting back
ItsRoy69 Jun 9, 2025
01b56df
Merge branch 'master' into fix-template
ItsRoy69 Jun 9, 2025
6e7077f
Merge branch 'master' into fix-template
derberg Jun 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/generator/lib/__mocks__/templateConfigValidator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const templateConfigValidator = jest.genMockFromModule('../templateConfigValidator');
const templateConfigValidator = jest.genMockFromModule('../templateConfig/validator');

module.exports = templateConfigValidator;
55 changes: 4 additions & 51 deletions apps/generator/lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const { isAsyncAPIDocument } = require('@asyncapi/parser/cjs/document');

const { configureReact, renderReact, saveRenderedReactContent } = require('./renderer/react');
const { configureNunjucks, renderNunjucks } = require('./renderer/nunjucks');
const { validateTemplateConfig } = require('./templateConfigValidator');
const { validateTemplateConfig } = require('./templateConfig/validator');
const { loadTemplateConfig } = require('./templateConfig/loader');
const { isGenerationConditionMet } = require('./conditionalGeneration');
const {
convertMapToObject,
Expand All @@ -36,7 +37,6 @@ const { definitions, flatten, shorthands } = require('@npmcli/config/lib/definit

const FILTERS_DIRNAME = 'filters';
const HOOKS_DIRNAME = 'hooks';
const CONFIG_FILENAME = 'package.json';
const PACKAGE_JSON_FILENAME = 'package.json';
const GIT_IGNORE_FILENAME = '{.gitignore}';
const NPM_IGNORE_FILENAME = '{.npmignore}';
Expand Down Expand Up @@ -1005,55 +1005,8 @@ class Generator {
* Loads the template configuration.
* @private
*/
async loadTemplateConfig() {
this.templateConfig = {};

// Try to load config from .ageneratorrc
try {
const rcConfigPath = path.resolve(this.templateDir, '.ageneratorrc');
const yaml = await readFile(rcConfigPath, { encoding: 'utf8' });
const yamlConfig = require('js-yaml').load(yaml);
this.templateConfig = yamlConfig || {};

await this.loadDefaultValues();
return;
} catch (rcError) {
// console.error('Could not load .ageneratorrc file:', rcError);
log.debug('Could not load .ageneratorrc file:', rcError);
// Continue to try package.json if .ageneratorrc fails
}

// Try to load config from package.json
try {
const configPath = path.resolve(this.templateDir, CONFIG_FILENAME);
const json = await readFile(configPath, { encoding: 'utf8' });
const generatorProp = JSON.parse(json).generator;
this.templateConfig = generatorProp || {};
} catch (packageError) {
// console.error('Could not load generator config from package.json:', packageError);
log.debug('Could not load generator config from package.json:', packageError);
}

await this.loadDefaultValues();
}

/**
* Loads default values of parameters from template config. If value was already set as parameter it will not be
* overriden.
* @private
*/
async loadDefaultValues() {
const parameters = this.templateConfig.parameters;
const defaultValues = Object.keys(parameters || {}).filter(key => parameters[key].default);

defaultValues.filter(dv => this.templateParams[dv] === undefined).forEach(dv =>
Object.defineProperty(this.templateParams, dv, {
enumerable: true,
get() {
return parameters[dv].default;
}
})
);
loadTemplateConfig() {
return loadTemplateConfig.call(this);
}

/**
Expand Down
63 changes: 63 additions & 0 deletions apps/generator/lib/templateConfig/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const path = require('path');
const log = require('loglevel');
const { readFile } = require('../utils');

const CONFIG_FILENAME = 'package.json';

/**
* Loads the template configuration.
* @private
*/
async function loadTemplateConfig() {
this.templateConfig = {};

// Try to load config from .ageneratorrc
try {
const rcConfigPath = path.resolve(this.templateDir, '.ageneratorrc');
const yaml = await readFile(rcConfigPath, { encoding: 'utf8' });
const yamlConfig = require('js-yaml').safeLoad(yaml);
this.templateConfig = yamlConfig || {};

await loadDefaultValues.call(this);
return;
} catch (rcError) {
log.debug('Could not load .ageneratorrc file:', rcError);
// Continue to try package.json if .ageneratorrc fails
}

// Try to load config from package.json
try {
const configPath = path.resolve(this.templateDir, CONFIG_FILENAME);
const json = await readFile(configPath, { encoding: 'utf8' });
const generatorProp = JSON.parse(json).generator;
this.templateConfig = generatorProp || {};
} catch (packageError) {
log.debug('Could not load generator config from package.json:', packageError);
}

await loadDefaultValues.call(this);
}

/**
* Loads default values of parameters from template config. If value was already set as parameter it will not be
* overriden.
* @private
*/
async function loadDefaultValues() {
const parameters = this.templateConfig.parameters;
const defaultValues = Object.keys(parameters || {}).filter(key => parameters[key].default);

defaultValues.filter(dv => this.templateParams[dv] === undefined).forEach(dv =>
Object.defineProperty(this.templateParams, dv, {
enumerable: true,
get() {
return parameters[dv].default;
}
})
);
}

module.exports = {
loadTemplateConfig,
loadDefaultValues
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const semver = require('semver');
const Ajv = require('ajv');
const { getGeneratorVersion } = require('./utils');
const { getGeneratorVersion } = require('../utils');
const levenshtein = require('levenshtein-edit-distance');
// eslint-disable-next-line no-unused-vars
const {AsyncAPIDocumentInterface, AsyncAPIDocument} = require('@asyncapi/parser');
const { usesNewAPI } = require('./parser');
const { usesNewAPI } = require('../parser');

const ajv = new Ajv({ allErrors: true });

Expand Down
11 changes: 6 additions & 5 deletions apps/generator/test/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ const Generator = require('../lib/generator');
const log = require('loglevel');
const unixify = require('unixify');
const dummyYAML = fs.readFileSync(path.resolve(__dirname, './docs/dummy.yml'), 'utf8');
const { loadDefaultValues } = require('../lib/templateConfig/loader');

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

jest.mock('../lib/utils');
jest.mock('../lib/filtersRegistry');
jest.mock('../lib/hooksRegistry');
jest.mock('../lib/templateConfigValidator');
jest.mock('../lib/templateConfig/validator');

describe('Generator', () => {
describe('constructor', () => {
Expand Down Expand Up @@ -131,7 +132,7 @@ describe('Generator', () => {
util = require('../lib/utils');
filtersRegistry = require('../lib/filtersRegistry');
hooksRegistry = require('../lib/hooksRegistry');
templateConfigValidator = require('../lib/templateConfigValidator');
templateConfigValidator = require('../lib/templateConfig/validator.js');
xfsMock = require('fs.extra');
const { AsyncAPIDocument } = require('@asyncapi/parser/cjs/models/v2/asyncapi');
asyncApiDocumentMock = new AsyncAPIDocument({ 'x-parser-api-version': 0 });
Expand Down Expand Up @@ -500,7 +501,7 @@ describe('Generator', () => {
}
};

await gen.loadDefaultValues();
await loadDefaultValues.call(gen);

expect(gen.templateParams).toStrictEqual({
test: true,
Expand All @@ -524,7 +525,7 @@ describe('Generator', () => {
}
};

await gen.loadDefaultValues();
await loadDefaultValues.call(gen);

expect(gen.templateParams).toStrictEqual({
test: true
Expand All @@ -550,7 +551,7 @@ describe('Generator', () => {
}
};

await gen.loadDefaultValues();
await loadDefaultValues.call(gen);

expect(gen.templateParams).toStrictEqual({
test: true
Expand Down
2 changes: 1 addition & 1 deletion apps/generator/test/templateConfigValidator.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable sonarjs/no-identical-functions */
/* eslint-disable sonarjs/no-duplicate-string */
const { validateTemplateConfig } = require('../lib/templateConfigValidator');
const { validateTemplateConfig } = require('../lib/templateConfig/validator');
const fs = require('fs');
const path = require('path');
const { parse } = require('../lib/parser');
Expand Down