lib.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. var plist = require("plist");
  2. var util = require("util");
  3. var url = require("url");
  4. var cson = require("cson");
  5. var https = require("https");
  6. var http = require("http");
  7. exports.parsePlist = function(xmlOrJSON, callback) {
  8. var json;
  9. if (xmlOrJSON[0] == "<") {
  10. plist.parseString(xmlOrJSON, function(_, result) {
  11. json = result[0];
  12. });
  13. } else {
  14. try {
  15. xmlOrJSON = xmlOrJSON.replace(
  16. /("(?:\\.|[^"])*")|(?:,\s*)+([\]\}])|(\w+)\s*:|([\]\}]\s*[\[\{])|(\/\/.*|\/\*(?:[^\*]|\*(?=[^\/]))*?\*\/)/g,
  17. function(_, str, extraComma, noQuote, missingComma, comment) {
  18. if (comment)
  19. return "";
  20. if (missingComma)
  21. return missingComma[0] + "," + missingComma.slice(1);
  22. return str || extraComma || '"' + noQuote + '":';
  23. });
  24. json = JSON.parse(xmlOrJSON);
  25. } catch(e) {
  26. json = cson.parse(xmlOrJSON);
  27. }
  28. }
  29. callback && callback(json);
  30. return json;
  31. };
  32. exports.formatJSON = function(object, initialIndent) {
  33. return JSON.stringify(object, null, 4).replace(/^/gm, initialIndent||"");
  34. };
  35. exports.formatJS = function(object, initialIndent) {
  36. return formatJS(object, 4, initialIndent);
  37. };
  38. function formatJS(object, indent, initialIndent) {
  39. if (typeof indent == "number")
  40. indent = Array(indent + 1).join(" ");
  41. function $format(buffer, totalIndent, state, o) {
  42. if (typeof o != "object" || !o) {
  43. if (typeof o == "string")
  44. buffer.push(JSON.stringify(o));
  45. else
  46. buffer.push("" + o);
  47. }
  48. else if (Array.isArray(o)) {
  49. buffer.push("[")
  50. var len = totalIndent.length
  51. var oneLine = true;
  52. for (var i = 0; i < o.length; i++) {
  53. if (typeof o[i] == "string") {
  54. len += o[i].length + 2
  55. } else if (!o[i]) {
  56. len += (o[i] + "").length
  57. } else {
  58. oneLine = false;
  59. break;
  60. }
  61. len += 2;
  62. if (len > 60) {
  63. oneLine = false;
  64. break;
  65. }
  66. }
  67. for (var i = 0; i < o.length; i++) {
  68. if (o[i] && typeof o[i] == "object") {
  69. $format(buffer, totalIndent, state, o[i]);
  70. if (i < o.length - 1)
  71. buffer.push(", ");
  72. } else {
  73. if (oneLine)
  74. i && buffer.push(" ");
  75. else
  76. buffer.push("\n", totalIndent + indent)
  77. $format(buffer, totalIndent + indent, state, o[i]);
  78. if (i < o.length - 1)
  79. buffer.push(",");
  80. }
  81. }
  82. if (!oneLine && buffer[buffer.length - 1] != "}")
  83. buffer.push("\n" + totalIndent)
  84. buffer.push("]")
  85. }
  86. else {
  87. var keys = Object.keys(o);
  88. buffer.push("{", "\n");
  89. for (var i = 0; i < keys.length; i++) {
  90. buffer.push(totalIndent + indent);
  91. if (/^\w+$/.test(keys[i]))
  92. buffer.push(keys[i]);
  93. else
  94. buffer.push(JSON.stringify(keys[i]));
  95. buffer.push(": ")
  96. if (keys[i] == "regex" && typeof o[keys[i]] == "string") {
  97. try {
  98. var re = new RegExp(o[keys[i]]);
  99. buffer.push("/" + re.source.replace(/\\.|\//g, function(f) {
  100. return f.length == 1 ? "\\" + f : f;
  101. }) + "/");
  102. } catch(e) {
  103. $format(buffer, totalIndent + indent, state, o[keys[i]]);
  104. }
  105. } else {
  106. $format(buffer, totalIndent + indent, state, o[keys[i]]);
  107. }
  108. if (i < keys.length - 1)
  109. buffer.push(",", "\n");
  110. }
  111. buffer.push("\n", totalIndent, "}");
  112. }
  113. }
  114. var buffer = [];
  115. $format(buffer, initialIndent || "", {}, object);
  116. return buffer.join("");
  117. }
  118. exports.fillTemplate = function(template, replacements) {
  119. return template.replace(/%(.+?)%/g, function(str, m) {
  120. return replacements[m] || "";
  121. });
  122. };
  123. exports.hyphenate = function(str) {
  124. return str.replace(/([A-Z])/g, "-$1").replace(/[_\s\-]+/g, "-").toLowerCase();
  125. };
  126. exports.camelCase = function(str) {
  127. return str.replace(/[\-_\s]+(.?)/g, function(x, y) {return y.toUpperCase()});
  128. };
  129. exports.snakeCase = function(str) {
  130. return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[_\s\-]+/g, "_").toLowerCase();
  131. };
  132. exports.quoteString = function(str) {
  133. return '"' + str.replace(/\\/, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\\n") + '"';
  134. };
  135. exports.restoreJSONComments = function(objStr) {
  136. return objStr.replace(/^(\s*)comment: '(.*)'/gm, function(_, i, c) {
  137. return i + "//" + c.replace(/\\n(\\t)*/g, "\n" + i + "//") + "\n" + i;
  138. }).replace(/ \/\/ ERROR/g, '", // ERROR');
  139. };
  140. exports.download = function(href, callback) {
  141. var options = url.parse(href);
  142. var protocol = options.protocol === "https:" ? https : http;
  143. console.log("connecting to " + options.host + " " + options.path);
  144. var request = protocol.get(options, function(res) {
  145. var data = "";
  146. res.setEncoding("utf-8");
  147. res.on("data", function(chunk) {
  148. data += chunk;
  149. });
  150. res.on("end", function(){
  151. callback(data);
  152. });
  153. });
  154. };
  155. exports.AceRoot = __dirname + "/../";
  156. exports.AceLib = __dirname + "/../lib/";