Skip to content

Commit

Permalink
feat(log): make log package
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh3112 committed Jan 17, 2019
1 parent 205a469 commit af7c2d8
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 107 deletions.
107 changes: 0 additions & 107 deletions bin/log.js

This file was deleted.

89 changes: 89 additions & 0 deletions packages/log/index.ts
@@ -0,0 +1,89 @@
import chalk from "chalk";

const log = (message: string) => process.stdout.write(message);
const logError = (message: string) => process.stderr.write(message);

export class Logger {
public name: string;

constructor(input: { name?: string, start?: boolean} | string) {
if (input) {
if (typeof input === "string") {
this.name = input;
} else {
if (!input.name || input.name === "") {
throw new Error("Name of the task was not passed");
}
this.name = input.name;
if (input.start) {
this.start();
}
}
} else {
throw new Error("Name of the task was not passed");
}
}

public log(message: string) {
message = this.build(message);
message = ` ${chalk.bold("•")} ${message}`;
log(message);
}

public success(message: string) {
message = this.build(message);
message = chalk.green(` ${chalk.bold("\u2713")} ${message}`);
log(message);
}

public error(message: string) {
message = this.build(message);
message = chalk.red(` ${chalk.bold("\u2717")} ${message}`);
logError(message);
}

public warn(message: string) {
message = this.build(message);
message = chalk.yellowBright(` ${chalk.bold("⚠")} ${message}`);
log(message);
}

public info(message: string) {
message = this.build(message);
message = chalk.cyan(` ${chalk.bold("i")} ${message}`);
log(message);
}

public clrscr() {
log("\x1Bc");
this.start();
}

public custom(symbol: string, message: string) {
if (symbol.length !== 1) {
throw new Error("Only single character can be passed to custom");
} else {
message = this.build(message);
message = ` ${chalk.bold(symbol)} ${message}`;
log(message);
}
}

private build(message: string): string {
const lines = message.split("\n");
if (lines.length === 1) {
return lines[0];
}
message = lines[0] + "\n";

for (let i = 1; i < lines.length; i++) {
message += ` ${lines[i]}\n`;
}
return message;
}

private start() {
const message: string = `${ chalk.bold(this.name) } - ${chalk.cyan("webpack-cli")}` + "\n";
log(message);
}
}
8 changes: 8 additions & 0 deletions packages/log/package.json
@@ -0,0 +1,8 @@
{
"name": "@webpack-cli/log",
"version": "0.0.1",
"description": "custom task based logger",
"main": "index.js",
"author": "",
"license": "MIT"
}
3 changes: 3 additions & 0 deletions packages/log/tsconfig.json
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.packages.json"
}

0 comments on commit af7c2d8

Please sign in to comment.