Skip to content

Commit

Permalink
Simplified programmatic CLI api; Other minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 15, 2016
1 parent 9c76950 commit fb74223
Show file tree
Hide file tree
Showing 22 changed files with 953 additions and 806 deletions.
2 changes: 1 addition & 1 deletion bin/pbjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
var path = require("path"),
cli = require(path.join(__dirname, "..", "cli", "pbjs.js"));
var ret = cli.main(process.argv);
var ret = cli.main(process.argv.slice(2));
if (typeof ret === 'number')
process.exit(ret);
2 changes: 1 addition & 1 deletion bin/pbts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
var path = require("path"),
cli = require(path.join(__dirname, "..", "cli", "pbts.js"));
var ret = cli.main(process.argv);
var ret = cli.main(process.argv.slice(2));
if (typeof ret === 'number')
process.exit(ret);
31 changes: 24 additions & 7 deletions cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ var minimist = util.require("minimist", pkg.devDependencies.minimist),
var protobuf = require(".."),
targets = util.requireAll("./targets");

exports.main = function(args) {
var argv = minimist(args.slice(2), {
/**
* Runs pbjs programmatically.
* @param {string[]} args Command line arguments
* @param {function(?Error)} [callback] Optional completion callback
* @returns {number|undefined} Exit code, if known
*/
exports.main = function(args, callback) {
var argv = minimist(args, {
alias: {
target : "t",
out : "o",
Expand Down Expand Up @@ -63,6 +69,8 @@ exports.main = function(args) {
"",
"usage: " + chalk.bold.green(path.basename(process.argv[1])) + " [options] file1.proto file2.json ..."
].join("\n"));
if (callback)
callback(Error("usage"));
return 1;
}

Expand Down Expand Up @@ -96,18 +104,27 @@ exports.main = function(args) {
};

root.load(files, function(err) {
if (err)
throw err;
target(root, argv, function(err, output) {
if (err)
if (err) {
if (callback)
return callback(err);
else
throw err;
}
target(root, argv, function(err, output) {
if (err) {
if (callback)
return callback(err);
else
throw err;
}
if (output !== "") {
if (argv.out)
fs.writeFileSync(argv.out, output, { encoding: "utf8" });
else
process.stdout.write(output, "utf8");
}
process.exit(0);
if (callback)
callback(null);
});
});
};
33 changes: 26 additions & 7 deletions cli/pbts.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ var jsdoc = util.require("jsdoc/package.json", pkg.devDependencies.jsdoc);

var protobuf = require("..");

exports.main = function(args) {
var argv = minimist(args.slice(2), {
/**
* Runs pbts programmatically.
* @param {string[]} args Command line arguments
* @param {function(?Error)} [callback] Optional completion callback
* @returns {number|undefined} Exit code, if known
*/
exports.main = function(args, callback) {
var argv = minimist(args, {
alias: {
name: "n",
out : "o"
Expand All @@ -35,6 +41,8 @@ exports.main = function(args) {
"",
"usage: " + chalk.bold.green(path.basename(process.argv[1])) + " [options] file1.js file2.js ..."
].join("\n"));
if (callback)
callback(Error("usage"));
return 1;
}

Expand Down Expand Up @@ -65,7 +73,11 @@ exports.main = function(args) {
if (code) {
out = out.join('').replace(/\s*JSDoc \d+\.\d+\.\d+ [^$]+/, "");
process.stderr.write(out);
process.exit(code);
var err = Error("code " + code);
if (callback)
callback(err);
else
throw err;
return;
}

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

if (argv.out)
fs.writeFileSync(argv.out, output);
else
process.stdout.write(output, "utf8");
try {
if (argv.out)
fs.writeFileSync(argv.out, output);
else
process.stdout.write(output, "utf8");
} catch (err) {
if (callback)
callback(err);
else
throw err;
}
});

return undefined;
Expand Down
68 changes: 16 additions & 52 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

26 changes: 1 addition & 25 deletions dist/runtime/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/runtime/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/runtime/protobuf.min.js.map

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions scripts/gentests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var fs = require("fs"),
path = require("path"),
pbjs = require("../cli/pbjs");

[
"tests/data/package.proto",
"tests/data/rpc.proto"
]
.forEach(function(file) {
var out = file.replace(/\.proto$/, ".js");
pbjs.main([
"--target", "static-module",
"--wrap", "commonjs",
"--root", "test_" + path.basename(out, ".js"),
"--out", out,
file
], function(err) {
if (err)
throw err;
fs.writeFileSync(out, fs.readFileSync(out).toString("utf8").replace(/\"protobufjs\/runtime\"/, "\"../../runtime\""), "utf8");
})
});
1 change: 0 additions & 1 deletion src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var _TypeError = util._TypeError;
* @classdesc Runtime class providing the tools to create your own custom classes.
* @constructor
* @param {Type} type Reflected type
* @abstract
*/
function Class(type) {
return Class.create(type);
Expand Down
Loading

0 comments on commit fb74223

Please sign in to comment.