fix(packages/shared): fix weird log dates and clean up imports

This commit is contained in:
PalmDevs
2024-01-14 21:52:07 +07:00
parent 41c59eba03
commit ee5ffa6f24
10 changed files with 38 additions and 26 deletions

View File

@@ -1,4 +1,4 @@
import DisconnectReason from './DisconnectReason.js' import DisconnectReason from './DisconnectReason'
/** /**
* Humanized disconnect reasons for logs * Humanized disconnect reasons for logs

View File

@@ -1,3 +1,3 @@
export { default as DisconnectReason } from './DisconnectReason.js' export { default as DisconnectReason } from './DisconnectReason'
export { default as HumanizedDisconnectReason } from './HumanizedDisconnectReason.js' export { default as HumanizedDisconnectReason } from './HumanizedDisconnectReason'
export * from './Operation.js' export * from './Operation'

View File

@@ -1,3 +1,3 @@
export * from './constants/index.js' export * from './constants/index'
export * from './schemas/index.js' export * from './schemas/index'
export * from './utils/index.js' export * from './utils/index'

View File

@@ -14,8 +14,8 @@ import {
string, string,
// merge // merge
} from 'valibot' } from 'valibot'
import DisconnectReason from '../constants/DisconnectReason.js' import DisconnectReason from '../constants/DisconnectReason'
import { ClientOperation, Operation, ServerOperation } from '../constants/Operation.js' import { ClientOperation, Operation, ServerOperation } from '../constants/Operation'
/** /**
* Schema to validate packets * Schema to validate packets
@@ -90,5 +90,5 @@ export const PacketDataSchemas = {
export type Packet<TOp extends Operation = Operation> = { export type Packet<TOp extends Operation = Operation> = {
op: TOp op: TOp
d: Output<typeof PacketDataSchemas[TOp]> d: Output<(typeof PacketDataSchemas)[TOp]>
} }

View File

@@ -1 +1 @@
export * from './Packet.js' export * from './Packet'

View File

@@ -0,0 +1,13 @@
export function getMissingEnvironmentVariables(keys: string[]) {
return keys.filter(key => !process.env[key])
}
export function getEnvironmentType() {
const environmentValue = process.env['NODE_ENV']
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/26255
if (!NodeEnvironments.includes(environmentValue)) return null
return process.env['NODE_ENV'] as NodeEnvironment
}
const NodeEnvironments = ['development', 'production'] as const
export type NodeEnvironment = (typeof NodeEnvironments)[number]

View File

@@ -1,5 +1,5 @@
import { ClientOperation, Operation, ServerOperation } from '../constants/Operation.js' import { ClientOperation, Operation, ServerOperation } from '../constants/Operation'
import { Packet } from '../schemas/Packet.js' import { Packet } from '../schemas/Packet'
/** /**
* Checks whether a packet is trying to do the given operation * Checks whether a packet is trying to do the given operation

View File

@@ -1,4 +1,5 @@
export * from './guard.js' export * from './environment'
export * from './logger.js' export * from './guard'
export * from './serialization.js' export * from './logger'
export * from './string.js' export * from './serialization'
export * from './string'

View File

@@ -1,15 +1,13 @@
import { colorConsole, console as uncoloredConsole, Tracer } from 'tracer'
import { Chalk, supportsColor, supportsColorStderr } from 'chalk' import { Chalk, supportsColor, supportsColorStderr } from 'chalk'
import { console as uncoloredConsole, Tracer, colorConsole } from 'tracer'
const chalk = new Chalk() const chalk = new Chalk()
const DefaultConfig = { const DefaultConfig = {
dateformat: 'DD/MM/YYYY HH:mm:ss.sss Z', dateformat: 'dd/mm/yyyy HH:mm:ss.sss Z',
format: [ format: [
'{{message}}', '{{message}}',
{ {
error: `${chalk.bgRedBright.whiteBright(' ERROR ')} {{message}}\n${chalk.gray( error: `${chalk.bgRedBright.whiteBright(' ERROR ')} {{message}}\n${chalk.gray('{{stack}}')}`,
'{{stack}}',
)}`,
debug: chalk.gray('DEBUG: {{message}}\n{{stack}}'), debug: chalk.gray('DEBUG: {{message}}\n{{stack}}'),
warn: `${chalk.bgYellowBright.whiteBright(' WARN ')} ${chalk.yellowBright('{{message}}')}\n${chalk.gray( warn: `${chalk.bgYellowBright.whiteBright(' WARN ')} ${chalk.yellowBright('{{message}}')}\n${chalk.gray(
'{{stack}}', '{{stack}}',
@@ -26,7 +24,7 @@ const DefaultConfig = {
filters: [], filters: [],
} satisfies Tracer.LoggerConfig } satisfies Tracer.LoggerConfig
export function createLogger(config: Omit<Tracer.LoggerConfig, keyof typeof DefaultConfig>) { export function createLogger(config?: Omit<Tracer.LoggerConfig, keyof typeof DefaultConfig>) {
const combinedConfig = { ...DefaultConfig, ...config } const combinedConfig = { ...DefaultConfig, ...config }
if ( if (
@@ -37,7 +35,7 @@ export function createLogger(config: Omit<Tracer.LoggerConfig, keyof typeof Defa
supportsColorStderr.hasBasic supportsColorStderr.hasBasic
) )
return colorConsole(combinedConfig) return colorConsole(combinedConfig)
else return uncoloredConsole(combinedConfig) return uncoloredConsole(combinedConfig)
} }
export type Logger = ReturnType<typeof createLogger> export type Logger = ReturnType<typeof createLogger>

View File

@@ -1,7 +1,7 @@
import * as BSON from 'bson' import * as BSON from 'bson'
import { parse } from 'valibot' import { parse } from 'valibot'
import { Operation } from '../constants/index.js' import { Operation } from '../constants/index'
import { Packet, PacketSchema } from '../schemas/index.js' import { Packet, PacketSchema } from '../schemas/index'
/** /**
* Compresses a packet into a buffer * Compresses a packet into a buffer