Skip to content

Commit

Permalink
feat: Colors in console output
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Nov 28, 2020
1 parent 488b477 commit 49189de
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -20,6 +20,7 @@
"node": ">= 10.8.0"
},
"dependencies": {
"colors": "^1.4.0",
"fs-extra": "^9.0.1",
"handlebars": "^4.7.6",
"lodash": "^4.17.20",
Expand Down
46 changes: 15 additions & 31 deletions src/lib/utils/loggers.ts
@@ -1,6 +1,8 @@
import * as ts from "typescript";
import * as Util from "util";
import { url } from "inspector";
import { resolve } from "path";
import { red, yellow, cyan, green, gray } from "colors/safe";

const isDebugging = () => Boolean(url());

Expand Down Expand Up @@ -162,29 +164,11 @@ export class Logger {
* @param diagnostic The TypeScript message that should be logged.
*/
public diagnostic(diagnostic: ts.Diagnostic) {
let output: string;
if (diagnostic.file) {
output = diagnostic.file.fileName;
output +=
"(" +
ts.getLineAndCharacterOfPosition(
diagnostic.file,
diagnostic.start || 0
).line +
")";
output +=
ts.sys.newLine +
" " +
ts.flattenDiagnosticMessageText(
diagnostic.messageText,
ts.sys.newLine
);
} else {
output = ts.flattenDiagnosticMessageText(
diagnostic.messageText,
ts.sys.newLine
);
}
const output = ts.formatDiagnosticsWithColorAndContext([diagnostic], {
getCanonicalFileName: resolve,
getCurrentDirectory: () => process.cwd(),
getNewLine: () => ts.sys.newLine,
});

switch (diagnostic.category) {
case ts.DiagnosticCategory.Error:
Expand Down Expand Up @@ -220,14 +204,14 @@ export class ConsoleLogger extends Logger {
return;
}

let output = "";
if (level === LogLevel.Error) {
output += "Error: ";
}
if (level === LogLevel.Warn) {
output += "Warning: ";
}
output += message;
const output =
{
[LogLevel.Error]: red("Error: "),
[LogLevel.Warn]: yellow("Warning: "),
[LogLevel.Info]: cyan("Info: "),
[LogLevel.Success]: green("Ok: "),
[LogLevel.Verbose]: gray("Debug: "),
}[level] + message;

if (newLine || level === LogLevel.Success) {
ts.sys.write(ts.sys.newLine);
Expand Down

0 comments on commit 49189de

Please sign in to comment.