Skip to content

Commit fb74223

Browse files
committed
Simplified programmatic CLI api; Other minor optimizations
1 parent 9c76950 commit fb74223

22 files changed

+953
-806
lines changed

bin/pbjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
var path = require("path"),
33
cli = require(path.join(__dirname, "..", "cli", "pbjs.js"));
4-
var ret = cli.main(process.argv);
4+
var ret = cli.main(process.argv.slice(2));
55
if (typeof ret === 'number')
66
process.exit(ret);

bin/pbts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
var path = require("path"),
33
cli = require(path.join(__dirname, "..", "cli", "pbts.js"));
4-
var ret = cli.main(process.argv);
4+
var ret = cli.main(process.argv.slice(2));
55
if (typeof ret === 'number')
66
process.exit(ret);

cli/pbjs.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ var minimist = util.require("minimist", pkg.devDependencies.minimist),
1010
var protobuf = require(".."),
1111
targets = util.requireAll("./targets");
1212

13-
exports.main = function(args) {
14-
var argv = minimist(args.slice(2), {
13+
/**
14+
* Runs pbjs programmatically.
15+
* @param {string[]} args Command line arguments
16+
* @param {function(?Error)} [callback] Optional completion callback
17+
* @returns {number|undefined} Exit code, if known
18+
*/
19+
exports.main = function(args, callback) {
20+
var argv = minimist(args, {
1521
alias: {
1622
target : "t",
1723
out : "o",
@@ -63,6 +69,8 @@ exports.main = function(args) {
6369
"",
6470
"usage: " + chalk.bold.green(path.basename(process.argv[1])) + " [options] file1.proto file2.json ..."
6571
].join("\n"));
72+
if (callback)
73+
callback(Error("usage"));
6674
return 1;
6775
}
6876

@@ -96,18 +104,27 @@ exports.main = function(args) {
96104
};
97105

98106
root.load(files, function(err) {
99-
if (err)
100-
throw err;
101-
target(root, argv, function(err, output) {
102-
if (err)
107+
if (err) {
108+
if (callback)
109+
return callback(err);
110+
else
103111
throw err;
112+
}
113+
target(root, argv, function(err, output) {
114+
if (err) {
115+
if (callback)
116+
return callback(err);
117+
else
118+
throw err;
119+
}
104120
if (output !== "") {
105121
if (argv.out)
106122
fs.writeFileSync(argv.out, output, { encoding: "utf8" });
107123
else
108124
process.stdout.write(output, "utf8");
109125
}
110-
process.exit(0);
126+
if (callback)
127+
callback(null);
111128
});
112129
});
113130
};

cli/pbts.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ var jsdoc = util.require("jsdoc/package.json", pkg.devDependencies.jsdoc);
1212

1313
var protobuf = require("..");
1414

15-
exports.main = function(args) {
16-
var argv = minimist(args.slice(2), {
15+
/**
16+
* Runs pbts programmatically.
17+
* @param {string[]} args Command line arguments
18+
* @param {function(?Error)} [callback] Optional completion callback
19+
* @returns {number|undefined} Exit code, if known
20+
*/
21+
exports.main = function(args, callback) {
22+
var argv = minimist(args, {
1723
alias: {
1824
name: "n",
1925
out : "o"
@@ -35,6 +41,8 @@ exports.main = function(args) {
3541
"",
3642
"usage: " + chalk.bold.green(path.basename(process.argv[1])) + " [options] file1.js file2.js ..."
3743
].join("\n"));
44+
if (callback)
45+
callback(Error("usage"));
3846
return 1;
3947
}
4048

@@ -65,7 +73,11 @@ exports.main = function(args) {
6573
if (code) {
6674
out = out.join('').replace(/\s*JSDoc \d+\.\d+\.\d+ [^$]+/, "");
6775
process.stderr.write(out);
68-
process.exit(code);
76+
var err = Error("code " + code);
77+
if (callback)
78+
callback(err);
79+
else
80+
throw err;
6981
return;
7082
}
7183

@@ -74,10 +86,17 @@ exports.main = function(args) {
7486
"// Generated " + (new Date()).toUTCString().replace(/GMT/, "UTC"),
7587
].join('\n') + "\n" + out.join('');
7688

77-
if (argv.out)
78-
fs.writeFileSync(argv.out, output);
79-
else
80-
process.stdout.write(output, "utf8");
89+
try {
90+
if (argv.out)
91+
fs.writeFileSync(argv.out, output);
92+
else
93+
process.stdout.write(output, "utf8");
94+
} catch (err) {
95+
if (callback)
96+
callback(err);
97+
else
98+
throw err;
99+
}
81100
});
82101

83102
return undefined;

dist/protobuf.js

Lines changed: 16 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

-52 Bytes
Binary file not shown.

dist/protobuf.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.js

Lines changed: 1 addition & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)