@@ -1,4 +0,0 @@
|
||||
module.exports = {
|
||||
...require('@innei/prettier'),
|
||||
importOrderParserPlugins: ['typescript', 'jsx', 'decorators-legacy'],
|
||||
}
|
||||
8
.prettierrc.mjs
Normal file
8
.prettierrc.mjs
Normal file
@@ -0,0 +1,8 @@
|
||||
import { factory } from '@innei/prettier'
|
||||
|
||||
export default {
|
||||
...factory({
|
||||
tailwindcss: false,
|
||||
}),
|
||||
importOrderParserPlugins: ['typescript', 'jsx', 'decorators-legacy'],
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
module.exports = require('../../.prettierrc.cjs')
|
||||
4
apps/core/global.d.ts
vendored
4
apps/core/global.d.ts
vendored
@@ -1,9 +1,9 @@
|
||||
import type { Consola } from 'consola'
|
||||
import type { Document, PaginateModel } from 'mongoose'
|
||||
|
||||
import 'zx-cjs/globals'
|
||||
|
||||
import type { ModelType } from '@typegoose/typegoose/lib/types'
|
||||
import type { WrappedConsola } from 'nestjs-pretty-logger/lib/consola'
|
||||
|
||||
declare global {
|
||||
export type KV<T = any> = Record<string, T>
|
||||
@@ -13,7 +13,7 @@ declare global {
|
||||
|
||||
export const isDev: boolean
|
||||
|
||||
export const consola: Consola
|
||||
export const consola: WrappedConsola
|
||||
|
||||
export const cwd: string
|
||||
}
|
||||
|
||||
@@ -82,7 +82,6 @@
|
||||
"class-validator": "0.13.2",
|
||||
"class-validator-jsonschema": "npm:@innei/class-validator-jsonschema@3.1.2",
|
||||
"commander": "11.1.0",
|
||||
"consola": "^2.15.3",
|
||||
"dayjs": "1.11.10",
|
||||
"ejs": "3.1.9",
|
||||
"form-data": "4.0.0",
|
||||
@@ -107,6 +106,7 @@
|
||||
"mongoose-lean-virtuals": "0.9.1",
|
||||
"mongoose-paginate-v2": "1.7.31",
|
||||
"nanoid": "3.3.4",
|
||||
"nestjs-pretty-logger": "0.1.0-5",
|
||||
"node-machine-id": "1.1.12",
|
||||
"node-pty": "1.0.0",
|
||||
"nodemailer": "6.9.7",
|
||||
|
||||
@@ -1,97 +1,32 @@
|
||||
/* eslint-disable prefer-rest-params */
|
||||
import { createWriteStream } from 'fs'
|
||||
import consola_, { FancyReporter, LogLevel } from 'consola'
|
||||
import { CronJob } from 'cron'
|
||||
|
||||
import { CronExpression } from '@nestjs/schedule'
|
||||
/* eslint-disable prefer-rest-params */
|
||||
import { createLogger } from 'nestjs-pretty-logger'
|
||||
|
||||
import { getTodayLogFilePath } from '~/utils/path.util'
|
||||
import { LOG_DIR } from '~/constants/path.constant'
|
||||
|
||||
import { redisSubPub } from '../utils/redis-subpub.util'
|
||||
import { getShortTime } from '../utils/time.util'
|
||||
import { isDebugMode, isDev, isTest } from './env.global'
|
||||
|
||||
class Reporter extends FancyReporter {
|
||||
isInVirtualTerminal = typeof process.stdout.columns === 'undefined' // HACK: if got `undefined` that means in PM2 pty
|
||||
private latestLogTime: number = Date.now()
|
||||
protected formatDate(date: Date): string {
|
||||
if (isDev) {
|
||||
const now = Date.now()
|
||||
const delta = now - this.latestLogTime
|
||||
this.latestLogTime = now
|
||||
return `+${delta | 0}ms ${super.formatDate(date)}`
|
||||
}
|
||||
|
||||
return this.isInVirtualTerminal ? '' : super.formatDate(date)
|
||||
}
|
||||
|
||||
protected formatLogObj(): string {
|
||||
return this.isInVirtualTerminal
|
||||
? `${chalk.gray(getShortTime(new Date()))} ${super.formatLogObj
|
||||
.apply(this, arguments)
|
||||
.replace(/^\n/, '')}`.trimEnd()
|
||||
: super.formatLogObj.apply(this, arguments)
|
||||
}
|
||||
}
|
||||
export const consola = consola_.create({
|
||||
reporters: [new Reporter()],
|
||||
level: isDev || isDebugMode ? LogLevel.Trace : LogLevel.Info,
|
||||
const logger = createLogger({
|
||||
writeToFile: {
|
||||
loggerDir: LOG_DIR,
|
||||
},
|
||||
})
|
||||
logger.wrapAll()
|
||||
logger.onData((data) => {
|
||||
redisSubPub.publish('log', data)
|
||||
})
|
||||
export function registerStdLogger() {
|
||||
let logStream = createWriteStream(getTodayLogFilePath(), {
|
||||
encoding: 'utf-8',
|
||||
flags: 'a+',
|
||||
})
|
||||
|
||||
logStream.write(
|
||||
'\n========================================================\n',
|
||||
)
|
||||
// HACK: forhidden pm2 to override this method
|
||||
Object.defineProperty(process.stdout, 'write', {
|
||||
value: process.stdout.write,
|
||||
writable: false,
|
||||
configurable: false,
|
||||
})
|
||||
Object.defineProperty(process.stderr, 'write', {
|
||||
value: process.stdout.write,
|
||||
writable: false,
|
||||
configurable: false,
|
||||
})
|
||||
|
||||
const job = new CronJob(CronExpression.EVERY_DAY_AT_MIDNIGHT, () => {
|
||||
logStream.destroy()
|
||||
|
||||
logStream = createWriteStream(getTodayLogFilePath(), {
|
||||
encoding: 'utf-8',
|
||||
flags: 'a+',
|
||||
})
|
||||
logStream.write(
|
||||
'\n========================================================\n',
|
||||
)
|
||||
})
|
||||
job.start()
|
||||
|
||||
const stdout = process.stdout.write
|
||||
const stderr = process.stderr.write
|
||||
|
||||
function log(data: string) {
|
||||
if (isTest) {
|
||||
return
|
||||
}
|
||||
logStream.write(data)
|
||||
redisSubPub.publish('log', data)
|
||||
}
|
||||
|
||||
process.stdout.write = function () {
|
||||
log(arguments[0])
|
||||
|
||||
return stdout.apply(this, arguments)
|
||||
}
|
||||
|
||||
process.stderr.write = function () {
|
||||
log(arguments[0])
|
||||
return stderr.apply(this, arguments)
|
||||
}
|
||||
|
||||
consola.wrapAll()
|
||||
// HACK: forhidden pm2 to override this method
|
||||
Object.defineProperty(process.stdout, 'write', {
|
||||
value: process.stdout.write,
|
||||
writable: false,
|
||||
configurable: false,
|
||||
})
|
||||
Object.defineProperty(process.stderr, 'write', {
|
||||
value: process.stdout.write,
|
||||
writable: false,
|
||||
configurable: false,
|
||||
})
|
||||
}
|
||||
export { logger as consola }
|
||||
|
||||
@@ -15,7 +15,7 @@ import {
|
||||
USER_ASSET_DIR,
|
||||
} from '~/constants/path.constant'
|
||||
|
||||
import { consola, registerStdLogger } from './consola.global'
|
||||
import { consola } from './consola.global'
|
||||
|
||||
import './dayjs.global'
|
||||
|
||||
@@ -58,7 +58,7 @@ function registerGlobal() {
|
||||
|
||||
export function register() {
|
||||
registerGlobal()
|
||||
registerStdLogger()
|
||||
|
||||
registerJSONGlobal()
|
||||
|
||||
mkdirs()
|
||||
|
||||
4
apps/core/test/global.d.ts
vendored
4
apps/core/test/global.d.ts
vendored
@@ -1,5 +1,5 @@
|
||||
import type { Consola } from 'consola'
|
||||
import type { Document, PaginateModel } from 'mongoose'
|
||||
import type { WrappedConsola } from 'nestjs-pretty-logger/lib/consola'
|
||||
|
||||
import 'vitest/globals'
|
||||
import 'zx-cjs/globals'
|
||||
@@ -14,7 +14,7 @@ declare global {
|
||||
|
||||
export const isDev: boolean
|
||||
|
||||
export const consola: Consola
|
||||
export const consola: WrappedConsola
|
||||
export const cwd: string
|
||||
|
||||
interface JSON {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { beforeAll } from 'vitest'
|
||||
|
||||
import 'zx/globals'
|
||||
|
||||
import consola from 'consola'
|
||||
import { createConsola } from 'nestjs-pretty-logger'
|
||||
import { dbHelper } from 'test/helper/db-mock.helper'
|
||||
import { redisHelper } from 'test/helper/redis-mock.helper'
|
||||
|
||||
@@ -14,7 +14,7 @@ beforeAll(async () => {
|
||||
|
||||
global.isDev = true
|
||||
global.cwd = process.cwd()
|
||||
global.consola = consola
|
||||
global.consola = createConsola()
|
||||
|
||||
registerJSONGlobal()
|
||||
})
|
||||
|
||||
34
pnpm-lock.yaml
generated
34
pnpm-lock.yaml
generated
@@ -247,6 +247,9 @@ importers:
|
||||
nanoid:
|
||||
specifier: 3.3.4
|
||||
version: 3.3.4
|
||||
nestjs-pretty-logger:
|
||||
specifier: 0.1.0-5
|
||||
version: 0.1.0-5(class-transformer@0.5.1)(class-validator@0.13.2)(reflect-metadata@0.1.13)(rxjs@7.8.1)
|
||||
node-machine-id:
|
||||
specifier: 1.1.12
|
||||
version: 1.1.12
|
||||
@@ -1351,6 +1354,11 @@ packages:
|
||||
- supports-color
|
||||
dev: true
|
||||
|
||||
/@innei/string-width@7.0.0-fork.1:
|
||||
resolution: {integrity: sha512-A9KZC0dExzZnxV/eWkqosFeYPwfR+tN9aZ7oSNQBxg9cJtopGXTqRwZON26TrDjbfDFJurxmG24GE6mpPGjEig==}
|
||||
engines: {node: '>=18'}
|
||||
dev: false
|
||||
|
||||
/@ioredis/commands@1.1.1:
|
||||
resolution: {integrity: sha512-fsR4P/ROllzf/7lXYyElUJCheWdTJVJvOTps8v9IWKFATxR61ANOlnoPqhH099xYLrJGpc2ZQ28B3rMeUt5VQg==}
|
||||
|
||||
@@ -3538,7 +3546,6 @@ packages:
|
||||
dependencies:
|
||||
'@types/luxon': 3.3.3
|
||||
luxon: 3.4.3
|
||||
dev: true
|
||||
|
||||
/cross-env@7.0.3:
|
||||
resolution: {integrity: sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==}
|
||||
@@ -3693,6 +3700,10 @@ packages:
|
||||
has-property-descriptors: 1.0.0
|
||||
object-keys: 1.1.1
|
||||
|
||||
/defu@6.1.3:
|
||||
resolution: {integrity: sha512-Vy2wmG3NTkmHNg/kzpuvHhkqeIx3ODWqasgCRbKtbXEN0G+HpEEv9BtJLp7ZG1CZloFaC41Ah3ZFbq7aqCqMeQ==}
|
||||
dev: false
|
||||
|
||||
/delayed-stream@1.0.0:
|
||||
resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==}
|
||||
engines: {node: '>=0.4.0'}
|
||||
@@ -6449,6 +6460,23 @@ packages:
|
||||
resolution: {integrity: sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==}
|
||||
dev: true
|
||||
|
||||
/nestjs-pretty-logger@0.1.0-5(class-transformer@0.5.1)(class-validator@0.13.2)(reflect-metadata@0.1.13)(rxjs@7.8.1):
|
||||
resolution: {integrity: sha512-LhQNI2I6cBp6ksGDgin7ofkN9pMvBrN0s5IAOqX/wPNH6XZkW4VBaGcOlup26qzcbMK7bch1O+Pgf9SjxrbuJQ==}
|
||||
dependencies:
|
||||
cron: 3.1.6
|
||||
defu: 6.1.3
|
||||
picocolors: 1.0.0
|
||||
std-env: 3.5.0
|
||||
string-width: /@innei/string-width@7.0.0-fork.1
|
||||
optionalDependencies:
|
||||
'@nestjs/common': 10.2.10(class-transformer@0.5.1)(class-validator@0.13.2)(reflect-metadata@0.1.13)(rxjs@7.8.1)
|
||||
transitivePeerDependencies:
|
||||
- class-transformer
|
||||
- class-validator
|
||||
- reflect-metadata
|
||||
- rxjs
|
||||
dev: false
|
||||
|
||||
/new-find-package-json@2.0.0:
|
||||
resolution: {integrity: sha512-lDcBsjBSMlj3LXH2v/FW3txlh2pYTjmbOXPYJD93HI5EwuLzI11tdHSIpUMmfq/IOsldj4Ps8M8flhm+pCK4Ew==}
|
||||
engines: {node: '>=12.22.0'}
|
||||
@@ -7673,6 +7701,10 @@ packages:
|
||||
resolution: {integrity: sha512-f9aPhy8fYBuMN+sNfakZV18U39PbalgjXG3lLB9WkaYTxijru61wb57V9wxxNthXM5Sd88ETBWi29qLAsHO52Q==}
|
||||
dev: true
|
||||
|
||||
/std-env@3.5.0:
|
||||
resolution: {integrity: sha512-JGUEaALvL0Mf6JCfYnJOTcobY+Nc7sG/TemDRBqCA0wEr4DER7zDchaaixTlmOxAjG1uRJmX82EQcxwTQTkqVA==}
|
||||
dev: false
|
||||
|
||||
/stream-combiner@0.0.4:
|
||||
resolution: {integrity: sha512-rT00SPnTVyRsaSz5zgSPma/aHSOic5U1prhYdRy5HS2kTZviFpmDgzilbtsJsxiroqACmayynDN/9VzIbX5DOw==}
|
||||
dependencies:
|
||||
|
||||
Reference in New Issue
Block a user