|
1 | 1 | const path = require('path');
|
2 |
| -const log = require('loglevel'); |
3 | 2 | const { readFile } = require('../utils');
|
| 3 | +const log = require('loglevel'); |
4 | 4 |
|
5 | 5 | const CONFIG_FILENAME = 'package.json';
|
6 | 6 |
|
7 | 7 | /**
|
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 |
10 | 11 | */
|
11 |
| -async function loadTemplateConfig() { |
12 |
| - this.templateConfig = {}; |
| 12 | +async function loadTemplateConfig(templateDir) { |
| 13 | + let templateConfig = {}; |
13 | 14 |
|
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 || {}; |
20 | 21 |
|
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 | + } |
28 | 28 |
|
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 || {}; |
39 | 35 |
|
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); |
41 | 39 | }
|
42 | 40 |
|
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 | +} |
51 | 43 |
|
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, { |
54 | 57 | enumerable: true,
|
55 | 58 | get() {
|
56 | 59 | return parameters[dv].default;
|
57 | 60 | }
|
58 |
| - }) |
59 |
| - ); |
60 |
| - } |
| 61 | + }); |
| 62 | + }); |
| 63 | +} |
61 | 64 |
|
62 | 65 | module.exports = {
|
63 | 66 | loadTemplateConfig,
|
|
0 commit comments