@depup/apollo__protobufjs 1.2.7-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +39 -0
- package/README.md +32 -0
- package/bin/pbjs +6 -0
- package/bin/pbts +6 -0
- package/changes.json +14 -0
- package/cli/LICENSE +33 -0
- package/cli/README.md +11 -0
- package/cli/bin/pbjs +6 -0
- package/cli/bin/pbts +6 -0
- package/cli/index.d.ts +3 -0
- package/cli/index.js +3 -0
- package/cli/lib/tsd-jsdoc/LICENSE +21 -0
- package/cli/lib/tsd-jsdoc/README.md +23 -0
- package/cli/lib/tsd-jsdoc/plugin.js +21 -0
- package/cli/lib/tsd-jsdoc/publish.js +693 -0
- package/cli/lib/tsd-jsdoc.json +18 -0
- package/cli/package.json +7 -0
- package/cli/package.standalone.json +32 -0
- package/cli/pbjs.d.ts +9 -0
- package/cli/pbjs.js +331 -0
- package/cli/pbts.d.ts +9 -0
- package/cli/pbts.js +198 -0
- package/cli/targets/json-module.js +38 -0
- package/cli/targets/json.js +8 -0
- package/cli/targets/proto.js +326 -0
- package/cli/targets/proto2.js +10 -0
- package/cli/targets/proto3.js +10 -0
- package/cli/targets/static-module.js +29 -0
- package/cli/targets/static.js +709 -0
- package/cli/util.js +183 -0
- package/cli/wrappers/amd.js +7 -0
- package/cli/wrappers/closure.js +7 -0
- package/cli/wrappers/commonjs.js +7 -0
- package/cli/wrappers/default.js +15 -0
- package/cli/wrappers/es6.js +5 -0
- package/dist/README.md +31 -0
- package/dist/light/README.md +31 -0
- package/dist/light/protobuf.js +7198 -0
- package/dist/light/protobuf.js.map +1 -0
- package/dist/light/protobuf.min.js +7 -0
- package/dist/light/protobuf.min.js.map +1 -0
- package/dist/minimal/README.md +31 -0
- package/dist/minimal/protobuf.js +2675 -0
- package/dist/minimal/protobuf.js.map +1 -0
- package/dist/minimal/protobuf.min.js +7 -0
- package/dist/minimal/protobuf.min.js.map +1 -0
- package/dist/protobuf.js +8775 -0
- package/dist/protobuf.js.map +1 -0
- package/dist/protobuf.min.js +7 -0
- package/dist/protobuf.min.js.map +1 -0
- package/ext/debug/README.md +4 -0
- package/ext/debug/index.js +71 -0
- package/ext/descriptor/README.md +72 -0
- package/ext/descriptor/index.d.ts +191 -0
- package/ext/descriptor/index.js +1052 -0
- package/ext/descriptor/test.js +54 -0
- package/google/LICENSE +27 -0
- package/google/README.md +1 -0
- package/google/api/annotations.json +83 -0
- package/google/api/annotations.proto +11 -0
- package/google/api/http.json +86 -0
- package/google/api/http.proto +31 -0
- package/google/protobuf/api.json +118 -0
- package/google/protobuf/api.proto +34 -0
- package/google/protobuf/descriptor.json +739 -0
- package/google/protobuf/descriptor.proto +286 -0
- package/google/protobuf/source_context.json +20 -0
- package/google/protobuf/source_context.proto +7 -0
- package/google/protobuf/type.json +202 -0
- package/google/protobuf/type.proto +89 -0
- package/index.d.ts +2628 -0
- package/index.js +4 -0
- package/light.d.ts +2 -0
- package/light.js +4 -0
- package/minimal.d.ts +2 -0
- package/minimal.js +4 -0
- package/package.json +147 -0
- package/scripts/postinstall.js +35 -0
- package/src/common.js +399 -0
- package/src/converter.js +304 -0
- package/src/decoder.js +106 -0
- package/src/encoder.js +119 -0
- package/src/enum.js +181 -0
- package/src/field.js +379 -0
- package/src/index-light.js +104 -0
- package/src/index-minimal.js +36 -0
- package/src/index.js +12 -0
- package/src/mapfield.js +126 -0
- package/src/message.js +139 -0
- package/src/method.js +151 -0
- package/src/namespace.js +433 -0
- package/src/object.js +200 -0
- package/src/oneof.js +203 -0
- package/src/parse.js +761 -0
- package/src/reader.js +405 -0
- package/src/reader_buffer.js +44 -0
- package/src/root.js +353 -0
- package/src/roots.js +18 -0
- package/src/rpc/service.js +142 -0
- package/src/rpc.js +36 -0
- package/src/service.js +167 -0
- package/src/tokenize.js +397 -0
- package/src/type.js +589 -0
- package/src/types.js +196 -0
- package/src/typescript.jsdoc +22 -0
- package/src/util/longbits.js +200 -0
- package/src/util/minimal.js +406 -0
- package/src/util.js +178 -0
- package/src/verifier.js +191 -0
- package/src/wrappers.js +83 -0
- package/src/writer.js +459 -0
- package/src/writer_buffer.js +81 -0
- package/tsconfig.json +7 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "protobufjs-cli",
|
|
3
|
+
"description": "Translates between file formats and generates static code as well as TypeScript definitions.",
|
|
4
|
+
"version": "6.7.0",
|
|
5
|
+
"author": "Daniel Wirtz <dcode+protobufjs@dcode.io>",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/dcodeIO/protobuf.js.git"
|
|
9
|
+
},
|
|
10
|
+
"license": "BSD-3-Clause",
|
|
11
|
+
"main": "index.js",
|
|
12
|
+
"types": "index.d.ts",
|
|
13
|
+
"bin": {
|
|
14
|
+
"pbjs": "bin/pbjs",
|
|
15
|
+
"pbts": "bin/pbts"
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@apollo/protobufjs": "~1.0.5"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"chalk": "^1.1.3",
|
|
22
|
+
"escodegen": "^1.8.1",
|
|
23
|
+
"espree": "^3.1.3",
|
|
24
|
+
"estraverse": "^4.2.0",
|
|
25
|
+
"glob": "^7.1.1",
|
|
26
|
+
"jsdoc": "^3.4.2",
|
|
27
|
+
"minimist": "^1.2.0",
|
|
28
|
+
"semver": "^5.3.0",
|
|
29
|
+
"tmp": "0.0.31",
|
|
30
|
+
"uglify-js": "^2.8.15"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/cli/pbjs.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type pbjsCallback = (err: Error|null, output?: string) => void;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Runs pbjs programmatically.
|
|
5
|
+
* @param {string[]} args Command line arguments
|
|
6
|
+
* @param {function(?Error, string=)} [callback] Optional completion callback
|
|
7
|
+
* @returns {number|undefined} Exit code, if known
|
|
8
|
+
*/
|
|
9
|
+
export function main(args: string[], callback?: pbjsCallback): number|undefined;
|
package/cli/pbjs.js
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var path = require("path"),
|
|
3
|
+
fs = require("fs"),
|
|
4
|
+
pkg = require("./package.json"),
|
|
5
|
+
util = require("./util");
|
|
6
|
+
|
|
7
|
+
util.setup();
|
|
8
|
+
|
|
9
|
+
var protobuf = require(util.pathToProtobufJs),
|
|
10
|
+
minimist = require("minimist"),
|
|
11
|
+
chalk = require("chalk"),
|
|
12
|
+
glob = require("glob");
|
|
13
|
+
|
|
14
|
+
var targets = util.requireAll("./targets");
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Runs pbjs programmatically.
|
|
18
|
+
* @param {string[]} args Command line arguments
|
|
19
|
+
* @param {function(?Error, string=)} [callback] Optional completion callback
|
|
20
|
+
* @returns {number|undefined} Exit code, if known
|
|
21
|
+
*/
|
|
22
|
+
exports.main = function main(args, callback) {
|
|
23
|
+
var lintDefault = "eslint-disable " + [
|
|
24
|
+
"block-scoped-var",
|
|
25
|
+
"id-length",
|
|
26
|
+
"no-control-regex",
|
|
27
|
+
"no-magic-numbers",
|
|
28
|
+
"no-prototype-builtins",
|
|
29
|
+
"no-redeclare",
|
|
30
|
+
"no-shadow",
|
|
31
|
+
"no-var",
|
|
32
|
+
"sort-vars"
|
|
33
|
+
].join(", ");
|
|
34
|
+
var argv = minimist(args, {
|
|
35
|
+
alias: {
|
|
36
|
+
target: "t",
|
|
37
|
+
out: "o",
|
|
38
|
+
path: "p",
|
|
39
|
+
wrap: "w",
|
|
40
|
+
root: "r",
|
|
41
|
+
lint: "l",
|
|
42
|
+
// backward compatibility:
|
|
43
|
+
"force-long": "strict-long",
|
|
44
|
+
"force-message": "strict-message"
|
|
45
|
+
},
|
|
46
|
+
string: [ "target", "out", "path", "wrap", "dependency", "root", "lint" ],
|
|
47
|
+
boolean: [ "create", "encode", "decode", "verify", "convert", "from-object", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message" ],
|
|
48
|
+
default: {
|
|
49
|
+
target: "json",
|
|
50
|
+
create: true,
|
|
51
|
+
encode: true,
|
|
52
|
+
decode: true,
|
|
53
|
+
verify: true,
|
|
54
|
+
convert: true,
|
|
55
|
+
"from-object": true,
|
|
56
|
+
delimited: true,
|
|
57
|
+
beautify: true,
|
|
58
|
+
comments: true,
|
|
59
|
+
es6: null,
|
|
60
|
+
lint: lintDefault,
|
|
61
|
+
"keep-case": false,
|
|
62
|
+
"force-long": false,
|
|
63
|
+
"force-number": false,
|
|
64
|
+
"force-enum-string": false,
|
|
65
|
+
"force-message": false
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
var target = targets[argv.target],
|
|
70
|
+
files = argv._,
|
|
71
|
+
paths = typeof argv.path === "string" ? [ argv.path ] : argv.path || [];
|
|
72
|
+
|
|
73
|
+
// alias hyphen args in camel case
|
|
74
|
+
Object.keys(argv).forEach(function(key) {
|
|
75
|
+
var camelKey = key.replace(/-([a-z])/g, function($0, $1) { return $1.toUpperCase(); });
|
|
76
|
+
if (camelKey !== key)
|
|
77
|
+
argv[camelKey] = argv[key];
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// protobuf.js package directory contains additional, otherwise non-bundled google types
|
|
81
|
+
paths.push(path.relative(process.cwd(), path.join(__dirname, "..")) || ".");
|
|
82
|
+
|
|
83
|
+
if (!files.length) {
|
|
84
|
+
var descs = Object.keys(targets).filter(function(key) { return !targets[key].private; }).map(function(key) {
|
|
85
|
+
return " " + util.pad(key, 14, true) + targets[key].description;
|
|
86
|
+
});
|
|
87
|
+
if (callback)
|
|
88
|
+
callback(Error("usage")); // eslint-disable-line callback-return
|
|
89
|
+
else
|
|
90
|
+
process.stderr.write([
|
|
91
|
+
"protobuf.js v" + pkg.version + " CLI for JavaScript",
|
|
92
|
+
"",
|
|
93
|
+
chalk.bold.white("Translates between file formats and generates static code."),
|
|
94
|
+
"",
|
|
95
|
+
" -t, --target Specifies the target format. Also accepts a path to require a custom target.",
|
|
96
|
+
"",
|
|
97
|
+
descs.join("\n"),
|
|
98
|
+
"",
|
|
99
|
+
" -p, --path Adds a directory to the include path.",
|
|
100
|
+
"",
|
|
101
|
+
" -o, --out Saves to a file instead of writing to stdout.",
|
|
102
|
+
"",
|
|
103
|
+
" --sparse Exports only those types referenced from a main file (experimental).",
|
|
104
|
+
"",
|
|
105
|
+
chalk.bold.gray(" Module targets only:"),
|
|
106
|
+
"",
|
|
107
|
+
" -w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.",
|
|
108
|
+
"",
|
|
109
|
+
" default Default wrapper supporting both CommonJS and AMD",
|
|
110
|
+
" commonjs CommonJS wrapper",
|
|
111
|
+
" amd AMD wrapper",
|
|
112
|
+
" es6 ES6 wrapper (implies --es6)",
|
|
113
|
+
" closure A closure adding to protobuf.roots where protobuf is a global",
|
|
114
|
+
"",
|
|
115
|
+
" --dependency Specifies which version of protobuf to require. Accepts any valid module id",
|
|
116
|
+
"",
|
|
117
|
+
" -r, --root Specifies an alternative protobuf.roots name.",
|
|
118
|
+
"",
|
|
119
|
+
" -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:",
|
|
120
|
+
"",
|
|
121
|
+
" " + lintDefault,
|
|
122
|
+
"",
|
|
123
|
+
" --es6 Enables ES6 syntax (const/let instead of var)",
|
|
124
|
+
"",
|
|
125
|
+
chalk.bold.gray(" Proto sources only:"),
|
|
126
|
+
"",
|
|
127
|
+
" --keep-case Keeps field casing instead of converting to camel case.",
|
|
128
|
+
"",
|
|
129
|
+
chalk.bold.gray(" Static targets only:"),
|
|
130
|
+
"",
|
|
131
|
+
" --no-create Does not generate create functions used for reflection compatibility.",
|
|
132
|
+
" --no-encode Does not generate encode functions.",
|
|
133
|
+
" --no-decode Does not generate decode functions.",
|
|
134
|
+
" --no-verify Does not generate verify functions.",
|
|
135
|
+
" --no-convert Does not generate convert functions like from/toObject",
|
|
136
|
+
" --no-from-object Does not generate fromObject",
|
|
137
|
+
" --no-delimited Does not generate delimited encode/decode functions.",
|
|
138
|
+
" --no-beautify Does not beautify generated code.",
|
|
139
|
+
" --no-comments Does not output any JSDoc comments.",
|
|
140
|
+
"",
|
|
141
|
+
" --force-long Enfores the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.",
|
|
142
|
+
" --force-number Enfores the use of 'number' for s-/u-/int64 and s-/fixed64 fields.",
|
|
143
|
+
" --force-message Enfores the use of message instances instead of plain objects.",
|
|
144
|
+
"",
|
|
145
|
+
"usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or pipe) ") + "other | " + chalk.bold.green("pbjs") + " [options] -",
|
|
146
|
+
""
|
|
147
|
+
].join("\n"));
|
|
148
|
+
return 1;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (typeof argv["strict-long"] === "boolean")
|
|
152
|
+
argv["force-long"] = argv["strict-long"];
|
|
153
|
+
|
|
154
|
+
// Resolve glob expressions
|
|
155
|
+
for (var i = 0; i < files.length;) {
|
|
156
|
+
if (glob.hasMagic(files[i])) {
|
|
157
|
+
var matches = glob.sync(files[i]);
|
|
158
|
+
Array.prototype.splice.apply(files, [i, 1].concat(matches));
|
|
159
|
+
i += matches.length;
|
|
160
|
+
} else
|
|
161
|
+
++i;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Require custom target
|
|
165
|
+
if (!target)
|
|
166
|
+
target = require(path.resolve(process.cwd(), argv.target));
|
|
167
|
+
|
|
168
|
+
var root = new protobuf.Root();
|
|
169
|
+
|
|
170
|
+
var mainFiles = [];
|
|
171
|
+
|
|
172
|
+
// Search include paths when resolving imports
|
|
173
|
+
root.resolvePath = function pbjsResolvePath(origin, target) {
|
|
174
|
+
var normOrigin = protobuf.util.path.normalize(origin),
|
|
175
|
+
normTarget = protobuf.util.path.normalize(target);
|
|
176
|
+
if (!normOrigin)
|
|
177
|
+
mainFiles.push(normTarget);
|
|
178
|
+
|
|
179
|
+
var resolved = protobuf.util.path.resolve(normOrigin, normTarget, true);
|
|
180
|
+
var idx = resolved.lastIndexOf("google/protobuf/");
|
|
181
|
+
if (idx > -1) {
|
|
182
|
+
var altname = resolved.substring(idx);
|
|
183
|
+
if (altname in protobuf.common)
|
|
184
|
+
resolved = altname;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (fs.existsSync(resolved))
|
|
188
|
+
return resolved;
|
|
189
|
+
|
|
190
|
+
for (var i = 0; i < paths.length; ++i) {
|
|
191
|
+
var iresolved = protobuf.util.path.resolve(paths[i] + "/", target);
|
|
192
|
+
if (fs.existsSync(iresolved))
|
|
193
|
+
return iresolved;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return resolved;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
// Use es6 syntax if not explicitly specified on the command line and the es6 wrapper is used
|
|
200
|
+
if (argv.wrap === "es6" || argv.es6) {
|
|
201
|
+
argv.wrap = "es6";
|
|
202
|
+
argv.es6 = true;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
var parseOptions = {
|
|
206
|
+
"keepCase": argv["keep-case"] || false
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// Read from stdin
|
|
210
|
+
if (files.length === 1 && files[0] === "-") {
|
|
211
|
+
var data = [];
|
|
212
|
+
process.stdin.on("data", function(chunk) {
|
|
213
|
+
data.push(chunk);
|
|
214
|
+
});
|
|
215
|
+
process.stdin.on("end", function() {
|
|
216
|
+
var source = Buffer.concat(data).toString("utf8");
|
|
217
|
+
try {
|
|
218
|
+
if (source.charAt(0) !== "{") {
|
|
219
|
+
protobuf.parse.filename = "-";
|
|
220
|
+
protobuf.parse(source, root, parseOptions);
|
|
221
|
+
} else {
|
|
222
|
+
var json = JSON.parse(source);
|
|
223
|
+
root.setOptions(json.options).addJSON(json);
|
|
224
|
+
}
|
|
225
|
+
callTarget();
|
|
226
|
+
} catch (err) {
|
|
227
|
+
if (callback) {
|
|
228
|
+
callback(err);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
throw err;
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
// Load from disk
|
|
236
|
+
} else {
|
|
237
|
+
try {
|
|
238
|
+
root.loadSync(files, parseOptions).resolveAll(); // sync is deterministic while async is not
|
|
239
|
+
if (argv.sparse)
|
|
240
|
+
sparsify(root);
|
|
241
|
+
callTarget();
|
|
242
|
+
} catch (err) {
|
|
243
|
+
if (callback) {
|
|
244
|
+
callback(err);
|
|
245
|
+
return undefined;
|
|
246
|
+
}
|
|
247
|
+
throw err;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function markReferenced(tobj) {
|
|
252
|
+
tobj.referenced = true;
|
|
253
|
+
// also mark a type's fields and oneofs
|
|
254
|
+
if (tobj.fieldsArray)
|
|
255
|
+
tobj.fieldsArray.forEach(function(fobj) {
|
|
256
|
+
fobj.referenced = true;
|
|
257
|
+
});
|
|
258
|
+
if (tobj.oneofsArray)
|
|
259
|
+
tobj.oneofsArray.forEach(function(oobj) {
|
|
260
|
+
oobj.referenced = true;
|
|
261
|
+
});
|
|
262
|
+
// also mark an extension field's extended type, but not its (other) fields
|
|
263
|
+
if (tobj.extensionField)
|
|
264
|
+
tobj.extensionField.parent.referenced = true;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function sparsify(root) {
|
|
268
|
+
|
|
269
|
+
// 1. mark directly or indirectly referenced objects
|
|
270
|
+
util.traverse(root, function(obj) {
|
|
271
|
+
if (!obj.filename)
|
|
272
|
+
return;
|
|
273
|
+
if (mainFiles.indexOf(obj.filename) > -1)
|
|
274
|
+
util.traverseResolved(obj, markReferenced);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
// 2. empty unreferenced objects
|
|
278
|
+
util.traverse(root, function(obj) {
|
|
279
|
+
var parent = obj.parent;
|
|
280
|
+
if (!parent || obj.referenced) // root or referenced
|
|
281
|
+
return;
|
|
282
|
+
// remove unreferenced namespaces
|
|
283
|
+
if (obj instanceof protobuf.Namespace) {
|
|
284
|
+
var hasReferenced = false;
|
|
285
|
+
util.traverse(obj, function(iobj) {
|
|
286
|
+
if (iobj.referenced)
|
|
287
|
+
hasReferenced = true;
|
|
288
|
+
});
|
|
289
|
+
if (hasReferenced) { // replace with plain namespace if a namespace subclass
|
|
290
|
+
if (obj instanceof protobuf.Type || obj instanceof protobuf.Service) {
|
|
291
|
+
var robj = new protobuf.Namespace(obj.name, obj.options);
|
|
292
|
+
robj.nested = obj.nested;
|
|
293
|
+
parent.add(robj);
|
|
294
|
+
}
|
|
295
|
+
} else // remove completely if nothing inside is referenced
|
|
296
|
+
parent.remove(obj);
|
|
297
|
+
|
|
298
|
+
// remove everything else unreferenced
|
|
299
|
+
} else if (!(obj instanceof protobuf.Namespace))
|
|
300
|
+
parent.remove(obj);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
// 3. validate that everything is fine
|
|
304
|
+
root.resolveAll();
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function callTarget() {
|
|
308
|
+
target(root, argv, function targetCallback(err, output) {
|
|
309
|
+
if (err) {
|
|
310
|
+
if (callback)
|
|
311
|
+
return callback(err);
|
|
312
|
+
throw err;
|
|
313
|
+
}
|
|
314
|
+
try {
|
|
315
|
+
if (argv.out)
|
|
316
|
+
fs.writeFileSync(argv.out, output, { encoding: "utf8" });
|
|
317
|
+
else if (!callback)
|
|
318
|
+
process.stdout.write(output, "utf8");
|
|
319
|
+
return callback
|
|
320
|
+
? callback(null, output)
|
|
321
|
+
: undefined;
|
|
322
|
+
} catch (err) {
|
|
323
|
+
if (callback)
|
|
324
|
+
return callback(err);
|
|
325
|
+
throw err;
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return undefined;
|
|
331
|
+
};
|
package/cli/pbts.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type pbtsCallback = (err: Error|null, output?: string) => void;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Runs pbts programmatically.
|
|
5
|
+
* @param {string[]} args Command line arguments
|
|
6
|
+
* @param {function(?Error, string=)} [callback] Optional completion callback
|
|
7
|
+
* @returns {number|undefined} Exit code, if known
|
|
8
|
+
*/
|
|
9
|
+
export function main(args: string[], callback?: pbtsCallback): number|undefined;
|
package/cli/pbts.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var child_process = require("child_process"),
|
|
3
|
+
path = require("path"),
|
|
4
|
+
fs = require("fs"),
|
|
5
|
+
pkg = require("./package.json"),
|
|
6
|
+
util = require("./util");
|
|
7
|
+
|
|
8
|
+
util.setup();
|
|
9
|
+
|
|
10
|
+
var minimist = require("minimist"),
|
|
11
|
+
chalk = require("chalk"),
|
|
12
|
+
glob = require("glob"),
|
|
13
|
+
tmp = require("tmp");
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Runs pbts programmatically.
|
|
17
|
+
* @param {string[]} args Command line arguments
|
|
18
|
+
* @param {function(?Error, string=)} [callback] Optional completion callback
|
|
19
|
+
* @returns {number|undefined} Exit code, if known
|
|
20
|
+
*/
|
|
21
|
+
exports.main = function(args, callback) {
|
|
22
|
+
var argv = minimist(args, {
|
|
23
|
+
alias: {
|
|
24
|
+
name: "n",
|
|
25
|
+
out : "o",
|
|
26
|
+
main: "m",
|
|
27
|
+
global: "g",
|
|
28
|
+
import: "i"
|
|
29
|
+
},
|
|
30
|
+
string: [ "name", "out", "global", "import" ],
|
|
31
|
+
boolean: [ "comments", "main" ],
|
|
32
|
+
default: {
|
|
33
|
+
comments: true,
|
|
34
|
+
main: false
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
var files = argv._;
|
|
39
|
+
|
|
40
|
+
if (!files.length) {
|
|
41
|
+
if (callback)
|
|
42
|
+
callback(Error("usage")); // eslint-disable-line callback-return
|
|
43
|
+
else
|
|
44
|
+
process.stderr.write([
|
|
45
|
+
"protobuf.js v" + pkg.version + " CLI for TypeScript",
|
|
46
|
+
"",
|
|
47
|
+
chalk.bold.white("Generates TypeScript definitions from annotated JavaScript files."),
|
|
48
|
+
"",
|
|
49
|
+
" -o, --out Saves to a file instead of writing to stdout.",
|
|
50
|
+
"",
|
|
51
|
+
" -g, --global Name of the global object in browser environments, if any.",
|
|
52
|
+
"",
|
|
53
|
+
" -i, --import Comma delimited list of imports. Local names will equal camelCase of the basename.",
|
|
54
|
+
"",
|
|
55
|
+
" --no-comments Does not output any JSDoc comments.",
|
|
56
|
+
"",
|
|
57
|
+
chalk.bold.gray(" Internal flags:"),
|
|
58
|
+
"",
|
|
59
|
+
" -n, --name Wraps everything in a module of the specified name.",
|
|
60
|
+
"",
|
|
61
|
+
" -m, --main Whether building the main library without any imports.",
|
|
62
|
+
"",
|
|
63
|
+
"usage: " + chalk.bold.green("pbts") + " [options] file1.js file2.js ..." + chalk.bold.gray(" (or) ") + "other | " + chalk.bold.green("pbts") + " [options] -",
|
|
64
|
+
""
|
|
65
|
+
].join("\n"));
|
|
66
|
+
return 1;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Resolve glob expressions
|
|
70
|
+
for (var i = 0; i < files.length;) {
|
|
71
|
+
if (glob.hasMagic(files[i])) {
|
|
72
|
+
var matches = glob.sync(files[i]);
|
|
73
|
+
Array.prototype.splice.apply(files, [i, 1].concat(matches));
|
|
74
|
+
i += matches.length;
|
|
75
|
+
} else
|
|
76
|
+
++i;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
var cleanup = [];
|
|
80
|
+
|
|
81
|
+
// Read from stdin (to a temporary file)
|
|
82
|
+
if (files.length === 1 && files[0] === "-") {
|
|
83
|
+
var data = [];
|
|
84
|
+
process.stdin.on("data", function(chunk) {
|
|
85
|
+
data.push(chunk);
|
|
86
|
+
});
|
|
87
|
+
process.stdin.on("end", function() {
|
|
88
|
+
files[0] = tmp.tmpNameSync() + ".js";
|
|
89
|
+
fs.writeFileSync(files[0], Buffer.concat(data));
|
|
90
|
+
cleanup.push(files[0]);
|
|
91
|
+
callJsdoc();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Load from disk
|
|
95
|
+
} else {
|
|
96
|
+
callJsdoc();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function callJsdoc() {
|
|
100
|
+
|
|
101
|
+
// There is no proper API for jsdoc, so this executes the CLI and pipes the output
|
|
102
|
+
var basedir = path.join(__dirname, ".");
|
|
103
|
+
var moduleName = argv.name || "null";
|
|
104
|
+
var nodePath = process.execPath;
|
|
105
|
+
var cmd = "\"" + nodePath + "\" \"" + require.resolve("jsdoc/jsdoc.js") + "\" -c \"" + path.join(basedir, "lib", "tsd-jsdoc.json") + "\" -q \"module=" + encodeURIComponent(moduleName) + "&comments=" + Boolean(argv.comments) + "\" " + files.map(function(file) { return "\"" + file + "\""; }).join(" ");
|
|
106
|
+
var child = child_process.exec(cmd, {
|
|
107
|
+
cwd: process.cwd(),
|
|
108
|
+
argv0: "node",
|
|
109
|
+
stdio: "pipe",
|
|
110
|
+
maxBuffer: 1 << 24 // 16mb
|
|
111
|
+
});
|
|
112
|
+
var out = [];
|
|
113
|
+
var ended = false;
|
|
114
|
+
var closed = false;
|
|
115
|
+
child.stdout.on("data", function(data) {
|
|
116
|
+
out.push(data);
|
|
117
|
+
});
|
|
118
|
+
child.stdout.on("end", function() {
|
|
119
|
+
if (closed) finish();
|
|
120
|
+
else ended = true;
|
|
121
|
+
});
|
|
122
|
+
child.stderr.pipe(process.stderr);
|
|
123
|
+
child.on("close", function(code) {
|
|
124
|
+
// clean up temporary files, no matter what
|
|
125
|
+
try { cleanup.forEach(fs.unlinkSync); } catch(e) {/**/} cleanup = [];
|
|
126
|
+
|
|
127
|
+
if (code) {
|
|
128
|
+
out = out.join("").replace(/\s*JSDoc \d+\.\d+\.\d+ [^$]+/, "");
|
|
129
|
+
process.stderr.write(out);
|
|
130
|
+
var err = Error("code " + code);
|
|
131
|
+
if (callback)
|
|
132
|
+
return callback(err);
|
|
133
|
+
throw err;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (ended) return finish();
|
|
137
|
+
closed = true;
|
|
138
|
+
return undefined;
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
function getImportName(importItem) {
|
|
142
|
+
return path.basename(importItem, ".js").replace(/([-_~.+]\w)/g, function(match) {
|
|
143
|
+
return match[1].toUpperCase();
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function finish() {
|
|
148
|
+
var output = [];
|
|
149
|
+
if (argv.main)
|
|
150
|
+
output.push(
|
|
151
|
+
"// DO NOT EDIT! This is a generated file. Edit the JSDoc in src/*.js instead and run 'npm run types'.",
|
|
152
|
+
""
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
if (argv.global)
|
|
156
|
+
output.push(
|
|
157
|
+
"export as namespace " + argv.global + ";",
|
|
158
|
+
""
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
if (!argv.main) {
|
|
162
|
+
// Ensure we have a usable array of imports
|
|
163
|
+
var importArray = typeof argv.import === "string" ? argv.import.split(",") : argv.import || [];
|
|
164
|
+
|
|
165
|
+
// Build an object of imports and paths
|
|
166
|
+
var imports = {
|
|
167
|
+
$protobuf: "@apollo/protobufjs"
|
|
168
|
+
};
|
|
169
|
+
importArray.forEach(function(importItem) {
|
|
170
|
+
imports[getImportName(importItem)] = importItem;
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// Write out the imports
|
|
174
|
+
Object.keys(imports).forEach(function(key) {
|
|
175
|
+
output.push("import * as " + key + " from \"" + imports[key] + "\";");
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
output = output.join("\n") + "\n" + out.join("");
|
|
180
|
+
|
|
181
|
+
try {
|
|
182
|
+
if (argv.out)
|
|
183
|
+
fs.writeFileSync(argv.out, output, { encoding: "utf8" });
|
|
184
|
+
else if (!callback)
|
|
185
|
+
process.stdout.write(output, "utf8");
|
|
186
|
+
return callback
|
|
187
|
+
? callback(null, output)
|
|
188
|
+
: undefined;
|
|
189
|
+
} catch (err) {
|
|
190
|
+
if (callback)
|
|
191
|
+
return callback(err);
|
|
192
|
+
throw err;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return undefined;
|
|
198
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = json_module;
|
|
3
|
+
|
|
4
|
+
var util = require("../util");
|
|
5
|
+
|
|
6
|
+
var protobuf = require("../..");
|
|
7
|
+
|
|
8
|
+
json_module.description = "JSON representation as a module";
|
|
9
|
+
|
|
10
|
+
function jsonSafeProp(json) {
|
|
11
|
+
return json.replace(/^( +)"(\w+)":/mg, function($0, $1, $2) {
|
|
12
|
+
return protobuf.util.safeProp($2).charAt(0) === "."
|
|
13
|
+
? $1 + $2 + ":"
|
|
14
|
+
: $0;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function json_module(root, options, callback) {
|
|
19
|
+
try {
|
|
20
|
+
var rootProp = protobuf.util.safeProp(options.root || "default");
|
|
21
|
+
var output = [
|
|
22
|
+
(options.es6 ? "const" : "var") + " $root = ($protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = new $protobuf.Root()))\n"
|
|
23
|
+
];
|
|
24
|
+
if (root.options) {
|
|
25
|
+
var optionsJson = jsonSafeProp(JSON.stringify(root.options, null, 2));
|
|
26
|
+
output.push(".setOptions(" + optionsJson + ")\n");
|
|
27
|
+
}
|
|
28
|
+
var json = jsonSafeProp(JSON.stringify(root.nested, null, 2).trim());
|
|
29
|
+
output.push(".addJSON(" + json + ");");
|
|
30
|
+
output = util.wrap(output.join(""), protobuf.util.merge({ dependency: "@apollo/protobufjs/light" }, options));
|
|
31
|
+
process.nextTick(function() {
|
|
32
|
+
callback(null, output);
|
|
33
|
+
});
|
|
34
|
+
} catch (e) {
|
|
35
|
+
return callback(e);
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|