Skip to content

Commit

Permalink
feat: add builtin logger
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 4, 2024
1 parent 94b114a commit 1efd00b
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 22 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
},
"devDependencies": {
"@cordisjs/eslint-config": "^1.0.4",
"@sinonjs/fake-timers": "^6.0.1",
"@types/chai": "^4.3.11",
"@types/chai-as-promised": "^7.1.8",
"@types/node": "^20.10.2",
"@types/sinonjs__fake-timers": "^6.0.4",
"c8": "^7.14.0",
"chai": "^4.3.10",
"chai-as-promised": "^7.1.1",
Expand Down
9 changes: 5 additions & 4 deletions packages/cordis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"exports": {
".": "./lib/index.js",
"./cli": "./lib/cli.js",
"./logger": "./lib/logger.js",
"./worker": "./lib/worker/index.js",
"./worker/main": "./lib/worker/main.js",
"./src/*": "./src/*",
Expand Down Expand Up @@ -38,11 +39,11 @@
"cli"
],
"dependencies": {
"@cordisjs/core": "^3.8.0",
"@cordisjs/loader": "0.4.0",
"@cordisjs/logger": "^0.1.4",
"@cordisjs/core": "3.8.0",
"@cordisjs/loader": "^0.4.0",
"cac": "^6.7.14",
"cosmokit": "^1.5.2",
"kleur": "^4.1.5"
"kleur": "^4.1.5",
"reggol": "^1.6.3"
}
}
1 change: 0 additions & 1 deletion packages/cordis/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from '@cordisjs/core'
export * from '@cordisjs/loader'
37 changes: 37 additions & 0 deletions packages/cordis/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Context } from '@cordisjs/core'
import Logger from 'reggol'

export { Logger }

declare module '@cordisjs/core' {
interface Context {
baseDir: string
logger: LoggerService
}
}

interface LoggerService {
(name: string): Logger
}

export function apply(ctx: Context) {
ctx.root.baseDir = globalThis.process?.cwd() || ''

ctx.provide('logger', undefined, true)

ctx.logger = function (name: string) {
return new Logger(name, { [Context.current]: this })
}

ctx.on('internal/info', function (format, ...args) {
this.logger('app').info(format, ...args)
})

ctx.on('internal/error', function (format, ...args) {
this.logger('app').error(format, ...args)
})

ctx.on('internal/warning', function (format, ...args) {
this.logger('app').warn(format, ...args)
})
}
2 changes: 1 addition & 1 deletion packages/cordis/src/worker/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function apply(ctx: Context, config: Config = {}) {
if (config.autoRestart) {
process.send!({ type: 'exit' })
}
ctx.logger('app').info(`terminated by ${signal}`)
ctx.emit('internal/info', `terminated by ${signal}`)
ctx.parallel('exit', signal).finally(() => process.exit())
}

Expand Down
6 changes: 1 addition & 5 deletions packages/cordis/src/worker/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { apply as logger, Logger } from '@cordisjs/logger'
import { apply as logger, Logger } from '../logger.js'
import { Context } from '@cordisjs/core'

declare module '@cordisjs/loader' {
Expand Down Expand Up @@ -39,10 +39,6 @@ export function apply(ctx: Context, config: Config = {}) {
new Logger('app').warn(error)
})

ctx.on('loader/update', (type, entry) => {
new Logger('loader').info('%s plugin %c', type, entry.name)
})

ctx.loader.prolog = []

Logger.targets.push({
Expand Down
2 changes: 1 addition & 1 deletion packages/hmr/src/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger } from '@cordisjs/logger'
import { Logger } from 'cordis/logger'
import { BuildFailure } from 'esbuild'
import { codeFrameColumns } from '@babel/code-frame'
import { readFileSync } from 'fs'
Expand Down
2 changes: 1 addition & 1 deletion packages/hmr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Schema from 'schemastery'
import { FSWatcher, watch, WatchOptions } from 'chokidar'
import { relative, resolve } from 'path'
import { handleError } from './error.js'
import { Logger } from '@cordisjs/logger'
import { Logger } from 'cordis/logger'
import {} from '@cordisjs/timer'
import { fileURLToPath, pathToFileURL } from 'url'

Expand Down
3 changes: 1 addition & 2 deletions packages/loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
"service"
],
"devDependencies": {
"@cordisjs/core": "^3.8.0",
"@cordisjs/logger": "^0.1.4"
"@cordisjs/core": "^3.8.0"
},
"peerDependencies": {
"@cordisjs/core": "^3.8.0"
Expand Down
5 changes: 2 additions & 3 deletions packages/loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {} from '@cordisjs/logger'
import Loader from './shared.ts'
import { promises as fs } from 'fs'
import * as dotenv from 'dotenv'
Expand Down Expand Up @@ -54,8 +53,8 @@ class NodeLoader extends Loader<NodeLoader.Options> {
fullReload(code = Loader.exitCode) {
const body = JSON.stringify(this.envData)
process.send?.({ type: 'shared', body }, (err: any) => {
if (err) this.app.logger('loader').error('failed to send shared data')
this.app.logger('loader').info('trigger full reload')
if (err) this.app.emit('internal/error', 'failed to send shared data')
this.app.emit('internal/info', 'trigger full reload')
process.exit(code)
})
}
Expand Down
7 changes: 3 additions & 4 deletions packages/loader/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ declare module '@cordisjs/core' {
interface Events {
'config'(): void
'exit'(signal: NodeJS.Signals): Promise<void>
'loader/update'(this: Context, type: string, entry: Entry): void
}

interface Context {
Expand Down Expand Up @@ -218,7 +217,7 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> {
state.fork.update(entry.config)
} else {
if (!this.isTruthyLike(entry.when)) return
parent.emit('loader/update', 'apply', entry)
parent.emit('internal/info', 'apply plugin %c', entry.name)
const plugin = await this.resolve(entry.name)
if (!plugin) return
const ctx = parent.extend()
Expand All @@ -234,7 +233,7 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> {
unload(parent: Context, entry: Entry) {
const state = this.states[entry.id]
if (state?.fork) {
parent.emit('loader/update', 'unload', entry)
parent.emit('internal/info', 'unload plugin %c', entry.name)
state.fork.dispose()
}
}
Expand Down Expand Up @@ -264,7 +263,7 @@ export abstract class Loader<T extends Loader.Options = Loader.Options> {
this.app.on('internal/update', (fork) => {
const state = this.states[fork.id!]
if (!state) return
fork.parent.emit('loader/update', 'reload', state.entry)
fork.parent.emit('internal/info', 'reload plugin %c', state.entry.name)
})

this.app.on('internal/before-update', (fork, config) => {
Expand Down

0 comments on commit 1efd00b

Please sign in to comment.