Skip to content

Commit 0ce099b

Browse files
committed
Added json-module target to pbjs, renamed static to static-module, see #522
1 parent ebae1e1 commit 0ce099b

File tree

9 files changed

+57
-5
lines changed

9 files changed

+57
-5
lines changed

cli/pbjs.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,23 @@ exports.main = function(args) {
3030
paths = typeof argv.path === 'string' ? [ argv.path ] : argv.path || [];
3131

3232
if (!files.length) {
33+
var descriptions = Object.keys(targets).filter(function(key) { return !targets[key].private; }).map(function(key) {
34+
return " " + util.pad(key, 14, true) + targets[key].description;
35+
});
3336
console.log([
3437
"protobuf.js v" + pkg.version + " cli",
3538
"",
3639
"Consolidates imports and converts between file formats.",
3740
"",
38-
" -t, --target Specifies the target format. [" + Object.keys(targets).filter(function(key) { return !targets[key].private; }).join(', ') + "]",
39-
" Also accepts a path to require a custom target.",
41+
" -t, --target Specifies the target format. Also accepts a path to require a custom target.",
42+
"",
43+
descriptions.join('\n'),
4044
"",
4145
" -p, --path Adds a directory to the include path.",
4246
"",
4347
" -o, --out Saves to a file instead of writing to stdout.",
4448
"",
45-
" -w, --wrap Specifies an alternative wrapper for the static target.",
49+
" -w, --wrap Specifies an alternative wrapper for any *-module target.",
4650
"",
4751
"usage: " + chalk.bold.green(path.basename(process.argv[1])) + " [options] file1.proto file2.json ..."
4852
].join("\n"));

cli/targets/json-module.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
module.exports = json_modulee;
3+
4+
var path = require("path"),
5+
fs = require("fs");
6+
7+
var protobuf = require("../..");
8+
9+
json_modulee.description = "JSON representation as a module (AMD, CommonJS, global)"
10+
11+
function json_modulee(root, options, callback) {
12+
if (options.wrap)
13+
options.wrap = path.resolve(process.cwd(), options.wrap);
14+
else
15+
options.wrap = path.join(__dirname, "json-module.tpl");
16+
var wrap = fs.readFileSync(options.wrap).toString("utf8");
17+
callback(null, wrap.replace(/%OUTPUT%/, JSON.stringify(root, null, 2)));
18+
}

cli/targets/json-module.tpl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
!(function(global, factory) {
2+
3+
/* AMD */ if (typeof define === 'function' && define.amd)
4+
define(["protobuf"], factory);
5+
6+
/* CommonJS */ else if (typeof require === 'function' && typeof module === 'object' && module && module.exports)
7+
module.exports = factory(require("protobufjs"));
8+
9+
/* Global */ else
10+
global.root = factory(global.protobuf);
11+
12+
})(this, function(protobuf) { return protobuf.Root.fromJSON(
13+
14+
%OUTPUT%
15+
16+
);});

cli/targets/json.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module.exports = json_target;
33

44
var protobuf = require("../..");
55

6+
json_target.description = "JSON representation"
7+
68
function json_target(root, options, callback) {
79
callback(null, JSON.stringify(root, null, 2));
810
}

cli/targets/proto2.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module.exports = proto2_target;
33

44
var protobuf = require("../..");
55

6+
proto2_target.description = "Protocol Buffers, Version 2";
7+
68
function proto2_target(root, options, callback) {
79
require("./proto")(root, protobuf.util.merge(options, { syntax: "proto2" }), callback);
810
}

cli/targets/proto3.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module.exports = proto3_target;
33

44
var protobuf = require("../..");
55

6+
proto3_target.description = "Protocol Buffers, Version 3";
7+
68
function proto3_target(root, options, callback) {
79
require("./proto")(root, protobuf.util.merge(options, { syntax: "proto3" }), callback);
810
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ var Type = protobuf.Type,
2424
var out = [];
2525
var indent = 0;
2626

27+
static_target.description = "Static code without reflection as a module (AMD, CommonJS, global)";
28+
2729
function static_target(root, options, callback) {
2830
if (options.wrap)
2931
options.wrap = path.resolve(process.cwd(), options.wrap);
3032
else
31-
options.wrap = path.join(__dirname, "static.tpl");
33+
options.wrap = path.join(__dirname, "static-module.tpl");
3234
try {
3335
var wrap = fs.readFileSync(options.wrap).toString("utf8");
3436
++indent;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
;(function(global, factory) {
1+
!(function(global, factory) {
22

33
/* AMD */ if (typeof define === 'function' && define.amd)
44
define(["protobuf"], factory);

cli/util.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,9 @@ exports.require = function(name, version) {
6969
}
7070
return require(name);
7171
};
72+
73+
exports.pad = function(str, len, l) {
74+
while (str.length < len)
75+
str = l ? str + " " : " " + str;
76+
return str;
77+
};

0 commit comments

Comments
 (0)