-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
config.service.ts
60 lines (47 loc) · 1.44 KB
/
config.service.ts
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
import {Inject, Injectable} from '@nestjs/common';
import * as path from 'path';
import {LOGGER} from '../constants';
import {set, get, has, merge} from 'lodash';
import * as fs from 'fs-extra';
@Injectable()
export class ConfigService {
public readonly cwd = process.env.PWD || process.env.INIT_CWD || process.cwd()
public readonly configFile = path.resolve(this.cwd, 'openapitools.json')
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,
) {
}
get<T = unknown>(path: string, defaultValue?: T): T {
return get(this.read(), path, defaultValue)
}
has(path) {
return has(this.read(), path)
}
set(path: string, value: unknown) {
this.write(set(this.read(), path, value))
return this
}
private read() {
fs.ensureFileSync(this.configFile)
return merge(
this.defaultConfig,
fs.readJSONSync(this.configFile, {throws: false, encoding: 'utf8'}),
)
}
private write(config) {
fs.writeJSONSync(this.configFile, config, {encoding: 'utf8', spaces: config.spaces || 2})
}
}