Skip to content

Commit df7c224

Browse files
rishabh3112evenstensberg
authored andcommitted
feat(log): make log package
1 parent 11b3bff commit df7c224

File tree

4 files changed

+100
-107
lines changed

4 files changed

+100
-107
lines changed

bin/log.js

Lines changed: 0 additions & 107 deletions
This file was deleted.

packages/log/index.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import chalk from "chalk";
2+
3+
const log = (message: string) => process.stdout.write(message);
4+
const logError = (message: string) => process.stderr.write(message);
5+
6+
export class Logger {
7+
public name: string;
8+
9+
constructor(input: { name?: string, start?: boolean} | string) {
10+
if (input) {
11+
if (typeof input === "string") {
12+
this.name = input;
13+
} else {
14+
if (!input.name || input.name === "") {
15+
throw new Error("Name of the task was not passed");
16+
}
17+
this.name = input.name;
18+
if (input.start) {
19+
this.start();
20+
}
21+
}
22+
} else {
23+
throw new Error("Name of the task was not passed");
24+
}
25+
}
26+
27+
public log(message: string) {
28+
message = this.build(message);
29+
message = ` ${chalk.bold("•")} ${message}`;
30+
log(message);
31+
}
32+
33+
public success(message: string) {
34+
message = this.build(message);
35+
message = chalk.green(` ${chalk.bold("\u2713")} ${message}`);
36+
log(message);
37+
}
38+
39+
public error(message: string) {
40+
message = this.build(message);
41+
message = chalk.red(` ${chalk.bold("\u2717")} ${message}`);
42+
logError(message);
43+
}
44+
45+
public warn(message: string) {
46+
message = this.build(message);
47+
message = chalk.yellowBright(` ${chalk.bold("⚠")} ${message}`);
48+
log(message);
49+
}
50+
51+
public info(message: string) {
52+
message = this.build(message);
53+
message = chalk.cyan(` ${chalk.bold("i")} ${message}`);
54+
log(message);
55+
}
56+
57+
public clrscr() {
58+
log("\x1Bc");
59+
this.start();
60+
}
61+
62+
public custom(symbol: string, message: string) {
63+
if (symbol.length !== 1) {
64+
throw new Error("Only single character can be passed to custom");
65+
} else {
66+
message = this.build(message);
67+
message = ` ${chalk.bold(symbol)} ${message}`;
68+
log(message);
69+
}
70+
}
71+
72+
private build(message: string): string {
73+
const lines = message.split("\n");
74+
if (lines.length === 1) {
75+
return lines[0];
76+
}
77+
message = lines[0] + "\n";
78+
79+
for (let i = 1; i < lines.length; i++) {
80+
message += ` ${lines[i]}\n`;
81+
}
82+
return message;
83+
}
84+
85+
private start() {
86+
const message: string = `${ chalk.bold(this.name) } - ${chalk.cyan("webpack-cli")}` + "\n";
87+
log(message);
88+
}
89+
}

packages/log/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@webpack-cli/log",
3+
"version": "0.0.1",
4+
"description": "custom task based logger",
5+
"main": "index.js",
6+
"author": "",
7+
"license": "MIT"
8+
}

packages/log/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../tsconfig.packages.json"
3+
}

0 commit comments

Comments
 (0)