-
Notifications
You must be signed in to change notification settings - Fork 34
/
config.ts
158 lines (134 loc) · 3.42 KB
/
config.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
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
import { existsSync } from 'fs';
import { resolve } from 'path';
/*
Configuration specification and types.
All default values look like
module.exports = {
debug: false,
embedWellKnownTypes: false,
files: {
pb: {
generate: true,
},
pbconf: {
generate: true,
},
pbsc: {
generate: true,
serviceClientProvidedIn: 'any',
},
pbwsc: {
generate: false,
},
},
};
For the meaning of the parameters check below
*/
/**
* Configuration for `pb` files
*/
export class ConfigPb {
/**
* Enable or disable generation of pb files
*/
generate: boolean;
constructor(config: Partial<ConfigPb> = {}) {
this.generate = config.generate ?? true;
}
}
/**
* Configuration for `pbconf` files
*/
export class ConfigPbconf {
/**
* Enable or disable generation of pbconf files
*/
generate: boolean;
constructor(config: Partial<ConfigPbconf> = {}) {
this.generate = config.generate ?? true;
}
}
/**
* Configuration for `pbsc` files
*/
export class ConfigPbsc {
/**
* Enable or disable generation of pbsc files
*/
generate: boolean;
/**
* Provide service clients in...
*/
serviceClientProvidedIn: 'root' | 'any' | 'none';
constructor(config: Partial<ConfigPbsc> = {}) {
this.generate = config.generate ?? true;
this.serviceClientProvidedIn = config.serviceClientProvidedIn ?? 'any';
}
}
/**
* Configuration for `pbwsc` files.
* These files are required for worker client. By default, not generated.
*/
export class ConfigPbwsc {
/**
* Enable or disable generation of pbwsc files
*/
generate: boolean;
constructor(config: Partial<ConfigPbwsc> = {}) {
this.generate = config.generate ?? false;
}
}
/**
* Configuration object for all generated file types
*/
export class ConfigFiles {
pb: ConfigPb;
pbconf: ConfigPbconf;
pbsc: ConfigPbsc;
pbwsc: ConfigPbwsc;
constructor(config: Partial<ConfigFiles> = {}) {
this.pb = new ConfigPb(config.pb);
this.pbconf = new ConfigPbconf(config.pbconf);
this.pbsc = new ConfigPbsc(config.pbsc);
this.pbwsc = new ConfigPbwsc(config.pbwsc);
}
}
/**
* Generator configuration
*/
export class Config {
/**
* Enables debug mode, mostly for internal use
* By default false
*/
public debug: boolean;
/**
* Generate well-known types in your project instead of using `@ngx-grpc/well-known-types`
* Used internally to actually generate `@ngx-grpc/well-known-types` and as a legacy support of old versions of the lib.
* By default false
*/
public embedWellKnownTypes: boolean;
/**
* Custom well-known type package names
*/
public customWellKnownTypes: {[key: string]: string};
/**
* Per-generated-file-type config
*/
public files: ConfigFiles;
static fromParameter(parameter: string) {
const params = (parameter || '').split(',').map(p => p.split('=')).reduce((r, p) => ({ ...r, [p[0]]: p[1] }), {}) as {
config: string;
};
if (params.config && !existsSync(params.config)) {
throw new Error(`The config file "${params.config}" cannot be found`);
}
return new Config(params.config ? require(resolve(params.config)) : {});
}
constructor(config: Partial<Config> = {}) {
this.debug = config.debug ?? false;
this.embedWellKnownTypes = config.embedWellKnownTypes ?? false;
this.customWellKnownTypes = config.customWellKnownTypes ?? {};
this.files = new ConfigFiles(config.files ?? {});
}
}