From 07d332022538b2106b1925cea3aa1fc988cd5cc5 Mon Sep 17 00:00:00 2001 From: deptyped Date: Fri, 17 May 2024 01:17:59 +0300 Subject: [PATCH] Add Bun example --- README.md | 22 +++++++++++----------- package.json | 6 +++--- src/config.ts | 14 +++----------- src/main.ts | 5 +---- src/server/index.ts | 30 ++++++++++++------------------ 5 files changed, 30 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index ca53bb61..eaa0c040 100644 --- a/README.md +++ b/README.md @@ -48,18 +48,18 @@ Follow these steps to set up and run your bot using this template: Install the required dependencies: ```bash - npm install + bun install ``` Start the bot in watch mode (auto-reload when code changes): ```bash - npm run dev + bun dev ``` **Production Mode:** Install only production dependencies (no development dependencies): ```bash - npm install --only=prod + bun install --only=prod ``` Set `NODE_ENV` environment variable to `production` in your `.env` file.
@@ -74,19 +74,19 @@ Follow these steps to set up and run your bot using this template: Start the bot in production mode: ```bash - npm start # with type checking (requires development dependencies) + bun start # with type checking (requires development dependencies) # or - npm run start:force # skip type checking and start + bun start:force # skip type checking and start ``` ### List of Available Commands -- `npm run lint` — Lint source code. -- `npm run format` — Format source code. -- `npm run typecheck` — Run type checking. -- `npm run dev` — Start the bot in development mode. -- `npm run start` — Start the bot. -- `npm run start:force` — Starts the bot without type checking. +- `bun lint` — Lint source code. +- `bun format` — Format source code. +- `bun typecheck` — Run type checking. +- `bun dev` — Start the bot in development mode. +- `bun start` — Start the bot. +- `bun start:force` — Starts the bot without type checking. ### Directory Structure diff --git a/package.json b/package.json index 8513993c..c3c6e1c4 100644 --- a/package.json +++ b/package.json @@ -18,9 +18,9 @@ "format": "eslint . --fix", "typecheck": "tsc", "build": "tsc --noEmit false", - "dev": "tsc-watch --onSuccess \"tsx ./src/main.ts\"", - "start": "tsc && tsx ./src/main.ts", - "start:force": "tsx ./src/main.ts", + "dev": "bun --watch ./src/main.ts", + "start": "tsc && bun ./src/main.ts", + "start:force": "bun ./src/main.ts", "prepare": "husky || true" }, "dependencies": { diff --git a/src/config.ts b/src/config.ts index 0ff44397..f2ceb2d5 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,15 +1,7 @@ -import process from 'node:process' import z from 'zod' import { parseEnv, port } from 'znv' import { API_CONSTANTS } from 'grammy' -try { - process.loadEnvFile() -} -catch { - // No .env file found -} - function createConfigFromEnvironment(environment: NodeJS.ProcessEnv) { const config = parseEnv(environment, { NODE_ENV: z.enum(['development', 'production']), @@ -51,11 +43,11 @@ function createConfigFromEnvironment(environment: NodeJS.ProcessEnv) { return { ...config, - isDev: process.env.NODE_ENV === 'development', - isProd: process.env.NODE_ENV === 'production', + isDev: Bun.env.NODE_ENV === 'development', + isProd: Bun.env.NODE_ENV === 'production', } } export type Config = ReturnType -export const config = createConfigFromEnvironment(process.env) +export const config = createConfigFromEnvironment(Bun.env) diff --git a/src/main.ts b/src/main.ts index 7b677b46..bf1fd467 100644 --- a/src/main.ts +++ b/src/main.ts @@ -58,10 +58,7 @@ async function startWebhook() { ) logger.info({ msg: 'Server started', - url: - info.family === 'IPv6' - ? `http://[${info.address}]:${info.port}` - : `http://${info.address}:${info.port}`, + url: info.url, }) // set webhook diff --git a/src/server/index.ts b/src/server/index.ts index 4fadd096..1579dede 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -59,25 +59,19 @@ export function createServer(bot: Bot) { export type Server = Awaited> export function createServerManager(server: Server) { - let handle: undefined | ReturnType + let handle: undefined | ReturnType return { - start: (host: string, port: number) => - new Promise((resolve) => { - handle = serve( - { - fetch: server.fetch, - hostname: host, - port, - }, - info => resolve(info), - ) - }), + start: (host: string, port: number) => { + handle = Bun.serve({ + fetch: server.fetch, + hostname: host, + port, + }) + return { + url: handle.url, + } + }, stop: () => - new Promise((resolve) => { - if (handle) - handle.close(() => resolve()) - else - resolve() - }), + handle?.stop(), } }