pbjs.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. "use strict";
  2. var path = require("path"),
  3. fs = require("fs"),
  4. pkg = require("./package.json"),
  5. util = require("./util");
  6. util.setup();
  7. var protobuf = require(util.pathToProtobufJs),
  8. minimist = require("minimist"),
  9. chalk = require("chalk"),
  10. glob = require("glob");
  11. var targets = util.requireAll("./targets");
  12. /**
  13. * Runs pbjs programmatically.
  14. * @param {string[]} args Command line arguments
  15. * @param {function(?Error, string=)} [callback] Optional completion callback
  16. * @returns {number|undefined} Exit code, if known
  17. */
  18. exports.main = function main(args, callback) {
  19. var lintDefault = "eslint-disable " + [
  20. "block-scoped-var",
  21. "id-length",
  22. "no-control-regex",
  23. "no-magic-numbers",
  24. "no-prototype-builtins",
  25. "no-redeclare",
  26. "no-shadow",
  27. "no-var",
  28. "sort-vars"
  29. ].join(", ");
  30. var argv = minimist(args, {
  31. alias: {
  32. target: "t",
  33. out: "o",
  34. path: "p",
  35. wrap: "w",
  36. root: "r",
  37. lint: "l",
  38. // backward compatibility:
  39. "force-long": "strict-long",
  40. "force-message": "strict-message"
  41. },
  42. string: [ "target", "out", "path", "wrap", "dependency", "root", "lint" ],
  43. boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message" ],
  44. default: {
  45. target: "json",
  46. create: true,
  47. encode: true,
  48. decode: true,
  49. verify: true,
  50. convert: true,
  51. delimited: true,
  52. beautify: true,
  53. comments: true,
  54. es6: null,
  55. lint: lintDefault,
  56. "keep-case": false,
  57. "force-long": false,
  58. "force-number": false,
  59. "force-enum-string": false,
  60. "force-message": false
  61. }
  62. });
  63. var target = targets[argv.target],
  64. files = argv._,
  65. paths = typeof argv.path === "string" ? [ argv.path ] : argv.path || [];
  66. // alias hyphen args in camel case
  67. Object.keys(argv).forEach(function(key) {
  68. var camelKey = key.replace(/-([a-z])/g, function($0, $1) { return $1.toUpperCase(); });
  69. if (camelKey !== key)
  70. argv[camelKey] = argv[key];
  71. });
  72. // protobuf.js package directory contains additional, otherwise non-bundled google types
  73. paths.push(path.relative(process.cwd(), path.join(__dirname, "..")) || ".");
  74. if (!files.length) {
  75. var descs = Object.keys(targets).filter(function(key) { return !targets[key].private; }).map(function(key) {
  76. return " " + util.pad(key, 14, true) + targets[key].description;
  77. });
  78. if (callback)
  79. callback(Error("usage")); // eslint-disable-line callback-return
  80. else
  81. process.stderr.write([
  82. "protobuf.js v" + pkg.version + " CLI for JavaScript",
  83. "",
  84. chalk.bold.white("Translates between file formats and generates static code."),
  85. "",
  86. " -t, --target Specifies the target format. Also accepts a path to require a custom target.",
  87. "",
  88. descs.join("\n"),
  89. "",
  90. " -p, --path Adds a directory to the include path.",
  91. "",
  92. " -o, --out Saves to a file instead of writing to stdout.",
  93. "",
  94. " --sparse Exports only those types referenced from a main file (experimental).",
  95. "",
  96. chalk.bold.gray(" Module targets only:"),
  97. "",
  98. " -w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.",
  99. "",
  100. " default Default wrapper supporting both CommonJS and AMD",
  101. " commonjs CommonJS wrapper",
  102. " amd AMD wrapper",
  103. " es6 ES6 wrapper (implies --es6)",
  104. " closure A closure adding to protobuf.roots where protobuf is a global",
  105. "",
  106. " --dependency Specifies which version of protobuf to require. Accepts any valid module id",
  107. "",
  108. " -r, --root Specifies an alternative protobuf.roots name.",
  109. "",
  110. " -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:",
  111. "",
  112. " " + lintDefault,
  113. "",
  114. " --es6 Enables ES6 syntax (const/let instead of var)",
  115. "",
  116. chalk.bold.gray(" Proto sources only:"),
  117. "",
  118. " --keep-case Keeps field casing instead of converting to camel case.",
  119. "",
  120. chalk.bold.gray(" Static targets only:"),
  121. "",
  122. " --no-create Does not generate create functions used for reflection compatibility.",
  123. " --no-encode Does not generate encode functions.",
  124. " --no-decode Does not generate decode functions.",
  125. " --no-verify Does not generate verify functions.",
  126. " --no-convert Does not generate convert functions like from/toObject",
  127. " --no-delimited Does not generate delimited encode/decode functions.",
  128. " --no-beautify Does not beautify generated code.",
  129. " --no-comments Does not output any JSDoc comments.",
  130. "",
  131. " --force-long Enfores the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.",
  132. " --force-number Enfores the use of 'number' for s-/u-/int64 and s-/fixed64 fields.",
  133. " --force-message Enfores the use of message instances instead of plain objects.",
  134. "",
  135. "usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or pipe) ") + "other | " + chalk.bold.green("pbjs") + " [options] -",
  136. ""
  137. ].join("\n"));
  138. return 1;
  139. }
  140. if (typeof argv["strict-long"] === "boolean")
  141. argv["force-long"] = argv["strict-long"];
  142. // Resolve glob expressions
  143. for (var i = 0; i < files.length;) {
  144. if (glob.hasMagic(files[i])) {
  145. var matches = glob.sync(files[i]);
  146. Array.prototype.splice.apply(files, [i, 1].concat(matches));
  147. i += matches.length;
  148. } else
  149. ++i;
  150. }
  151. // Require custom target
  152. if (!target)
  153. target = require(path.resolve(process.cwd(), argv.target));
  154. var root = new protobuf.Root();
  155. var mainFiles = [];
  156. // Search include paths when resolving imports
  157. root.resolvePath = function pbjsResolvePath(origin, target) {
  158. var normOrigin = protobuf.util.path.normalize(origin),
  159. normTarget = protobuf.util.path.normalize(target);
  160. if (!normOrigin)
  161. mainFiles.push(normTarget);
  162. var resolved = protobuf.util.path.resolve(normOrigin, normTarget, true);
  163. var idx = resolved.lastIndexOf("google/protobuf/");
  164. if (idx > -1) {
  165. var altname = resolved.substring(idx);
  166. if (altname in protobuf.common)
  167. resolved = altname;
  168. }
  169. if (fs.existsSync(resolved))
  170. return resolved;
  171. for (var i = 0; i < paths.length; ++i) {
  172. var iresolved = protobuf.util.path.resolve(paths[i] + "/", target);
  173. if (fs.existsSync(iresolved))
  174. return iresolved;
  175. }
  176. return resolved;
  177. };
  178. // Use es6 syntax if not explicitly specified on the command line and the es6 wrapper is used
  179. if (argv.wrap === "es6" || argv.es6) {
  180. argv.wrap = "es6";
  181. argv.es6 = true;
  182. }
  183. var parseOptions = {
  184. "keepCase": argv["keep-case"] || false
  185. };
  186. // Read from stdin
  187. if (files.length === 1 && files[0] === "-") {
  188. var data = [];
  189. process.stdin.on("data", function(chunk) {
  190. data.push(chunk);
  191. });
  192. process.stdin.on("end", function() {
  193. var source = Buffer.concat(data).toString("utf8");
  194. try {
  195. if (source.charAt(0) !== "{") {
  196. protobuf.parse.filename = "-";
  197. protobuf.parse(source, root, parseOptions);
  198. } else {
  199. var json = JSON.parse(source);
  200. root.setOptions(json.options).addJSON(json);
  201. }
  202. callTarget();
  203. } catch (err) {
  204. if (callback) {
  205. callback(err);
  206. return;
  207. }
  208. throw err;
  209. }
  210. });
  211. // Load from disk
  212. } else {
  213. try {
  214. root.loadSync(files, parseOptions).resolveAll(); // sync is deterministic while async is not
  215. if (argv.sparse)
  216. sparsify(root);
  217. callTarget();
  218. } catch (err) {
  219. if (callback) {
  220. callback(err);
  221. return undefined;
  222. }
  223. throw err;
  224. }
  225. }
  226. function markReferenced(tobj) {
  227. tobj.referenced = true;
  228. // also mark a type's fields and oneofs
  229. if (tobj.fieldsArray)
  230. tobj.fieldsArray.forEach(function(fobj) {
  231. fobj.referenced = true;
  232. });
  233. if (tobj.oneofsArray)
  234. tobj.oneofsArray.forEach(function(oobj) {
  235. oobj.referenced = true;
  236. });
  237. // also mark an extension field's extended type, but not its (other) fields
  238. if (tobj.extensionField)
  239. tobj.extensionField.parent.referenced = true;
  240. }
  241. function sparsify(root) {
  242. // 1. mark directly or indirectly referenced objects
  243. util.traverse(root, function(obj) {
  244. if (!obj.filename)
  245. return;
  246. if (mainFiles.indexOf(obj.filename) > -1)
  247. util.traverseResolved(obj, markReferenced);
  248. });
  249. // 2. empty unreferenced objects
  250. util.traverse(root, function(obj) {
  251. var parent = obj.parent;
  252. if (!parent || obj.referenced) // root or referenced
  253. return;
  254. // remove unreferenced namespaces
  255. if (obj instanceof protobuf.Namespace) {
  256. var hasReferenced = false;
  257. util.traverse(obj, function(iobj) {
  258. if (iobj.referenced)
  259. hasReferenced = true;
  260. });
  261. if (hasReferenced) { // replace with plain namespace if a namespace subclass
  262. if (obj instanceof protobuf.Type || obj instanceof protobuf.Service) {
  263. var robj = new protobuf.Namespace(obj.name, obj.options);
  264. robj.nested = obj.nested;
  265. parent.add(robj);
  266. }
  267. } else // remove completely if nothing inside is referenced
  268. parent.remove(obj);
  269. // remove everything else unreferenced
  270. } else if (!(obj instanceof protobuf.Namespace))
  271. parent.remove(obj);
  272. });
  273. // 3. validate that everything is fine
  274. root.resolveAll();
  275. }
  276. function callTarget() {
  277. target(root, argv, function targetCallback(err, output) {
  278. if (err) {
  279. if (callback)
  280. return callback(err);
  281. throw err;
  282. }
  283. try {
  284. if (argv.out)
  285. fs.writeFileSync(argv.out, output, { encoding: "utf8" });
  286. else if (!callback)
  287. process.stdout.write(output, "utf8");
  288. return callback
  289. ? callback(null, output)
  290. : undefined;
  291. } catch (err) {
  292. if (callback)
  293. return callback(err);
  294. throw err;
  295. }
  296. });
  297. }
  298. return undefined;
  299. };