From 596d6e92d2812178bd0d60d16d278de6061a6135 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Wed, 22 Dec 2021 16:30:34 +0700 Subject: [PATCH] Task: Fixed ts common package --- package.json | 3 ++- src/index.ts | 30 ++++++++++++------------- src/log/index.ts | 7 +----- src/log/writter.ts | 55 ---------------------------------------------- 4 files changed, 18 insertions(+), 77 deletions(-) delete mode 100644 src/log/writter.ts diff --git a/package.json b/package.json index 16c940e..580dcc2 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "description": "Typescript common and web utils", "main": "dist/index.js", + "source": "src/index.ts", "scripts": { "start": "tsc -w", "build": "rm -rf dist && tsc", @@ -39,4 +40,4 @@ "lint-staged": { "**/*": "prettier --write --ignore-unknown" } -} +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 1b26a25..9838f89 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,16 +1,16 @@ -// import { formatDate, formatDateOnly, formatDateFromTimestamp } from "./date" -// import { debug, info,error, warn } from './log' -// import { parse2Int, parse2IntOrNull, parseToBoolean } from "./number/parser" +import { formatDate, formatDateOnly, formatDateFromTimestamp } from "./date" +import { debug, info, error, warn } from "./log" +import { parse2Int, parse2IntOrNull, parseToBoolean } from "./number/parser" -// export { -// formatDate, -// formatDateOnly, -// formatDateFromTimestamp, -// debug, -// info, -// error, -// warn, -// parse2Int, -// parse2IntOrNull, -// parseToBoolean, -// } +export { + formatDate, + formatDateOnly, + formatDateFromTimestamp, + debug, + info, + error, + warn, + parse2Int, + parse2IntOrNull, + parseToBoolean, +} diff --git a/src/log/index.ts b/src/log/index.ts index 2849c70..77f9934 100644 --- a/src/log/index.ts +++ b/src/log/index.ts @@ -1,8 +1,7 @@ import chalk from "chalk" import { formatDate } from "../date" -import Logger from "./writter" -let logger = Logger.getProvider() +const logger = console const prefixColor = ( prefix: string, @@ -10,10 +9,6 @@ const prefixColor = ( color: string = "green" ) => { const timestamp = formatDate(new Date()) - if (Logger.isEnabled()) { - return `[${timestamp}] - ${prefix}: ${message}` - } - const firstStep = chalk`[${timestamp}] - {${color} ${prefix}}:` return `${firstStep} ${message}` } diff --git a/src/log/writter.ts b/src/log/writter.ts deleted file mode 100644 index 9bee82a..0000000 --- a/src/log/writter.ts +++ /dev/null @@ -1,55 +0,0 @@ -import * as fs from "fs" -import { Console } from "console" -import * as path from "path" -import { formatDateOnly } from "../date" - -class Logger { - private static enabled: boolean = false - public static setEnabled(enabled: boolean): void { - this.enabled = enabled - } - public static isEnabled(): boolean { - return this.enabled - } - - private static logDir?: string - public static setLogDir(logDir: string): void { - this.logDir = logDir - } - public static getLogDir(): string | null | undefined { - return this.logDir - } - - public static getProvider(): Console { - if (this.isEnabled()) { - const rootPath = path.resolve(this.getLogDir() || "./") - const logPath = `${rootPath}/logs` - - if (!fs.existsSync(logPath)) { - fs.mkdirSync(logPath, { recursive: true }) - } - - const time = formatDateOnly(new Date()).replace(/\//g, "-") - - console.log("Logs writing to path =>", logPath) - const output = fs.createWriteStream( - `${logPath}/stdout-${time}.log`, - { - flags: "a", - } - ) - const errorOutput = fs.createWriteStream( - `${logPath}/stderr-${time}.log`, - { - flags: "a", - } - ) - - return new Console({ stdout: output, stderr: errorOutput }) - } - - return console - } -} - -export default Logger