123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var errors_1 = require("../util/errors");
  4. var config_1 = require("../util/config");
  5. var chalk = require("chalk");
  6. var Logger = (function () {
  7. function Logger(scope) {
  8. this.start = Date.now();
  9. this.scope = scope;
  10. var msg = scope + " started " + chalk.dim('...');
  11. if (config_1.isDebugMode()) {
  12. msg += memoryUsage();
  13. }
  14. Logger.info(msg);
  15. }
  16. Logger.prototype.ready = function (color, bold) {
  17. this.completed('ready', color, bold);
  18. };
  19. Logger.prototype.finish = function (color, bold) {
  20. this.completed('finished', color, bold);
  21. };
  22. Logger.prototype.completed = function (type, color, bold) {
  23. var duration = Date.now() - this.start;
  24. var time;
  25. if (duration > 1000) {
  26. time = 'in ' + (duration / 1000).toFixed(2) + ' s';
  27. }
  28. else {
  29. var ms = parseFloat((duration).toFixed(3));
  30. if (ms > 0) {
  31. time = 'in ' + duration + ' ms';
  32. }
  33. else {
  34. time = 'in less than 1 ms';
  35. }
  36. }
  37. var msg = this.scope + " " + type;
  38. if (color) {
  39. msg = chalk[color](msg);
  40. }
  41. if (bold) {
  42. msg = chalk.bold(msg);
  43. }
  44. msg += ' ' + chalk.dim(time);
  45. if (config_1.isDebugMode()) {
  46. msg += memoryUsage();
  47. }
  48. Logger.info(msg);
  49. };
  50. Logger.prototype.fail = function (err) {
  51. if (err) {
  52. if (err instanceof errors_1.IgnorableError) {
  53. return;
  54. }
  55. if (err instanceof errors_1.BuildError) {
  56. var failedMsg = this.scope + " failed";
  57. if (err.message) {
  58. failedMsg += ": " + err.message;
  59. }
  60. if (!err.hasBeenLogged) {
  61. Logger.error("" + failedMsg);
  62. err.hasBeenLogged = true;
  63. if (err.stack && config_1.isDebugMode()) {
  64. Logger.debug(err.stack);
  65. }
  66. }
  67. else if (config_1.isDebugMode()) {
  68. Logger.debug("" + failedMsg);
  69. }
  70. return err;
  71. }
  72. }
  73. return err;
  74. };
  75. Logger.prototype.setStartTime = function (startTime) {
  76. this.start = startTime;
  77. };
  78. /**
  79. * Does not print out a time prefix or color any text. Only prefix
  80. * with whitespace so the message is lined up with timestamped logs.
  81. */
  82. Logger.log = function () {
  83. var msg = [];
  84. for (var _i = 0; _i < arguments.length; _i++) {
  85. msg[_i] = arguments[_i];
  86. }
  87. Logger.wordWrap(msg).forEach(function (line) {
  88. console.log(line);
  89. });
  90. };
  91. /**
  92. * Prints out a dim colored timestamp prefix, with optional color
  93. * and bold message.
  94. */
  95. Logger.info = function (msg, color, bold) {
  96. var lines = Logger.wordWrap([msg]);
  97. if (lines.length) {
  98. var prefix = timePrefix();
  99. var lineOneMsg = lines[0].substr(prefix.length);
  100. if (color) {
  101. lineOneMsg = chalk[color](lineOneMsg);
  102. }
  103. if (bold) {
  104. lineOneMsg = chalk.bold(lineOneMsg);
  105. }
  106. lines[0] = chalk.dim(prefix) + lineOneMsg;
  107. }
  108. lines.forEach(function (line, lineIndex) {
  109. if (lineIndex > 0) {
  110. if (color) {
  111. line = chalk[color](line);
  112. }
  113. if (bold) {
  114. line = chalk.bold(line);
  115. }
  116. }
  117. console.log(line);
  118. });
  119. };
  120. /**
  121. * Prints out a yellow colored timestamp prefix.
  122. */
  123. Logger.warn = function () {
  124. var msg = [];
  125. for (var _i = 0; _i < arguments.length; _i++) {
  126. msg[_i] = arguments[_i];
  127. }
  128. var lines = Logger.wordWrap(msg);
  129. if (lines.length) {
  130. var prefix = timePrefix();
  131. lines[0] = prefix + lines[0].substr(prefix.length);
  132. }
  133. lines.forEach(function (line) {
  134. console.warn(chalk.yellow(line));
  135. });
  136. };
  137. /**
  138. * Prints out a error colored timestamp prefix.
  139. */
  140. Logger.error = function () {
  141. var msg = [];
  142. for (var _i = 0; _i < arguments.length; _i++) {
  143. msg[_i] = arguments[_i];
  144. }
  145. var lines = Logger.wordWrap(msg);
  146. if (lines.length) {
  147. var prefix = timePrefix();
  148. lines[0] = prefix + lines[0].substr(prefix.length);
  149. if (config_1.isDebugMode()) {
  150. lines[0] += memoryUsage();
  151. }
  152. }
  153. lines.forEach(function (line) {
  154. console.error(chalk.red(line));
  155. });
  156. };
  157. Logger.unformattedError = function (msg) {
  158. console.error(chalk.red(msg));
  159. };
  160. Logger.unformattedDebug = function (msg) {
  161. console.log(chalk.cyan(msg));
  162. };
  163. /**
  164. * Prints out a blue colored DEBUG prefix. Only prints out when debug mode.
  165. */
  166. Logger.debug = function () {
  167. var msg = [];
  168. for (var _i = 0; _i < arguments.length; _i++) {
  169. msg[_i] = arguments[_i];
  170. }
  171. if (config_1.isDebugMode()) {
  172. msg.push(memoryUsage());
  173. var lines = Logger.wordWrap(msg);
  174. if (lines.length) {
  175. var prefix = '[ DEBUG! ]';
  176. lines[0] = prefix + lines[0].substr(prefix.length);
  177. }
  178. lines.forEach(function (line) {
  179. console.log(chalk.cyan(line));
  180. });
  181. }
  182. };
  183. Logger.wordWrap = function (msg) {
  184. var output = [];
  185. var words = [];
  186. msg.forEach(function (m) {
  187. if (m === null) {
  188. words.push('null');
  189. }
  190. else if (typeof m === 'undefined') {
  191. words.push('undefined');
  192. }
  193. else if (typeof m === 'string') {
  194. m.replace(/\s/gm, ' ').split(' ').forEach(function (strWord) {
  195. if (strWord.trim().length) {
  196. words.push(strWord.trim());
  197. }
  198. });
  199. }
  200. else if (typeof m === 'number' || typeof m === 'boolean') {
  201. words.push(m.toString());
  202. }
  203. else if (typeof m === 'function') {
  204. words.push(m.toString());
  205. }
  206. else if (Array.isArray(m)) {
  207. words.push(function () {
  208. return m.toString();
  209. });
  210. }
  211. else if (Object(m) === m) {
  212. words.push(function () {
  213. return m.toString();
  214. });
  215. }
  216. else {
  217. words.push(m.toString());
  218. }
  219. });
  220. var line = Logger.INDENT;
  221. words.forEach(function (word) {
  222. if (typeof word === 'function') {
  223. if (line.trim().length) {
  224. output.push(line);
  225. }
  226. output.push(word());
  227. line = Logger.INDENT;
  228. }
  229. else if (Logger.INDENT.length + word.length > Logger.MAX_LEN) {
  230. // word is too long to play nice, just give it its own line
  231. if (line.trim().length) {
  232. output.push(line);
  233. }
  234. output.push(Logger.INDENT + word);
  235. line = Logger.INDENT;
  236. }
  237. else if ((word.length + line.length) > Logger.MAX_LEN) {
  238. // this word would make the line too long
  239. // print the line now, then start a new one
  240. output.push(line);
  241. line = Logger.INDENT + word + ' ';
  242. }
  243. else {
  244. line += word + ' ';
  245. }
  246. });
  247. if (line.trim().length) {
  248. output.push(line);
  249. }
  250. return output;
  251. };
  252. Logger.formatFileName = function (rootDir, fileName) {
  253. fileName = fileName.replace(rootDir, '');
  254. if (/\/|\\/.test(fileName.charAt(0))) {
  255. fileName = fileName.substr(1);
  256. }
  257. if (fileName.length > 80) {
  258. fileName = '...' + fileName.substr(fileName.length - 80);
  259. }
  260. return fileName;
  261. };
  262. Logger.formatHeader = function (type, fileName, rootDir, startLineNumber, endLineNumber) {
  263. if (startLineNumber === void 0) { startLineNumber = null; }
  264. if (endLineNumber === void 0) { endLineNumber = null; }
  265. var header = type + ": " + Logger.formatFileName(rootDir, fileName);
  266. if (startLineNumber !== null && startLineNumber > 0) {
  267. if (endLineNumber !== null && endLineNumber > startLineNumber) {
  268. header += ", lines: " + startLineNumber + " - " + endLineNumber;
  269. }
  270. else {
  271. header += ", line: " + startLineNumber;
  272. }
  273. }
  274. return header;
  275. };
  276. Logger.newLine = function () {
  277. console.log('');
  278. };
  279. Logger.INDENT = ' ';
  280. Logger.MAX_LEN = 120;
  281. return Logger;
  282. }());
  283. exports.Logger = Logger;
  284. function timePrefix() {
  285. var date = new Date();
  286. return '[' + ('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2) + ':' + ('0' + date.getSeconds()).slice(-2) + ']';
  287. }
  288. function memoryUsage() {
  289. return chalk.dim(" MEM: " + (process.memoryUsage().rss / 1000000).toFixed(1) + "MB");
  290. }