-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.js
153 lines (143 loc) · 4.67 KB
/
config.js
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
const path = require('path')
const fs = require('fs')
const merge = require('lodash.merge')
const defaultConfig = {
baseNamespace: 'proto',
embed: false, // generate extra library with Manifest.json or embed into existing sources (no Manifest generation)
sourceDir: 'source',
skipCoreFiles: false, // do not generate the .core.* files
messageType: {
'*': {
// relative to baseNamespace (starting with .)
extend: '.core.BaseMessage',
include: [],
implement: []
}
},
service: {
'*': {
// relative to baseNamespace (starting with .)
extend: '.core.BaseService',
include: [],
implement: []
}
},
// list of external dependencies that need to be required (e.g. for extensions)
require: [],
// skip generation and including of selected external libraries (filename needed, e.g. google-protobuf.js or grpc-web-client.js)
skipDeps: [],
// if true: do not add the fallback loading of external dependencies to proto.core.BaseMessage' defer method
skipDepLoadingFallback: false,
// do not end lines of generated code with ';' if true
withoutSemi: false,
// class to use for repeated properties
repeatedClass: 'qx.data.Array',
// code to append to any repeatedClass reference to transform it into a plain array, if necessary (leave empty if no transform is necessary)
repeatedClassTransform: '.toArray()',
// static classes that provide property validation methods (like qx.util.Validate)
// these classes are registered in the proto.util.ValidationFactory
validatorClasses: [],
// Field option handlers (for example see: handlers/options/qx.js)
optionHandlers: [],
// activate grpc client debugging mode: [true] => all requests, [false] => no debugging, ['stream'] => only streaming requests, ['single'] => only non-streaming requests
debugApiCalls: false,
// do not validate property values while the object is beeing initialized
disableValidatorsInConstructor: false,
// override settings for generated Manifest.json
manifest: {},
whitelist: {},
// convert local date object to UTC by adding/subtracting the timezone offset before converting from/to string
useUTC: false,
// float decimal place accuracy
floatDigits: 0
}
class Config {
constructor () {
this._config = defaultConfig
this.loadConfigs()
}
/**
* Load configs from filesystem
*/
async loadConfigs () {
const localConfigFile = path.join(process.cwd(), 'protoc-gen-qx.config.js')
if (fs.existsSync(localConfigFile)) {
const localConfig = require(localConfigFile)
merge(this._config, localConfig)
}
// console.error(JSON.stringify(this._config, null, 2))
}
get (option, identifier, key) {
if (!this._config.hasOwnProperty(option)) {
return null
}
if (!identifier) {
return this._config[option]
}
let config = this._config[option]['*'] ? Object.assign({}, this._config[option]['*']) : {}
if (identifier && identifier !== '*') {
// match the regexes
Object.keys(this._config[option]).filter( key => {
if (key.startsWith('/')) {
var parts = key.substring(1).split('/')
var regexp = RegExp(parts[0], parts[1])
return regexp.test(identifier)
}
return false
}).forEach(id => {
config = Object.assign(config, this._config[option][id])
})
if (this._config[option].hasOwnProperty(identifier)) {
config = Object.assign(config, this._config[option][identifier])
}
}
if (!key) {
return config
}
if (config.hasOwnProperty(key)) {
return config[key]
}
return null
}
set (option, value) {
this._config[option] = value
}
getExtend (option, identifier) {
let extend = this.get(option, identifier, 'extend')
if (extend) {
if (extend.startsWith('.')) {
// relative
return this.get('baseNamespace') + extend
} else {
return extend
}
}
return null
}
getIncludes (option, identifier) {
return this.__getNamespacedList(option, identifier, 'include')
}
getImplements (option, identifier) {
return this.__getNamespacedList(option, identifier, 'implement')
}
__getNamespacedList (option, identifier, key) {
let entries = this.get(option, identifier, key)
// console.error(option, identifier, key, entries)
if (entries) {
if (!Array.isArray(entries)) {
entries = [entries]
}
entries.forEach((inc, index) => {
if (inc.startsWith('.')) {
// relative
entries[index] = this.get('baseNamespace') + inc
}
})
} else {
entries = []
}
return entries
}
}
const config = new Config()
module.exports = config