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
149 lines (118 loc) · 3.63 KB
/
config.service.ts
File metadata and controls
149 lines (118 loc) · 3.63 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
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 result = getPath(this.read(), path.split('.')) as T;
return result !== undefined ? result : 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: object,
source: object,
): object => {
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] || {});
result[key] = deepMerge(value, source[key]);
} else {
result[key] = source[key];
}
}
}
return result;
};
fs.ensureFileSync(this.configFile);
return deepMerge(
this.defaultConfig,
fs.readJSONSync(this.configFile, { throws: false, encoding: 'utf8' }),
);
}
private write(config) {
fs.writeJSONSync(this.configFile, config, {encoding: 'utf8', spaces: config.spaces || 2})
}
}