From 0ce099bf4f4666fd00403a2839e6da628b8328a9 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Wed, 7 Dec 2016 12:05:41 +0100 Subject: [PATCH] Added json-module target to pbjs, renamed static to static-module, see #522 --- cli/pbjs.js | 10 +++++++--- cli/targets/json-module.js | 18 ++++++++++++++++++ cli/targets/json-module.tpl | 16 ++++++++++++++++ cli/targets/json.js | 2 ++ cli/targets/proto2.js | 2 ++ cli/targets/proto3.js | 2 ++ cli/targets/{static.js => static-module.js} | 4 +++- cli/targets/{static.tpl => static-module.tpl} | 2 +- cli/util.js | 6 ++++++ 9 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 cli/targets/json-module.js create mode 100644 cli/targets/json-module.tpl rename cli/targets/{static.js => static-module.js} (98%) rename cli/targets/{static.tpl => static-module.tpl} (96%) diff --git a/cli/pbjs.js b/cli/pbjs.js index 3f0864466..47f235184 100644 --- a/cli/pbjs.js +++ b/cli/pbjs.js @@ -30,19 +30,23 @@ exports.main = function(args) { paths = typeof argv.path === 'string' ? [ argv.path ] : argv.path || []; if (!files.length) { + var descriptions = Object.keys(targets).filter(function(key) { return !targets[key].private; }).map(function(key) { + return " " + util.pad(key, 14, true) + targets[key].description; + }); console.log([ "protobuf.js v" + pkg.version + " cli", "", "Consolidates imports and converts between file formats.", "", - " -t, --target Specifies the target format. [" + Object.keys(targets).filter(function(key) { return !targets[key].private; }).join(', ') + "]", - " Also accepts a path to require a custom target.", + " -t, --target Specifies the target format. Also accepts a path to require a custom target.", + "", + descriptions.join('\n'), "", " -p, --path Adds a directory to the include path.", "", " -o, --out Saves to a file instead of writing to stdout.", "", - " -w, --wrap Specifies an alternative wrapper for the static target.", + " -w, --wrap Specifies an alternative wrapper for any *-module target.", "", "usage: " + chalk.bold.green(path.basename(process.argv[1])) + " [options] file1.proto file2.json ..." ].join("\n")); diff --git a/cli/targets/json-module.js b/cli/targets/json-module.js new file mode 100644 index 000000000..9c25d5877 --- /dev/null +++ b/cli/targets/json-module.js @@ -0,0 +1,18 @@ +"use strict"; +module.exports = json_modulee; + +var path = require("path"), + fs = require("fs"); + +var protobuf = require("../.."); + +json_modulee.description = "JSON representation as a module (AMD, CommonJS, global)" + +function json_modulee(root, options, callback) { + if (options.wrap) + options.wrap = path.resolve(process.cwd(), options.wrap); + else + options.wrap = path.join(__dirname, "json-module.tpl"); + var wrap = fs.readFileSync(options.wrap).toString("utf8"); + callback(null, wrap.replace(/%OUTPUT%/, JSON.stringify(root, null, 2))); +} diff --git a/cli/targets/json-module.tpl b/cli/targets/json-module.tpl new file mode 100644 index 000000000..4d35719ea --- /dev/null +++ b/cli/targets/json-module.tpl @@ -0,0 +1,16 @@ +!(function(global, factory) { + + /* AMD */ if (typeof define === 'function' && define.amd) + define(["protobuf"], factory); + + /* CommonJS */ else if (typeof require === 'function' && typeof module === 'object' && module && module.exports) + module.exports = factory(require("protobufjs")); + + /* Global */ else + global.root = factory(global.protobuf); + +})(this, function(protobuf) { return protobuf.Root.fromJSON( + +%OUTPUT% + +);}); diff --git a/cli/targets/json.js b/cli/targets/json.js index 722390106..75b7a0a87 100644 --- a/cli/targets/json.js +++ b/cli/targets/json.js @@ -3,6 +3,8 @@ module.exports = json_target; var protobuf = require("../.."); +json_target.description = "JSON representation" + function json_target(root, options, callback) { callback(null, JSON.stringify(root, null, 2)); } diff --git a/cli/targets/proto2.js b/cli/targets/proto2.js index c5fcc3217..09521e061 100644 --- a/cli/targets/proto2.js +++ b/cli/targets/proto2.js @@ -3,6 +3,8 @@ module.exports = proto2_target; var protobuf = require("../.."); +proto2_target.description = "Protocol Buffers, Version 2"; + function proto2_target(root, options, callback) { require("./proto")(root, protobuf.util.merge(options, { syntax: "proto2" }), callback); } diff --git a/cli/targets/proto3.js b/cli/targets/proto3.js index 40dacfe76..661c916bd 100644 --- a/cli/targets/proto3.js +++ b/cli/targets/proto3.js @@ -3,6 +3,8 @@ module.exports = proto3_target; var protobuf = require("../.."); +proto3_target.description = "Protocol Buffers, Version 3"; + function proto3_target(root, options, callback) { require("./proto")(root, protobuf.util.merge(options, { syntax: "proto3" }), callback); } diff --git a/cli/targets/static.js b/cli/targets/static-module.js similarity index 98% rename from cli/targets/static.js rename to cli/targets/static-module.js index afae7930d..46eaf2631 100644 --- a/cli/targets/static.js +++ b/cli/targets/static-module.js @@ -24,11 +24,13 @@ var Type = protobuf.Type, var out = []; var indent = 0; +static_target.description = "Static code without reflection as a module (AMD, CommonJS, global)"; + function static_target(root, options, callback) { if (options.wrap) options.wrap = path.resolve(process.cwd(), options.wrap); else - options.wrap = path.join(__dirname, "static.tpl"); + options.wrap = path.join(__dirname, "static-module.tpl"); try { var wrap = fs.readFileSync(options.wrap).toString("utf8"); ++indent; diff --git a/cli/targets/static.tpl b/cli/targets/static-module.tpl similarity index 96% rename from cli/targets/static.tpl rename to cli/targets/static-module.tpl index e5ea05044..e78d6c5c0 100644 --- a/cli/targets/static.tpl +++ b/cli/targets/static-module.tpl @@ -1,4 +1,4 @@ -;(function(global, factory) { +!(function(global, factory) { /* AMD */ if (typeof define === 'function' && define.amd) define(["protobuf"], factory); diff --git a/cli/util.js b/cli/util.js index c45dcc7fa..f8619e791 100644 --- a/cli/util.js +++ b/cli/util.js @@ -69,3 +69,9 @@ exports.require = function(name, version) { } return require(name); }; + +exports.pad = function(str, len, l) { + while (str.length < len) + str = l ? str + " " : " " + str; + return str; +};