Skip to content

Commit

Permalink
Refactor cli to support multiple built-in wrappers, added named roots…
Browse files Browse the repository at this point in the history
… instead of always using global.root and added additionally necessary eslint comments, see #540
  • Loading branch information
dcodeIO committed Dec 11, 2016
1 parent 2cdc316 commit 691231f
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 45 deletions.
9 changes: 6 additions & 3 deletions cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ exports.main = function(args) {
target : "t",
out : "o",
path : "p",
wrap : "w"
wrap : "w",
root : "r"
},
string: [ "target", "out", "path", "wrap" ],
string: [ "target", "out", "path", "wrap", "root" ],
default: {
target: "json"
}
Expand All @@ -46,7 +47,9 @@ exports.main = function(args) {
"",
" -o, --out Saves to a file instead of writing to stdout.",
"",
" -w, --wrap Specifies an alternative wrapper for any *-module target.",
" -w, --wrap Specifies an alternative wrapper for *-module targets.",
"",
" -r, --root Specifies an alternative root name for *-module targets.",
"",
"usage: " + chalk.bold.green(path.basename(process.argv[1])) + " [options] file1.proto file2.json ..."
].join("\n"));
Expand Down
18 changes: 11 additions & 7 deletions cli/targets/json-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
module.exports = json_module;

var path = require("path"),
fs = require("fs");
fs = require("fs"),
util = require("../util");

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

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

function json_module(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).replace(/^(?!$)/mg, " ").trim()));
try {
var output = JSON.stringify(root, null, 2).replace(/^(?!$)/mg, " ").trim();
output = util.wrap(options.wrap || "json-module", output, options.root);
process.nextTick(function() {
callback(null, output);
});
} catch (e) {
callback(e);
}
}
28 changes: 13 additions & 15 deletions cli/targets/static-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,23 @@ module.exports = static_module_target;
// - AMD and global scope depend on the full library for now.

var path = require("path"),
fs = require("fs");
fs = require("fs"),
util = require("../util");

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

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

function static_module_target(root, options, callback) {
if (options.wrap)
options.wrap = path.resolve(process.cwd(), options.wrap);
else
options.wrap = path.join(__dirname, "static-module.tpl");
try {
var wrap = fs.readFileSync(options.wrap).toString("utf8");
require("./static")(root, options, function(err, output) {
if (err)
return callback(err);
callback(null, wrap.replace(/%OUTPUT%/, output.replace(/^(?!$)/mg, " ")));
});
} catch (err) {
callback(err);
}
require("./static")(root, options, function(err, output) {
if (err)
return callback(err);
try {
output = util.wrap(options.wrap || "static-module", output, options.root);
} catch (e) {
callback(e);
return;
}
callback(null, output);
});
}
2 changes: 1 addition & 1 deletion cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ function buildType(ref, type) {
]);
push(name(type.name) + ".decodeDelimited = function decodeDelimited(readerOrBuffer) {");
++indent;
push("readerOrBuffer = readerOrBuffer instanceof Reader ? readerOrBuffer : Reader(readerOrBuffer);");
push("readerOrBuffer = readerOrBuffer instanceof $protobuf.Reader ? readerOrBuffer : $protobuf.Reader(readerOrBuffer);");
push("return this.decode(readerOrBuffer, readerOrBuffer.uint32());");
--indent;
push("};");
Expand Down
18 changes: 18 additions & 0 deletions cli/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ exports.require = function(name, version) {
return require(name);
};

exports.wrap = function(name, OUTPUT, ROOT) {
if (!ROOT)
ROOT = "default";
var wrap;
try {
// try built-in wrappers first
wrap = fs.readFileSync(path.join(__dirname, "wrappers", name + ".js")).toString("utf8");
} catch (e) {
// otherwise fetch the custom one
wrap = fs.readFileSync(path.resolve(process.cwd(), name)).toString("utf8");
}
wrap = wrap.replace(/%ROOT%/g, JSON.stringify(ROOT));
wrap = wrap.replace(/( *)%OUTPUT%/, function($0, $1) {
return $1.length ? OUTPUT.replace(/^/mg, $1) : OUTPUT;
});
return wrap;
};

exports.pad = function(str, len, l) {
while (str.length < len)
str = l ? str + " " : " " + str;
Expand Down
11 changes: 6 additions & 5 deletions cli/targets/json-module.tpl → cli/wrappers/json-module.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
!(function(global, factory) {
(function(global, factory) {
/* eslint-disable no-undef */

/* 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);

/* eslint-enable no-undef */
})(this, function(protobuf) {
return protobuf.Root.fromJSON(%OUTPUT%);

var $root = protobuf.roots[%ROOT%] = protobuf.Root.fromJSON(%OUTPUT%);
return $root;
});
13 changes: 7 additions & 6 deletions cli/targets/static-module.tpl → cli/wrappers/static-module.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
!(function(global, factory) {
(function(global, factory) {
/* eslint-disable no-undef */

/* 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/runtime"));
/* Global */ else
global.root = factory(global.protobuf);

/* eslint-enable no-undef */
})(this, function($protobuf) {
"use strict";
"use strict"; // eslint-disable-line strict

%OUTPUT%

%OUTPUT%
$protobuf.roots[%ROOT%] = $root;

return $root;
});
13 changes: 10 additions & 3 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ function loadSync(filename, root) {

protobuf.loadSync = loadSync;

/**
* Named roots.
* @name roots
* @type {Object.<string,Root>}
*/
protobuf.roots = {};

// Parser
protobuf.tokenize = require("./tokenize");
protobuf.parse = require("./parse");
Expand Down
9 changes: 8 additions & 1 deletion types/protobuf.js.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* protobuf.js v6.1.0 TypeScript definitions
* Generated Sun, 11 Dec 2016 01:04:40 UTC
* Generated Sun, 11 Dec 2016 12:37:58 UTC
*/
declare module "protobufjs" {

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

/**
* Named roots.
* @name roots
* @type {Object.<string,Root>}
*/
var roots: { [k: string]: Root };

/**
* Reconfigures the library according to the environment.
* @returns {undefined}
Expand Down

0 comments on commit 691231f

Please sign in to comment.