-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
160 lines (148 loc) · 4.63 KB
/
gulpfile.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
'use strict';
const gulp = require('gulp');
const through = require('through2')
const PluginError = require('plugin-error');
const replace = require('gulp-replace');
const rename = require('gulp-rename');
const PLUGIN_NAME = 'proto2typescript-interfaces';
function buildProtoTS(file, path, callback) {
if (file.isNull()) {
// nothing to do
return callback(null, file);
}
if (file.isStream()) {
// file.contents is a Stream - https://nodejs.org/api/stream.html
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams not supported!'));
// or, if you can handle Streams:
//file.contents = file.contents.pipe(...
//return callback(null, file);
return callback(null, file);
} else if (file.isBuffer()) {
function protoToTsType(p) {
const types = {
"double": "number",
"float": "number",
"int32": "number",
"int64": "number",
"uint32": "number",
"uint64": "number",
"sint32": "number",
"sint64": "number",
"fixed32": "number",
"fixed64": "number",
"sfixed32": "number",
"sfixed64": "number",
"bool": "boolean",
"string": "string",
"bytes": "string",
"Code": "number",
"interface Error {": "",
};
if (types[p]) {
return types[p];
}
return p;
}
function parseProtobufLine(line) {
if (!line) {
return "";
}
const indent = line.length - line.trimLeft().length
const indentChar = line[0];
const tokens = line.trim().split(" ").filter(Boolean);
let isRepeated = false;
// debugger;
switch (tokens[0]) {
case "//":
return line;
case "}":
return "}";
case "message":
return "interface " + tokens[1] + " {";
case "repeated":
isRepeated = true;
tokens.shift();
}
return `${indentChar.repeat(indent)}${tokens[1]}: ${protoToTsType(tokens[0])}${ isRepeated ? "[]" : ""}`;
}
let parsed = "";
if (file.path.indexOf('Common.proto') > 0) {
parsed += "declare namespace Common {\n";
}
if (file.path.indexOf('SubAPI.proto') > 0) {
parsed += "declare namespace SubAPI {\n";
}
if (file.path.indexOf('AdminAPI.proto') > 0) {
parsed += "declare namespace AdminAPI {\n";
}
let pb = file.contents.toString();
let inMessage = false;
for (const line of pb.split("\n")) {
if (line.indexOf('message') !== -1) {
inMessage = true;
}
if (inMessage) {
parsed += parseProtobufLine(line);
parsed += "\n";
}
if (line.indexOf('}') !== -1) {
inMessage = false;
}
}
if (file.path.indexOf('Common.proto') > 0 || file.path.indexOf('SubAPI.proto') > 0 || file.path.indexOf('AdminAPI.proto') > 0) {
parsed += "}";
}
file.contents = Buffer(parsed)
file.path = file.path.replace('proto', 'd.ts')
callback(null, file)
}
}
gulp.task('build-proto-ts', () => {
let stream = gulp.src('Gigamunch-Proto/**/*.proto')
.pipe(
through.obj(buildProtoTS)
)
.pipe(
gulp.dest('Gigamunch-Proto')
)
.pipe(rename({
dirname: ''
}))
.pipe(
gulp.dest('admin/app/ts/prototypes')
)
return new Promise((resolve, reject) => {
stream.on('end', resolve);
stream.on('error', reject);
});
});
gulp.task('build-proto-go', () => {
let stream = gulp.src('Gigamunch-Proto/*/*.pb.go')
.pipe(replace('../pbcommon', 'github.com/atishpatel/Gigamunch-Backend/Gigamunch-Proto/pbcommon'))
.pipe(replace(',omitempty', ''))
.pipe(replace('Url', 'URL'))
.pipe(replace('Id', 'ID'))
.pipe(replace('Sms', 'SMS'))
.pipe(replace('Option_1', 'Option1'))
.pipe(replace('Option_2', 'Option2'))
.pipe(replace('Instructions_1', 'Instructions1'))
.pipe(replace('Instructions_2', 'Instructions2'))
.pipe(replace('Time_1', 'Time1'))
.pipe(replace('Time_2', 'Time2'))
.pipe(gulp.dest('Gigamunch-Proto/'));
return new Promise((resolve, reject) => {
stream.on('end', resolve);
stream.on('error', reject);
});
});
gulp.task('build-proto-swagger', () => {
let stream = gulp.src(['*/*/*.swagger.json', '*/*.swagger.json'])
.pipe(replace('"http",', ''))
.pipe(replace('"2.0",', '"2.0",\n"securityDefinitions": {"auth-token": {"type": "apiKey","in": "header","name": "auth-token"}},"security": [{"auth-token": []}],'))
.pipe(gulp.dest('.'));
return new Promise((resolve, reject) => {
stream.on('end', resolve);
stream.on('error', reject);
});
});
gulp.task('build', gulp.parallel('build-proto-ts', 'build-proto-go', 'build-proto-swagger'));