Task: Fixed ts common package
This commit is contained in:
parent
e26ee50faf
commit
596d6e92d2
@ -3,6 +3,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "Typescript common and web utils",
|
"description": "Typescript common and web utils",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
"source": "src/index.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "tsc -w",
|
"start": "tsc -w",
|
||||||
"build": "rm -rf dist && tsc",
|
"build": "rm -rf dist && tsc",
|
||||||
@ -39,4 +40,4 @@
|
|||||||
"lint-staged": {
|
"lint-staged": {
|
||||||
"**/*": "prettier --write --ignore-unknown"
|
"**/*": "prettier --write --ignore-unknown"
|
||||||
}
|
}
|
||||||
}
|
}
|
30
src/index.ts
30
src/index.ts
@ -1,16 +1,16 @@
|
|||||||
// import { formatDate, formatDateOnly, formatDateFromTimestamp } from "./date"
|
import { formatDate, formatDateOnly, formatDateFromTimestamp } from "./date"
|
||||||
// import { debug, info,error, warn } from './log'
|
import { debug, info, error, warn } from "./log"
|
||||||
// import { parse2Int, parse2IntOrNull, parseToBoolean } from "./number/parser"
|
import { parse2Int, parse2IntOrNull, parseToBoolean } from "./number/parser"
|
||||||
|
|
||||||
// export {
|
export {
|
||||||
// formatDate,
|
formatDate,
|
||||||
// formatDateOnly,
|
formatDateOnly,
|
||||||
// formatDateFromTimestamp,
|
formatDateFromTimestamp,
|
||||||
// debug,
|
debug,
|
||||||
// info,
|
info,
|
||||||
// error,
|
error,
|
||||||
// warn,
|
warn,
|
||||||
// parse2Int,
|
parse2Int,
|
||||||
// parse2IntOrNull,
|
parse2IntOrNull,
|
||||||
// parseToBoolean,
|
parseToBoolean,
|
||||||
// }
|
}
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import chalk from "chalk"
|
import chalk from "chalk"
|
||||||
import { formatDate } from "../date"
|
import { formatDate } from "../date"
|
||||||
import Logger from "./writter"
|
|
||||||
|
|
||||||
let logger = Logger.getProvider()
|
const logger = console
|
||||||
|
|
||||||
const prefixColor = (
|
const prefixColor = (
|
||||||
prefix: string,
|
prefix: string,
|
||||||
@ -10,10 +9,6 @@ const prefixColor = (
|
|||||||
color: string = "green"
|
color: string = "green"
|
||||||
) => {
|
) => {
|
||||||
const timestamp = formatDate(new Date())
|
const timestamp = formatDate(new Date())
|
||||||
if (Logger.isEnabled()) {
|
|
||||||
return `[${timestamp}] - ${prefix}: ${message}`
|
|
||||||
}
|
|
||||||
|
|
||||||
const firstStep = chalk`[${timestamp}] - {${color} ${prefix}}:`
|
const firstStep = chalk`[${timestamp}] - {${color} ${prefix}}:`
|
||||||
return `${firstStep} ${message}`
|
return `${firstStep} ${message}`
|
||||||
}
|
}
|
||||||
|
@ -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
|
|
Loading…
Reference in New Issue
Block a user