forked from OpenAPITools/openapi-generator-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.service.ts
More file actions
201 lines (162 loc) · 5.38 KB
/
config.service.ts
File metadata and controls
201 lines (162 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { Inject, Injectable } from '@nestjs/common';
import * as path from 'path';
import { COMMANDER_PROGRAM, LOGGER } from '../constants';
import * as fs from 'fs-extra';
import { Command } from 'commander';
@Injectable()
export class ConfigService {
public readonly cwd =
process.env.PWD || process.env.INIT_CWD || process.cwd();
public readonly configFile = this.configFileOrDefault();
private configFileOrDefault() {
this.program.parseOptions(process.argv);
const conf = this.program.opts().openapitools;
if (!conf) {
return path.resolve(this.cwd, 'openapitools.json');
}
return path.isAbsolute(conf) ? conf : path.resolve(this.cwd, conf);
}
public get useDocker() {
return this.get('generator-cli.useDocker', false);
}
public get dockerImageName() {
return this.get('generator-cli.dockerImageName', 'openapitools/openapi-generator-cli');
}
private readonly defaultConfig = {
$schema:
'./node_modules/@openapitools/openapi-generator-cli/config.schema.json',
spaces: 2,
'generator-cli': {
version: undefined,
},
};
constructor(
@Inject(LOGGER) private readonly logger: LOGGER,
@Inject(COMMANDER_PROGRAM) private readonly program: Command,
) {}
get<T = unknown>(path: string, defaultValue?: T): T {
const getPath = (
obj: Record<string, unknown> | unknown,
keys: string[],
): unknown => {
if (!obj || keys.length === 0) return obj;
const [head, ...tail] = keys;
if (tail.length === 0) {
return obj[head];
}
return getPath(obj[head], tail);
};
const raw = getPath(this.read(), path.split('.')) as Record<
string,
unknown
>;
const resolved = this.replacePlaceholders(raw) as T;
return resolved !== undefined ? resolved : defaultValue;
}
has(path: string) {
const hasPath = (
obj: Record<string, unknown> | unknown,
keys: string[],
): boolean => {
if (!obj || keys.length === 0) return false;
const [head, ...tail] = keys;
if (tail.length === 0) {
return Object.prototype.hasOwnProperty.call(obj, head);
}
return hasPath(obj[head] as Record<string, unknown>, tail);
};
return hasPath(this.read(), path.split('.'));
}
set(path: string, value: unknown) {
const setPath = (
obj: object,
keys: string[],
val: unknown,
): object => {
const [head, ...tail] = keys;
if (tail.length === 0) {
obj[head] = val;
return obj;
}
if (!obj[head] || typeof obj[head] !== 'object') {
obj[head] = {};
}
setPath(obj[head] as Record<string, unknown>, tail, val);
return obj;
};
const config = this.read();
this.write(setPath(config, path.split('.'), value));
return this;
}
private read() {
const deepMerge = (
target: Record<string, unknown>,
source: object,
): Record<string, unknown> => {
if (!source || typeof source !== 'object') return target;
const result = { ...target };
for (const key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
if (
source[key] &&
typeof source[key] === 'object' &&
!Array.isArray(source[key])
) {
const value = (result[key] || {}) as Record<string, unknown>;
result[key] = deepMerge(value, source[key]);
} else {
result[key] = source[key];
}
}
}
return result;
};
fs.ensureFileSync(this.configFile);
const fileConfig =
fs.readJSONSync(this.configFile, { throws: false, encoding: 'utf8' }) ??
{};
return deepMerge(this.defaultConfig, fileConfig);
}
private replacePlaceholders(config: Record<string, unknown>): Record<string, unknown> {
const replacePlaceholderInString = (inputString: string): string => {
return inputString.replace(/\${(.*?)}/g, (fullMatch, placeholderKey) => {
const environmentVariableKey = placeholderKey.startsWith('env.')
? placeholderKey.substring(4)
: placeholderKey;
const environmentVariableValue = process.env[environmentVariableKey];
if (environmentVariableValue === undefined) {
this.logger.error(
`Environment variable for placeholder '${environmentVariableKey}' not found.`,
);
return fullMatch;
}
return environmentVariableValue;
});
};
const traverseConfigurationObject = (
configurationValue: unknown,
): unknown => {
if (typeof configurationValue === 'string') {
return replacePlaceholderInString(configurationValue);
}
if (Array.isArray(configurationValue)) {
return configurationValue.map(traverseConfigurationObject);
}
if (configurationValue && typeof configurationValue === 'object') {
return Object.fromEntries(
Object.entries(configurationValue as Record<string, unknown>).map(
([propertyKey, propertyValue]) => [
propertyKey,
traverseConfigurationObject(propertyValue),
],
),
);
}
return configurationValue;
};
return traverseConfigurationObject(config) as Record<string, unknown>;
}
private write(config) {
fs.writeJSONSync(this.configFile, config, {encoding: 'utf8', spaces: config.spaces || 2})
}
}