Permalink
Browse files

Simplified programmatic CLI api; Other minor optimizations

  • Loading branch information...
dcodeIO committed Dec 15, 2016
1 parent 9c76950 commit fb74223b7273530d8baa53437ee96c65a387436d
@@ -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);
@@ -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);
@@ -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",
@@ -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;
}
@@ -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);
});
});
};
@@ -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"
@@ -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;
}
@@ -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;
}
@@ -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;

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN -52 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN -44 Bytes (99%) dist/runtime/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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");
})
});
@@ -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);
Oops, something went wrong.

0 comments on commit fb74223

Please sign in to comment.