|
| 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 | +} |
0 commit comments