Permalink
Browse files

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
  • Loading branch information...
dcodeIO committed Dec 11, 2016
1 parent 2cdc316 commit 691231fbc453a243f48a97bfb86794ab5718ef49
@@ -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"
}
@@ -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"));
@@ -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);
}
}
@@ -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);
});
}
@@ -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("};");
@@ -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;
@@ -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;
});
@@ -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;
});

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +8 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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");
@@ -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" {
@@ -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}

0 comments on commit 691231f

Please sign in to comment.