UNPKG

3.55 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 }
17 hasErrors() {
18 return this.errorCount > 0;
19 }
20 resetErrors() {
21 this.errorCount = 0;
22 }
23 write(text, ...args) {
24 this.log(Util.format.apply(this, arguments), LogLevel.Info);
25 }
26 writeln(text, ...args) {
27 this.log(Util.format.apply(this, arguments), LogLevel.Info, true);
28 }
29 success(text, ...args) {
30 this.log(Util.format.apply(this, arguments), LogLevel.Success);
31 }
32 verbose(text, ...args) {
33 this.log(Util.format.apply(this, arguments), LogLevel.Verbose);
34 }
35 warn(text, ...args) {
36 this.log(Util.format.apply(this, arguments), LogLevel.Warn);
37 }
38 error(text, ...args) {
39 this.log(Util.format.apply(this, arguments), LogLevel.Error);
40 }
41 log(message, level = LogLevel.Info, newLine) {
42 if (level === LogLevel.Error) {
43 this.errorCount += 1;
44 }
45 }
46 diagnostics(diagnostics) {
47 diagnostics.forEach((diagnostic) => {
48 this.diagnostic(diagnostic);
49 });
50 }
51 diagnostic(diagnostic) {
52 let output;
53 if (diagnostic.file) {
54 output = diagnostic.file.fileName;
55 output += '(' + ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start || 0).line + ')';
56 output += ts.sys.newLine + ' ' + ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine);
57 }
58 else {
59 output = ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine);
60 }
61 switch (diagnostic.category) {
62 case ts.DiagnosticCategory.Error:
63 this.log(output, LogLevel.Error);
64 break;
65 case ts.DiagnosticCategory.Warning:
66 this.log(output, LogLevel.Warn);
67 break;
68 case ts.DiagnosticCategory.Message:
69 this.log(output, LogLevel.Info);
70 }
71 }
72}
73exports.Logger = Logger;
74class ConsoleLogger extends Logger {
75 log(message, level = LogLevel.Info, newLine) {
76 if (level === LogLevel.Error) {
77 this.errorCount += 1;
78 }
79 let output = '';
80 if (level === LogLevel.Error) {
81 output += 'Error: ';
82 }
83 if (level === LogLevel.Warn) {
84 output += 'Warning: ';
85 }
86 output += message;
87 if (newLine || level === LogLevel.Success) {
88 ts.sys.write(ts.sys.newLine);
89 }
90 ts.sys.write(output + ts.sys.newLine);
91 if (level === LogLevel.Success) {
92 ts.sys.write(ts.sys.newLine);
93 }
94 }
95}
96exports.ConsoleLogger = ConsoleLogger;
97class CallbackLogger extends Logger {
98 constructor(callback) {
99 super();
100 this.callback = callback;
101 }
102 log(message, level = LogLevel.Info, newLine) {
103 if (level === LogLevel.Error) {
104 this.errorCount += 1;
105 }
106 this.callback(message, level, newLine);
107 }
108}
109exports.CallbackLogger = CallbackLogger;
110//# sourceMappingURL=loggers.js.map
\No newline at end of file