UNPKG

3.71 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const ts = require("typescript");
4const Util = require("util");
5var LogLevel;
6(function (LogLevel) {
7 LogLevel[LogLevel["Verbose"] = 0] = "Verbose";
8 LogLevel[LogLevel["Info"] = 1] = "Info";
9 LogLevel[LogLevel["Warn"] = 2] = "Warn";
10 LogLevel[LogLevel["Error"] = 3] = "Error";
11 LogLevel[LogLevel["Success"] = 4] = "Success";
12})(LogLevel = exports.LogLevel || (exports.LogLevel = {}));
13class Logger {
14 constructor() {
15 this.errorCount = 0;
16 this.warningCount = 0;
17 }
18 hasErrors() {
19 return this.errorCount > 0;
20 }
21 hasWarnings() {
22 return this.warningCount > 0;
23 }
24 resetErrors() {
25 this.errorCount = 0;
26 }
27 resetWarnings() {
28 this.warningCount = 0;
29 }
30 write(text, ...args) {
31 this.log(Util.format.apply(this, arguments), LogLevel.Info);
32 }
33 writeln(text, ...args) {
34 this.log(Util.format.apply(this, arguments), LogLevel.Info, true);
35 }
36 success(text, ...args) {
37 this.log(Util.format.apply(this, arguments), LogLevel.Success);
38 }
39 verbose(text, ...args) {
40 this.log(Util.format.apply(this, arguments), LogLevel.Verbose);
41 }
42 warn(text, ...args) {
43 this.log(Util.format.apply(this, arguments), LogLevel.Warn);
44 }
45 error(text, ...args) {
46 this.log(Util.format.apply(this, arguments), LogLevel.Error);
47 }
48 log(message, level = LogLevel.Info, newLine) {
49 if (level === LogLevel.Error) {
50 this.errorCount += 1;
51 }
52 if (level === LogLevel.Warn) {
53 this.warningCount += 1;
54 }
55 }
56 diagnostics(diagnostics) {
57 diagnostics.forEach((diagnostic) => {
58 this.diagnostic(diagnostic);
59 });
60 }
61 diagnostic(diagnostic) {
62 let output;
63 if (diagnostic.file) {
64 output = diagnostic.file.fileName;
65 output += '(' + ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start || 0).line + ')';
66 output += ts.sys.newLine + ' ' + ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine);
67 }
68 else {
69 output = ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine);
70 }
71 switch (diagnostic.category) {
72 case ts.DiagnosticCategory.Error:
73 this.log(output, LogLevel.Error);
74 break;
75 case ts.DiagnosticCategory.Warning:
76 this.log(output, LogLevel.Warn);
77 break;
78 case ts.DiagnosticCategory.Message:
79 this.log(output, LogLevel.Info);
80 }
81 }
82}
83exports.Logger = Logger;
84class ConsoleLogger extends Logger {
85 log(message, level = LogLevel.Info, newLine) {
86 super.log(message, level, newLine);
87 let output = '';
88 if (level === LogLevel.Error) {
89 output += 'Error: ';
90 }
91 if (level === LogLevel.Warn) {
92 output += 'Warning: ';
93 }
94 output += message;
95 if (newLine || level === LogLevel.Success) {
96 ts.sys.write(ts.sys.newLine);
97 }
98 ts.sys.write(output + ts.sys.newLine);
99 if (level === LogLevel.Success) {
100 ts.sys.write(ts.sys.newLine);
101 }
102 }
103}
104exports.ConsoleLogger = ConsoleLogger;
105class CallbackLogger extends Logger {
106 constructor(callback) {
107 super();
108 this.callback = callback;
109 }
110 log(message, level = LogLevel.Info, newLine) {
111 super.log(message, level, newLine);
112 this.callback(message, level, newLine);
113 }
114}
115exports.CallbackLogger = CallbackLogger;
116//# sourceMappingURL=loggers.js.map
\No newline at end of file