-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·250 lines (224 loc) · 8.17 KB
/
index.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#! /usr/bin/env node
/* eslint-env es6, node */
const webpack = require('webpack')
const MemoryFileSystem = require('memfs')
const fs = require('fs')
const path = require('path')
const mkdirp = require("mkdirp")
const config = require('./config')
const {CodeGeneratorRequest, CodeGeneratorResponse, CodeGeneratorResponseError} = require('protoc-plugin')
const baseNamespace = config.get('baseNamespace')
const handlebars = require('handlebars')
const merge = require('lodash.merge')
const handlers = {
serviceList: require('./handlers/service'),
enumTypeList: require('./handlers/enum'),
messageTypeList: require('./handlers/message')
}
const lineEnd = config.get('withoutSemi') ? '' : ';'
const webpackConfig = require('./webpack.config')
const externalResources = []
webpackConfig.forEach(config => {
externalResources.push(`"${baseNamespace}/${config.output.filename}"`)
})
let template = handlebars.compile(fs.readFileSync(path.join(__dirname, 'templates', 'core', 'BaseService.js.hbs'), 'utf8'))
const debugApiCalls = config.get('debugApiCalls')
let baseServiceClass = template({
baseNamespace: baseNamespace,
lineEnd: lineEnd,
debugUnary: debugApiCalls === true || debugApiCalls === 'single',
debugInvoke: debugApiCalls === true || debugApiCalls === 'stream'
})
// extensions must be required before parsing
config.get('require').forEach(dep => {
const depPath = path.normalize(path.join(process.cwd() + '/' + dep))
require(depPath)
})
let sourceDir = config.get('sourceDir')
if (sourceDir && !sourceDir.endsWith('/')) {
sourceDir += '/'
}
// register option handlers
const optionHandler = require('./handlers/options/index')
let {name, handler} = require('./handlers/options/qx')
const { getSystemErrorMap } = require('util')
optionHandler.registerHandler(name, handler)
config.get('optionHandlers').forEach(handlerPath => {
let {name, handler} = require(path.normalize(path.join(process.cwd() + '/' + handlerPath)))
optionHandler.registerHandler(name, handler)
})
CodeGeneratorRequest()
.then(async r => {
const req = r.toObject()
// parse parameters
let paramParts, key, value
const parameters = {}
if (req.parameter) {
req.parameter.split(',').forEach(param => {
paramParts = param.split('=')
key = paramParts.shift()
value = paramParts.length > 0 ? paramParts.join('=') : true
parameters[key] = value
})
}
const protos = req.protoFileList.filter(p => req.fileToGenerateList.indexOf(p.name) !== -1)
const skipDeps = config.get('skipDeps')
let external = [
"proto/google-protobuf.js",
"proto/grpc-web-client.js"
].filter(entry => !skipDeps.includes(entry.split('/').pop()))
const files = []
if (!config.get('skipCoreFiles')) {
files.push({
name: `${sourceDir}/class/${baseNamespace}/core/BaseService.js`,
content: baseServiceClass
})
}
if (config.get('embed') !== true) {
const manifest = merge({
info: {
version: "latest"
},
provides: {
namespace: baseNamespace,
encoding: "utf-8",
class: sourceDir + "class",
resource: sourceDir + "resource",
type: "library"
}
}, config.get('manifest'))
if (external.length > 0) {
if (!manifest.externalResources) {
manifest.externalResources = {}
}
if (!manifest.externalResources.script) {
manifest.externalResources.script = []
}
manifest.externalResources.script = manifest.externalResources.script.concat(external)
}
files.push({
name: 'Manifest.json',
content: JSON.stringify(manifest, null, 2)
})
}
const whitelist = config.get('whitelist')
protos.forEach(proto => {
if (parameters.dump === true) {
files.push({
name: `${proto.pb_package}-${proto.name}-extended.json`,
content: JSON.stringify(proto, null, 2)
})
}
Object.keys(handlers).forEach( propName => {
if (proto[propName]) {
let items = proto[propName]
if (whitelist.hasOwnProperty(proto.name)) {
items = items.filter(item => {
return whitelist[proto.name].indexOf(item.name) >= 0
})
}
items.forEach((item, s) => {
handlers[propName](item, s, proto).forEach(entry => {
files.push({
name: `${sourceDir}/class/${entry.namespace.split('.').join('/')}.js`,
content: entry.code
})
})
})
}
})
})
if (!config.get('skipCoreFiles')) {
template = handlebars.compile(fs.readFileSync(path.join(__dirname, 'templates', 'core', 'BaseMessage.js.hbs'), 'utf8'))
let baseMessageClass = template({
baseNamespace: baseNamespace,
lineEnd: lineEnd,
timestampSupport: !!config.get('timestampSupport'),
useUTC: config.get('useUTC') === true,
defer: config.get('skipDepLoadingFallback') === true
? ''
: `,
defer: function (statics) {
if (!window.grpc) {
// load dependencies
var dynLoader = new qx.util.DynamicScriptLoader([
qx.util.ResourceManager.getInstance().toUri(${externalResources.join('),\n qx.util.ResourceManager.getInstance().toUri(')})
]);
qx.bom.Lifecycle.onReady(function () {
dynLoader.start().catch(function (err) {
qx.log.Logger.error(statics, 'failed to load scripts', err);
});
}, this);
}
}`
})
files.push({
name: `${sourceDir}/class/${baseNamespace}/core/BaseMessage.js`,
content: baseMessageClass
})
template = handlebars.compile(fs.readFileSync(path.join(__dirname, 'templates', 'core', 'Error.js.hbs'), 'utf8'))
let errorClass = template({
lineEnd: lineEnd
})
files.push({
name: `${sourceDir}/class/${baseNamespace}/core/Error.js`,
content: errorClass
})
if (config.get('validatorClasses').length) {
template = handlebars.compile(fs.readFileSync(path.join(__dirname, 'templates', 'core', 'ValidatorFactory.js.hbs'), 'utf8'))
let validatorFactoryClass = template({
baseNamespace: baseNamespace,
validatorClasses: config.get('validatorClasses').filter(clazz => clazz !== 'qx.util.Validate'),
lineEnd: lineEnd,
})
files.push({
name: `${sourceDir}/class/${baseNamespace}/util/ValidatorFactory.js`,
content: validatorFactoryClass
})
}
}
if (!config.get('skipCoreFiles')) {
template = handlebars.compile(fs.readFileSync(path.join(__dirname, 'templates', 'core', 'BaseError.js.hbs'), 'utf8'))
let baseErrorClass = template({
baseNamespace: baseNamespace,
lineEnd: lineEnd,
})
files.push({
name: `${sourceDir}/class/${baseNamespace}/core/BaseError.js`,
content: baseErrorClass
})
}
if (parameters.skipDeps !== true) {
// create resources
let compiler
const promises = []
webpackConfig.forEach(async config => {
if (skipDeps.includes(config.output.filename)) {
return;
}
config.output.path = '/build'
compiler = webpack(config)
compiler.outputFileSystem = MemoryFileSystem.fs
compiler.outputFileSystem.join = path.join;
promises.push(new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) reject(err)
if (stats.compilation.assets[config.output.filename]) {
files.push({
name: `${sourceDir}/resource/${baseNamespace}/${config.output.filename}`,
content: stats.compilation.assets[config.output.filename].source()
})
} else if (stats.compilation.errors.length > 0) {
reject(new Error(stats.compilation.errors[0]));
} else {
reject(new Error('file ' + config.output.filename + ' not found'));
}
resolve()
})
}))
})
await Promise.all(promises)
}
return files
}).then(CodeGeneratorResponse())
.catch(CodeGeneratorResponseError())