-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (50 loc) · 1.38 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
'use strict';
const pbjs = require('protobufjs/cli/pbjs');
const progeny = require('progeny');
const formatError = (path, err) => {
const error = new Error(`${path}\n${err}`);
error.name = '';
return error;
};
class ProtoBufJSCompiler {
constructor(cfg) {
if (cfg == null) cfg = {};
this.config = cfg.plugins && cfg.plugins.proto || {};
if (this.config.includePath !== undefined) {
this.includePath = this.config.includePath;
}
}
get getDependencies() {
return progeny({
extension: 'proto',
regexp: /^\s*import\s+(?:public\s+)?"(.+)";/,
reverseArgs: true,
});
}
compile(file) {
let args = ['--target', 'json-module', '--wrap', 'commonjs', file.path];
if (this.includePath) {
args = args.concat(['-p', this.includePath]);
}
return new Promise((resolve, reject) => {
try {
pbjs.main(args, (err, output) => {
if (err) {
throw err;
}
const result = {
data: output,
};
return resolve(result);
});
} catch (err) {
return reject(formatError(err));
}
});
}
}
ProtoBufJSCompiler.prototype.brunchPlugin = true;
ProtoBufJSCompiler.prototype.type = 'javascript';
ProtoBufJSCompiler.prototype.extension = 'proto';
ProtoBufJSCompiler.prototype.targetExtension = 'js';
module.exports = ProtoBufJSCompiler;