Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. "use strict";
  2. var fs = require("fs"),
  3. path = require("path"),
  4. child_process = require("child_process");
  5. var semver;
  6. try {
  7. // installed as a peer dependency
  8. require.resolve("protobufjs");
  9. exports.pathToProtobufJs = "protobufjs";
  10. } catch (e) {
  11. // local development, i.e. forked from github
  12. exports.pathToProtobufJs = "..";
  13. }
  14. var protobuf = require(exports.pathToProtobufJs);
  15. function basenameCompare(a, b) {
  16. var aa = String(a).replace(/\.\w+$/, "").split(/(-?\d*\.?\d+)/g),
  17. bb = String(b).replace(/\.\w+$/, "").split(/(-?\d*\.?\d+)/g);
  18. for (var i = 0, k = Math.min(aa.length, bb.length); i < k; i++) {
  19. var x = parseFloat(aa[i]) || aa[i].toLowerCase(),
  20. y = parseFloat(bb[i]) || bb[i].toLowerCase();
  21. if (x < y)
  22. return -1;
  23. if (x > y)
  24. return 1;
  25. }
  26. return a.length < b.length ? -1 : 0;
  27. }
  28. exports.requireAll = function requireAll(dirname) {
  29. dirname = path.join(__dirname, dirname);
  30. var files = fs.readdirSync(dirname).sort(basenameCompare),
  31. all = {};
  32. files.forEach(function(file) {
  33. var basename = path.basename(file, ".js"),
  34. extname = path.extname(file);
  35. if (extname === ".js")
  36. all[basename] = require(path.join(dirname, file));
  37. });
  38. return all;
  39. };
  40. exports.traverse = function traverse(current, fn) {
  41. fn(current);
  42. if (current.fieldsArray)
  43. current.fieldsArray.forEach(function(field) {
  44. traverse(field, fn);
  45. });
  46. if (current.oneofsArray)
  47. current.oneofsArray.forEach(function(oneof) {
  48. traverse(oneof, fn);
  49. });
  50. if (current.methodsArray)
  51. current.methodsArray.forEach(function(method) {
  52. traverse(method, fn);
  53. });
  54. if (current.nestedArray)
  55. current.nestedArray.forEach(function(nested) {
  56. traverse(nested, fn);
  57. });
  58. };
  59. exports.traverseResolved = function traverseResolved(current, fn) {
  60. fn(current);
  61. if (current.resolvedType)
  62. traverseResolved(current.resolvedType, fn);
  63. if (current.resolvedKeyType)
  64. traverseResolved(current.resolvedKeyType, fn);
  65. if (current.resolvedRequestType)
  66. traverseResolved(current.resolvedRequestType, fn);
  67. if (current.resolvedResponseType)
  68. traverseResolved(current.resolvedResponseType, fn);
  69. };
  70. exports.inspect = function inspect(object, indent) {
  71. if (!object)
  72. return "";
  73. var chalk = require("chalk");
  74. var sb = [];
  75. if (!indent)
  76. indent = "";
  77. var ind = indent ? indent.substring(0, indent.length - 2) + "└ " : "";
  78. sb.push(
  79. ind + chalk.bold(object.toString()) + (object.visible ? " (visible)" : ""),
  80. indent + chalk.gray("parent: ") + object.parent
  81. );
  82. if (object instanceof protobuf.Field) {
  83. if (object.extend !== undefined)
  84. sb.push(indent + chalk.gray("extend: ") + object.extend);
  85. if (object.partOf)
  86. sb.push(indent + chalk.gray("oneof : ") + object.oneof);
  87. }
  88. sb.push("");
  89. if (object.fieldsArray)
  90. object.fieldsArray.forEach(function(field) {
  91. sb.push(inspect(field, indent + " "));
  92. });
  93. if (object.oneofsArray)
  94. object.oneofsArray.forEach(function(oneof) {
  95. sb.push(inspect(oneof, indent + " "));
  96. });
  97. if (object.methodsArray)
  98. object.methodsArray.forEach(function(service) {
  99. sb.push(inspect(service, indent + " "));
  100. });
  101. if (object.nestedArray)
  102. object.nestedArray.forEach(function(nested) {
  103. sb.push(inspect(nested, indent + " "));
  104. });
  105. return sb.join("\n");
  106. };
  107. function modExists(name, version) {
  108. for (var i = 0; i < module.paths.length; ++i) {
  109. try {
  110. var pkg = JSON.parse(fs.readFileSync(path.join(module.paths[i], name, "package.json")));
  111. return semver
  112. ? semver.satisfies(pkg.version, version)
  113. : parseInt(pkg.version, 10) === parseInt(version.replace(/^[\^~]/, ""), 10); // used for semver only
  114. } catch (e) {/**/}
  115. }
  116. return false;
  117. }
  118. function modInstall(install) {
  119. child_process.execSync("npm --silent install " + (typeof install === "string" ? install : install.join(" ")), {
  120. cwd: __dirname,
  121. stdio: "ignore"
  122. });
  123. }
  124. exports.setup = function() {
  125. var pkg = require(path.join(__dirname, "..", "package.json"));
  126. var version = pkg.dependencies["semver"] || pkg.devDependencies["semver"];
  127. if (!modExists("semver", version)) {
  128. process.stderr.write("installing semver@" + version + "\n");
  129. modInstall("semver@" + version);
  130. }
  131. semver = require("semver"); // used from now on for version comparison
  132. var install = [];
  133. pkg.cliDependencies.forEach(function(name) {
  134. if (name === "semver")
  135. return;
  136. version = pkg.dependencies[name] || pkg.devDependencies[name];
  137. if (!modExists(name, version)) {
  138. process.stderr.write("installing " + name + "@" + version + "\n");
  139. install.push(name + "@" + version);
  140. }
  141. });
  142. require("../scripts/postinstall"); // emit postinstall warning, if any
  143. if (!install.length)
  144. return;
  145. modInstall(install);
  146. };
  147. exports.wrap = function(OUTPUT, options) {
  148. var name = options.wrap || "default";
  149. var wrap;
  150. try {
  151. // try built-in wrappers first
  152. wrap = fs.readFileSync(path.join(__dirname, "wrappers", name + ".js")).toString("utf8");
  153. } catch (e) {
  154. // otherwise fetch the custom one
  155. wrap = fs.readFileSync(path.resolve(process.cwd(), name)).toString("utf8");
  156. }
  157. wrap = wrap.replace(/\$DEPENDENCY/g, JSON.stringify(options.dependency || "protobufjs"));
  158. wrap = wrap.replace(/( *)\$OUTPUT;/, function($0, $1) {
  159. return $1.length ? OUTPUT.replace(/^/mg, $1) : OUTPUT;
  160. });
  161. if (options.lint !== "")
  162. wrap = "/*" + options.lint + "*/\n" + wrap;
  163. return wrap.replace(/\r?\n/g, "\n");
  164. };
  165. exports.pad = function(str, len, l) {
  166. while (str.length < len)
  167. str = l ? str + " " : " " + str;
  168. return str;
  169. };