Skip to content

Commit

Permalink
Added json-module target to pbjs, renamed static to static-module, see
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 7, 2016
1 parent ebae1e1 commit 0ce099b
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 5 deletions.
10 changes: 7 additions & 3 deletions cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
18 changes: 18 additions & 0 deletions cli/targets/json-module.js
Original file line number Diff line number Diff line change
@@ -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)));
}
16 changes: 16 additions & 0 deletions cli/targets/json-module.tpl
Original file line number Diff line number Diff line change
@@ -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%
);});
2 changes: 2 additions & 0 deletions cli/targets/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
2 changes: 2 additions & 0 deletions cli/targets/proto2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions cli/targets/proto3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 3 additions & 1 deletion cli/targets/static.js → cli/targets/static-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion cli/targets/static.tpl → cli/targets/static-module.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;(function(global, factory) {
!(function(global, factory) {
/* AMD */ if (typeof define === 'function' && define.amd)
define(["protobuf"], factory);
Expand Down
6 changes: 6 additions & 0 deletions cli/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit 0ce099b

Please sign in to comment.