Skip to content

Commit 691231f

Browse files
committed
Refactor cli to support multiple built-in wrappers, added named roots instead of always using global.root and added additionally necessary eslint comments, see #540
1 parent 2cdc316 commit 691231f

14 files changed

+91
-45
lines changed

cli/pbjs.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ exports.main = function(args) {
1717
target : "t",
1818
out : "o",
1919
path : "p",
20-
wrap : "w"
20+
wrap : "w",
21+
root : "r"
2122
},
22-
string: [ "target", "out", "path", "wrap" ],
23+
string: [ "target", "out", "path", "wrap", "root" ],
2324
default: {
2425
target: "json"
2526
}
@@ -46,7 +47,9 @@ exports.main = function(args) {
4647
"",
4748
" -o, --out Saves to a file instead of writing to stdout.",
4849
"",
49-
" -w, --wrap Specifies an alternative wrapper for any *-module target.",
50+
" -w, --wrap Specifies an alternative wrapper for *-module targets.",
51+
"",
52+
" -r, --root Specifies an alternative root name for *-module targets.",
5053
"",
5154
"usage: " + chalk.bold.green(path.basename(process.argv[1])) + " [options] file1.proto file2.json ..."
5255
].join("\n"));

cli/targets/json-module.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
module.exports = json_module;
33

44
var path = require("path"),
5-
fs = require("fs");
5+
fs = require("fs"),
6+
util = require("../util");
67

78
var protobuf = require("../..");
89

910
json_module.description = "JSON representation as a module (AMD, CommonJS, global)"
1011

1112
function json_module(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).replace(/^(?!$)/mg, " ").trim()));
13+
try {
14+
var output = JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim();
15+
output = util.wrap(options.wrap || "json-module", output, options.root);
16+
process.nextTick(function() {
17+
callback(null, output);
18+
});
19+
} catch (e) {
20+
callback(e);
21+
}
1822
}

cli/targets/static-module.js

+13-15
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,23 @@ module.exports = static_module_target;
77
// - AMD and global scope depend on the full library for now.
88

99
var path = require("path"),
10-
fs = require("fs");
10+
fs = require("fs"),
11+
util = require("../util");
1112

1213
var protobuf = require("../..");
1314

1415
static_module_target.description = "Static code without reflection as a module (AMD, CommonJS, global)";
1516

1617
function static_module_target(root, options, callback) {
17-
if (options.wrap)
18-
options.wrap = path.resolve(process.cwd(), options.wrap);
19-
else
20-
options.wrap = path.join(__dirname, "static-module.tpl");
21-
try {
22-
var wrap = fs.readFileSync(options.wrap).toString("utf8");
23-
require("./static")(root, options, function(err, output) {
24-
if (err)
25-
return callback(err);
26-
callback(null, wrap.replace(/%OUTPUT%/, output.replace(/^(?!$)/mg, " ")));
27-
});
28-
} catch (err) {
29-
callback(err);
30-
}
18+
require("./static")(root, options, function(err, output) {
19+
if (err)
20+
return callback(err);
21+
try {
22+
output = util.wrap(options.wrap || "static-module", output, options.root);
23+
} catch (e) {
24+
callback(e);
25+
return;
26+
}
27+
callback(null, output);
28+
});
3129
}

cli/targets/static.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ function buildType(ref, type) {
273273
]);
274274
push(name(type.name) + ".decodeDelimited = function decodeDelimited(readerOrBuffer) {");
275275
++indent;
276-
push("readerOrBuffer = readerOrBuffer instanceof Reader ? readerOrBuffer : Reader(readerOrBuffer);");
276+
push("readerOrBuffer = readerOrBuffer instanceof $protobuf.Reader ? readerOrBuffer : $protobuf.Reader(readerOrBuffer);");
277277
push("return this.decode(readerOrBuffer, readerOrBuffer.uint32());");
278278
--indent;
279279
push("};");

cli/util.js

+18
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ exports.require = function(name, version) {
8484
return require(name);
8585
};
8686

87+
exports.wrap = function(name, OUTPUT, ROOT) {
88+
if (!ROOT)
89+
ROOT = "default";
90+
var wrap;
91+
try {
92+
// try built-in wrappers first
93+
wrap = fs.readFileSync(path.join(__dirname, "wrappers", name + ".js")).toString("utf8");
94+
} catch (e) {
95+
// otherwise fetch the custom one
96+
wrap = fs.readFileSync(path.resolve(process.cwd(), name)).toString("utf8");
97+
}
98+
wrap = wrap.replace(/%ROOT%/g, JSON.stringify(ROOT));
99+
wrap = wrap.replace(/( *)%OUTPUT%/, function($0, $1) {
100+
return $1.length ? OUTPUT.replace(/^/mg, $1) : OUTPUT;
101+
});
102+
return wrap;
103+
};
104+
87105
exports.pad = function(str, len, l) {
88106
while (str.length < len)
89107
str = l ? str + " " : " " + str;
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
!(function(global, factory) {
1+
(function(global, factory) {
2+
/* eslint-disable no-undef */
23

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

67
/* CommonJS */ else if (typeof require === 'function' && typeof module === 'object' && module && module.exports)
78
module.exports = factory(require("protobufjs"));
8-
9-
/* Global */ else
10-
global.root = factory(global.protobuf);
119

10+
/* eslint-enable no-undef */
1211
})(this, function(protobuf) {
13-
return protobuf.Root.fromJSON(%OUTPUT%);
12+
13+
var $root = protobuf.roots[%ROOT%] = protobuf.Root.fromJSON(%OUTPUT%);
14+
return $root;
1415
});
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
!(function(global, factory) {
1+
(function(global, factory) {
2+
/* eslint-disable no-undef */
23

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

67
/* CommonJS */ else if (typeof require === 'function' && typeof module === 'object' && module && module.exports)
78
module.exports = factory(require("protobufjs/runtime"));
8-
9-
/* Global */ else
10-
global.root = factory(global.protobuf);
119

10+
/* eslint-enable no-undef */
1211
})(this, function($protobuf) {
13-
"use strict";
12+
"use strict"; // eslint-disable-line strict
13+
14+
%OUTPUT%
1415

15-
%OUTPUT%
16+
$protobuf.roots[%ROOT%] = $root;
1617

1718
return $root;
1819
});

dist/protobuf.js

+10-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

8 Bytes
Binary file not shown.

dist/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ function loadSync(filename, root) {
6666

6767
protobuf.loadSync = loadSync;
6868

69+
/**
70+
* Named roots.
71+
* @name roots
72+
* @type {Object.<string,Root>}
73+
*/
74+
protobuf.roots = {};
75+
6976
// Parser
7077
protobuf.tokenize = require("./tokenize");
7178
protobuf.parse = require("./parse");

types/protobuf.js.d.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* protobuf.js v6.1.0 TypeScript definitions
3-
* Generated Sun, 11 Dec 2016 01:04:40 UTC
3+
* Generated Sun, 11 Dec 2016 12:37:58 UTC
44
*/
55
declare module "protobufjs" {
66

@@ -418,6 +418,13 @@ declare module "protobufjs" {
418418
*/
419419
function loadSync(filename: (string|string[]), root?: Root): Root;
420420

421+
/**
422+
* Named roots.
423+
* @name roots
424+
* @type {Object.<string,Root>}
425+
*/
426+
var roots: { [k: string]: Root };
427+
421428
/**
422429
* Reconfigures the library according to the environment.
423430
* @returns {undefined}

0 commit comments

Comments
 (0)