mirror of
https://github.com/protobufjs/protobuf.js.git
synced 2025-12-08 20:58:55 +00:00
119 lines
4.0 KiB
JavaScript
119 lines
4.0 KiB
JavaScript
var fs = require("fs"),
|
|
path = require("path"),
|
|
child_process = require("child_process");
|
|
|
|
var protobuf = require("..");
|
|
|
|
function basenameCompare(a, b) {
|
|
var aa = String(a).replace(/\.\w+$/, "").split(/(-?\d*\.?\d+)/g),
|
|
bb = String(b).replace(/\.\w+$/, "").split(/(-?\d*\.?\d+)/g);
|
|
for (var i = 0, k = Math.min(aa.length, bb.length); i < k; i++) {
|
|
var x = parseFloat(aa[i]) || aa[i].toLowerCase(),
|
|
y = parseFloat(bb[i]) || bb[i].toLowerCase();
|
|
if (x < y)
|
|
return -1;
|
|
if (x > y)
|
|
return 1;
|
|
}
|
|
return a.length < b.length ? -1 : 0;
|
|
};
|
|
|
|
exports.requireAll = function requireAll(dirname) {
|
|
dirname = path.join(__dirname, dirname);
|
|
var files = fs.readdirSync(dirname).sort(basenameCompare),
|
|
all = {};
|
|
files.forEach(function(file) {
|
|
var basename = path.basename(file, ".js"),
|
|
extname = path.extname(file);
|
|
if (extname === ".js")
|
|
all[basename] = require(path.join(dirname, file));
|
|
});
|
|
return all;
|
|
};
|
|
|
|
exports.inspect = function inspect(object, indent) {
|
|
if (!object)
|
|
return "";
|
|
var chalk = require("chalk");
|
|
var sb = [];
|
|
if (!indent)
|
|
indent = "";
|
|
var ind = indent ? indent.substring(0, indent.length - 2) + "└ " : "";
|
|
sb.push(
|
|
ind + chalk.bold(object.toString()) + (object.visible ? " (visible)" : ""),
|
|
indent + chalk.gray("parent: ") + object.parent
|
|
);
|
|
if (object instanceof protobuf.Field) {
|
|
if (object.extend !== undefined)
|
|
sb.push(indent + chalk.gray("extend: ") + object.extend);
|
|
if (object.partOf)
|
|
sb.push(indent + chalk.gray("oneof : ") + object.oneof);
|
|
}
|
|
sb.push("");
|
|
if (object.fieldsArray)
|
|
object.fieldsArray.forEach(function(field) {
|
|
sb.push(inspect(field, indent + " "));
|
|
});
|
|
if (object.oneofsArray)
|
|
object.oneofsArray.forEach(function(oneof) {
|
|
sb.push(inspect(oneof, indent + " "));
|
|
});
|
|
if (object.methodsArray)
|
|
object.methodsArray.forEach(function(service) {
|
|
sb.push(inspect(service, indent + " "));
|
|
});
|
|
if (object.nestedArray)
|
|
object.nestedArray.forEach(function(nested) {
|
|
sb.push(inspect(nested, indent + " "));
|
|
});
|
|
return sb.join("\n");
|
|
};
|
|
|
|
exports.require = function(name, version) {
|
|
var sub = "";
|
|
var p = name.indexOf("/");
|
|
if (p > -1) {
|
|
sub = name.substring(p);
|
|
name = name.substring(0, p);
|
|
}
|
|
var cwd = path.join(__dirname, "..");
|
|
var dir = path.join(cwd, "node_modules", name);
|
|
try {
|
|
// do not feed the cache
|
|
require.resolve(path.join(dir, "package.json"));
|
|
} catch (e) {
|
|
console.error("installing " + name + "@" + version + " ...");
|
|
child_process.execSync("npm install " + name + "@" + version, {
|
|
cwd: cwd
|
|
});
|
|
}
|
|
return require(name + sub);
|
|
};
|
|
|
|
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;
|
|
return str;
|
|
};
|
|
|
|
exports.reserved = function(name) {
|
|
return /^(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$/.test(name);
|
|
}; |