Skip to content

Commit bb7933e

Browse files
committed
feat(config): refactor config service to improve environment variable handling
1 parent 0f6913f commit bb7933e

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

apps/generator-cli/src/app/services/config.service.spec.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ describe('ConfigService', () => {
9898

9999
beforeEach(() => {
100100
originalEnv = { ...process.env };
101-
101+
102102
fs.readJSONSync.mockReturnValue({
103103
$schema: 'foo.json',
104104
spaces: 4,
@@ -214,11 +214,9 @@ describe('ConfigService', () => {
214214
});
215215
describe('--openapitools not set', () => {
216216
it('returns default path, if openapitools argument not provided', () => {
217-
expect(
218-
fixture.configFile.endsWith(
219-
'openapi-generator-cli/openapitools.json'
220-
)
221-
).toBeTruthy();
217+
expect(fixture.configFile).toEqual(
218+
expect.stringMatching(/[/\\]openapitools\.json$/),
219+
);
222220
});
223221
});
224222
});

apps/generator-cli/src/app/services/config.service.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { Command } from 'commander';
66

77
@Injectable()
88
export class ConfigService {
9-
10-
public readonly cwd = process.env.PWD || process.env.INIT_CWD || process.cwd();
9+
public readonly cwd =
10+
process.env.PWD || process.env.INIT_CWD || process.cwd();
1111
public readonly configFile = this.configFileOrDefault();
1212

1313
private configFileOrDefault() {
@@ -59,8 +59,14 @@ export class ConfigService {
5959
return getPath(obj[head], tail);
6060
};
6161

62-
const result = getPath(this.read(), path.split('.')) as T;
63-
return result !== undefined ? result : defaultValue;
62+
const raw = getPath(this.read(), path.split('.')) as Record<
63+
string,
64+
unknown
65+
>;
66+
67+
const resolved = this.replacePlaceholders(raw) as T;
68+
69+
return resolved !== undefined ? resolved : defaultValue;
6470
}
6571

6672
has(path: string) {
@@ -137,12 +143,11 @@ export class ConfigService {
137143

138144
fs.ensureFileSync(this.configFile);
139145

140-
const config = deepMerge(
141-
this.defaultConfig,
142-
fs.readJSONSync(this.configFile, { throws: false, encoding: 'utf8' }),
143-
);
146+
const fileConfig =
147+
fs.readJSONSync(this.configFile, { throws: false, encoding: 'utf8' }) ??
148+
{};
144149

145-
return this.replacePlaceholders(config);
150+
return deepMerge(this.defaultConfig, fileConfig);
146151
}
147152

148153
private replacePlaceholders(config: Record<string, unknown>): Record<string, unknown> {

0 commit comments

Comments
 (0)