Task: Fixed ts common package

This commit is contained in:
Sambo Chea 2021-12-22 16:30:34 +07:00
parent e26ee50faf
commit 596d6e92d2
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
4 changed files with 18 additions and 77 deletions

View File

@ -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",

View File

@ -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,
}

View File

@ -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}`
}

View File

@ -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