-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathconfig.service.ts
More file actions
73 lines (57 loc) · 1.79 KB
/
config.service.ts
File metadata and controls
73 lines (57 loc) · 1.79 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
import {Inject, Injectable} from '@nestjs/common';
import * as path from 'path';
import {COMMANDER_PROGRAM, LOGGER} from '../constants';
import {set, get, has, merge} from 'lodash';
import * as fs from 'fs-extra';
import { Command } from 'commander';
@Injectable()
export class ConfigService {
public readonly 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 {
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})
}
}