diff --git a/.github/images/screenshot.png b/.github/images/screenshot.png index 2525605..0756560 100644 Binary files a/.github/images/screenshot.png and b/.github/images/screenshot.png differ diff --git a/README.md b/README.md index 1ad4268..5f71330 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,7 @@ const app = new Elysia({ name: 'Logixlysia Example' }).use(logger()) -console.log( - `๐Ÿงช Listening on http://${app.server!.hostname}:${app.server!.port}` -) +app.listen(3000) ``` ## `๐Ÿ“„` License diff --git a/example/basic.ts b/example/basic.ts index ff57d8a..4e6c754 100644 --- a/example/basic.ts +++ b/example/basic.ts @@ -30,8 +30,5 @@ const app = new Elysia({ message: 'Basic Example' } }) - .listen(3000) -console.log( - `๐Ÿงช Listening on http://${app.server!.hostname}:${app.server!.port}` -) +app.listen(3000) diff --git a/package.json b/package.json index d42514e..fb3a19e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "logixlysia", - "version": "1.0.1", + "version": "1.1.0", "description": "๐ŸฆŠ Logixlysia is a logger for Elysia", "type": "module", "module": "src/index.ts", diff --git a/src/index.ts b/src/index.ts index 565b617..c0d090b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,7 @@ import Elysia from 'elysia' import { formatLogger } from './logger' +import { startString } from './utils/start' +import { Server } from 'bun' export const logger = () => { const log = formatLogger() @@ -7,6 +9,9 @@ export const logger = () => { const elysia = new Elysia({ name: 'Logixlysia' }) + .onStart(ctx => { + startString(ctx.app.server as Server) + }) .onRequest(ctx => { ctx.store = { beforeTime: process.hrtime.bigint() } as { beforeTime: bigint diff --git a/src/utils/start.ts b/src/utils/start.ts new file mode 100644 index 0000000..e352c93 --- /dev/null +++ b/src/utils/start.ts @@ -0,0 +1,34 @@ +interface Server { + hostname?: string + port?: number + protocol?: string +} + +function createBoxText(text: string, width: number): string { + const padding = ' '.repeat((width - text.length) / 2) + return `${padding}${text}${padding}` +} + +function startString(config: Server): void { + const { hostname, port, protocol } = config + const ELYSIA_VERSION = import.meta.require('elysia/package.json').version + const title = `Elysia v${ELYSIA_VERSION}` + const message = `๐ŸฆŠ Elysia is running at ${protocol}://${hostname}:${port}` + const messageWidth = message.length + const boxWidth = Math.max(title.length, messageWidth) + 4 + const border = 'โ”€'.repeat(boxWidth) + + process.stdout.write('\x1Bc') + + console.log(` + โ”Œ${border}โ” + โ”‚${createBoxText('', boxWidth)} โ”‚ + โ”‚${createBoxText(title, boxWidth)} โ”‚ + โ”‚${createBoxText('', boxWidth)} โ”‚ + โ”‚${createBoxText(message, boxWidth)}โ”‚ + โ”‚${createBoxText('', boxWidth)} โ”‚ + โ””${border}โ”˜ + `) +} + +export { Server, startString }