GSI - Employe Self Service Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

330 lines
12 KiB

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