Skip to content

Commit

Permalink
CLI: Added customizable linter configuration to pbjs; CLI: Added stdi…
Browse files Browse the repository at this point in the history
…n support to pbjs and pbts
  • Loading branch information
dcodeIO committed Jan 2, 2017
1 parent fde56c0 commit 9681854
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 116 deletions.
104 changes: 70 additions & 34 deletions cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ var protobuf = require(".."),
* @returns {number|undefined} Exit code, if known
*/
exports.main = function(args, callback) {
var lintDefault = "eslint-disable block-scoped-var, no-redeclare, no-control-regex";
var argv = minimist(args, {
alias: {
target : "t",
out : "o",
path : "p",
wrap : "w",
root : "r"
root : "r",
lint : "l"
},
string: [ "target", "out", "path", "wrap", "root" ],
string: [ "target", "out", "path", "wrap", "root", "lint" ],
boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments" ],
default: {
target : "json",
Expand All @@ -36,7 +38,8 @@ exports.main = function(args, callback) {
convert : true,
delimited : true,
beautify : true,
comments : true
comments : true,
lint : lintDefault
}
});

Expand All @@ -52,9 +55,9 @@ exports.main = function(args, callback) {
callback(Error("usage"));
else
console.error([
"protobuf.js v" + pkg.version + " cli",
"protobuf.js v" + pkg.version + " CLI for JavaScript",
"",
"Consolidates imports and converts between file formats.",
chalk.bold.white("Consolidates imports and converts between file formats."),
"",
" -t, --target Specifies the target format. Also accepts a path to require a custom target.",
"",
Expand All @@ -64,21 +67,26 @@ exports.main = function(args, callback) {
"",
" -o, --out Saves to a file instead of writing to stdout.",
"",
" Module targets only:",
chalk.bold.gray(" Module targets only:"),
"",
" -w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.",
"",
" default Default wrapper supporting both CommonJS and AMD",
" commonjs CommonJS only wrapper",
" amd AMD only wrapper",
" commonjs CommonJS wrapper",
" amd AMD wrapper",
" es6 ES6 wrapper",
"",
" -r, --root Specifies an alternative protobuf.roots name.",
"",
" Proto sources only:",
" -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:",
"",
" " + lintDefault,
"",
chalk.bold.gray(" Proto sources only:"),
"",
" --keep-case Keeps field casing instead of converting to camel case (not recommended).",
"",
" Static targets only:",
chalk.bold.gray(" Static targets only:"),
"",
" --no-create Does not generate create functions used for runtime compatibility.",
" --no-encode Does not generate encode functions.",
Expand All @@ -89,7 +97,7 @@ exports.main = function(args, callback) {
" --no-beautify Does not beautify generated code.",
" --no-comments Does not output any JSDoc comments.",
"",
"usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..."
"usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or) ") + "other | " + chalk.bold.green("pbjs") + " [options] -"
].join("\n"));
return 1;
}
Expand Down Expand Up @@ -127,30 +135,58 @@ exports.main = function(args, callback) {
"keepCase": argv["keep-case"] || false
};

try {
root.loadSync(files, parseOptions); // sync is deterministic while async is not
} catch (err) {
if (callback) {
callback(err);
return undefined;
}
throw err;
}
// Read from stdin
if (files.length === 1 && files[0] === "-") {
var data = [];
process.stdin.on("data", function(chunk) {
data.push(chunk);
});
process.stdin.on("end", function() {
var source = Buffer.concat(data).toString("utf8");
if (source.charAt(0) !== "{") {
protobuf.parse(source, root, parseOptions);
} else {
var json = JSON.parse(source);
root.setOptions(json.options).addJSON(json);
}
callTarget();
});

target(root, argv, function targetCallback(err, output) {
if (err) {
if (callback)
return callback(err);
// Load from disk
} else {
try {
root.loadSync(files, parseOptions); // sync is deterministic while async is not
callTarget();
} catch (err) {
if (callback) {
callback(err);
return undefined;
}
throw err;
}
if (output !== "") {
if (argv.out)
fs.writeFileSync(argv.out, output, { encoding: "utf8" });
else
process.stdout.write(output, "utf8");
}
return callback
? callback(null)
: undefined;
});
}

function callTarget() {
target(root, argv, function targetCallback(err, output) {
if (err) {
if (callback)
return callback(err);
throw err;
}
if (output !== "") {
output = [
"// $> pbjs " + args.join(" "),
"// Generated " + (new Date()).toUTCString().replace(/GMT/, "UTC"),
""
].join("\n") + "\n" + output;
if (argv.out)
fs.writeFileSync(argv.out, output, { encoding: "utf8" });
else
process.stdout.write(output, "utf8");
}
return callback
? callback(null)
: undefined;
});
}
};
146 changes: 87 additions & 59 deletions cli/pbts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var child_process = require("child_process");

var minimist = util.require("minimist", pkg.devDependencies.minimist),
chalk = util.require("chalk", pkg.devDependencies.chalk),
glob = util.require("glob", pkg.devDependencies.glob);
glob = util.require("glob", pkg.devDependencies.glob),
tmp = util.require("tmp", pkg.devDependencies.glob);

var jsdoc = util.require("jsdoc/package.json", pkg.devDependencies.jsdoc);

Expand Down Expand Up @@ -41,9 +42,9 @@ exports.main = function(args, callback) {
callback(Error("usage"));
else
console.error([
"protobuf.js v" + pkg.version + " cli for TypeScript",
"protobuf.js v" + pkg.version + " CLI for TypeScript",
"",
"Generates TypeScript definitions from annotated JavaScript files.",
chalk.bold.white("Generates TypeScript definitions from annotated JavaScript files."),
"",
" -n, --name Wraps everything in a module of the specified name.",
"",
Expand All @@ -55,7 +56,7 @@ exports.main = function(args, callback) {
"",
" --no-comments Does not output any JSDoc comments.",
"",
"usage: " + chalk.bold.green("pbts") + " [options] file1.js file2.js ..."
"usage: " + chalk.bold.green("pbts") + " [options] file1.js file2.js ..." + chalk.bold.gray(" (or) ") + "other | " + chalk.bold.green("pbts") + " [options] -"
].join("\n"));
if (callback)
callback(Error("usage"));
Expand All @@ -72,63 +73,90 @@ exports.main = function(args, callback) {
++i;
}

// There is no proper API for jsdoc, so this executes the CLI and pipes the output
var basedir = path.join(__dirname, "..");
var moduleName = argv.name || "null";
var child = child_process.exec("node \"" + basedir + "/node_modules/jsdoc/jsdoc.js\" -c \"" + basedir + "/jsdoc.types.json\" -q \"module=" + encodeURIComponent(moduleName) + "&comments=" + Boolean(argv.comments) + "\" " + files.map(function(file) { return '"' + file + '"'; }).join(' '), {
cwd: process.cwd(),
argv0: "node",
stdio: "pipe",
maxBuffer: 1 << 24 // 16mb
});
var out = [];
child.stdout.on("data", function(data) {
out.push(data);
});
child.stderr.pipe(process.stderr);
child.on("close", function(code) {
if (code) {
out = out.join('').replace(/\s*JSDoc \d+\.\d+\.\d+ [^$]+/, "");
process.stderr.write(out);
var err = Error("code " + code);
if (callback)
callback(err);
else
throw err;
return;
}
var cleanup = [];

var output = [
"// $> pbts " + args.join(" "),
"// Generated " + (new Date()).toUTCString().replace(/GMT/, "UTC"),
""
];
if (argv.global)
output.push(
"export as namespace " + argv.global + ";",
""
);
if (!argv.main)
output.push(
"import * as $protobuf from \"protobufjs\";",
// Read from stdin (to a temporary file)
if (files.length === 1 && files[0] === "-") {
var data = [];
process.stdin.on("data", function(chunk) {
data.push(chunk);
});
process.stdin.on("end", function() {
files[0] = tmp.tmpNameSync() + ".js";
fs.writeFileSync(files[0], Buffer.concat(data));
cleanup.push(files[0]);
callJsdoc();
});

// Load from disk
} else {
callJsdoc();
}

function callJsdoc() {

// There is no proper API for jsdoc, so this executes the CLI and pipes the output
var basedir = path.join(__dirname, "..");
var moduleName = argv.name || "null";
var cmd = "node \"" + basedir + "/node_modules/jsdoc/jsdoc.js\" -c \"" + basedir + "/jsdoc.types.json\" -q \"module=" + encodeURIComponent(moduleName) + "&comments=" + Boolean(argv.comments) + "\" " + files.map(function(file) { return '"' + file + '"'; }).join(' ');
var child = child_process.exec(cmd, {
cwd: process.cwd(),
argv0: "node",
stdio: "pipe",
maxBuffer: 1 << 24 // 16mb
});
var out = [];
child.stdout.on("data", function(data) {
out.push(data);
});
child.stderr.pipe(process.stderr);
child.on("close", function(code) {
// clean up temporary files, no matter what
try { cleanup.forEach(fs.unlinkSync); } catch(e) {} cleanup = [];

if (code) {
out = out.join('').replace(/\s*JSDoc \d+\.\d+\.\d+ [^$]+/, "");
process.stderr.write(out);
var err = Error("code " + code);
if (callback)
callback(err);
else
throw err;
return;
}

var output = [
"// $> pbts " + args.join(" "),
"// Generated " + (new Date()).toUTCString().replace(/GMT/, "UTC"),
""
);
output = output.join('\n') + "\n" + out.join('');

try {
if (argv.out)
fs.writeFileSync(argv.out, output);
else
process.stdout.write(output, "utf8");
if (callback)
callback(null);
} catch (err) {
if (callback)
callback(err);
else
throw err;
}
});
];
if (argv.global)
output.push(
"export as namespace " + argv.global + ";",
""
);
if (!argv.main)
output.push(
"import * as $protobuf from \"protobufjs\";",
""
);
output = output.join('\n') + "\n" + out.join('');

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

return undefined;
};
2 changes: 1 addition & 1 deletion cli/targets/json-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ json_module.description = "JSON representation as a module"
function json_module(root, options, callback) {
try {
var output = "var $root = protobuf.Root.fromJSON(" + JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim() + ").resolveAll();";
output = util.wrap(options.wrap || "default", output, options.root);
output = util.wrap(output, options);
process.nextTick(function() {
callback(null, output);
});
Expand Down
2 changes: 1 addition & 1 deletion cli/targets/static-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function static_module_target(root, options, callback) {
if (err)
return callback(err);
try {
output = util.wrap(options.wrap || "default", output, options.root);
output = util.wrap(output, options);
} catch (e) {
callback(e);
return;
Expand Down
4 changes: 2 additions & 2 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ function buildService(ref, service) {
]);
push("this.responseDelimited = Boolean(responseDelimited);");
--indent;
push("};");
push("}");

service.methodsArray.forEach(function(method) {
method.resolve();
Expand Down Expand Up @@ -567,7 +567,7 @@ function buildService(ref, service) {
--indent;
push("} catch (err) {");
++indent;
push("(typeof setImmediate === 'function' ? setImmediate : setTimeout)(function() { callback(err); });");
push("(typeof setImmediate === \"function\" ? setImmediate : setTimeout)(function() { callback(err); });");
push("return;");
--indent;
push("}");
Expand Down
Loading

0 comments on commit 9681854

Please sign in to comment.