From 5e03ef2dd95741781da6b4f2d5ec2eac9c2670f4 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Wed, 30 Apr 2025 19:27:31 +0800 Subject: [PATCH] temp --- .github/workflows/git-sync.yml | 12 ++++-- bun.config.mjs | 20 +++++++++ package.json | 26 ++---------- rollup.config.mjs | 75 ---------------------------------- src/dev.ts | 4 +- src/modules/redis.ts | 57 ++++++++++++++++---------- src/modules/sequelize.ts | 19 +++++---- 7 files changed, 82 insertions(+), 131 deletions(-) create mode 100644 bun.config.mjs delete mode 100644 rollup.config.mjs diff --git a/.github/workflows/git-sync.yml b/.github/workflows/git-sync.yml index 9a00356..ac08ba8 100644 --- a/.github/workflows/git-sync.yml +++ b/.github/workflows/git-sync.yml @@ -1,5 +1,11 @@ name: Sync to CNB -on: [push] +on: + push: + branches: + # - main + - 'releases/**' + workflow_dispatch: + # This workflow is triggered on push events to the repository. jobs: sync: @@ -8,7 +14,7 @@ jobs: - uses: actions/checkout@v4 with: fetch-depth: 0 - + - name: Sync to CNB Repository run: | docker run --rm \ @@ -19,4 +25,4 @@ jobs: -e PLUGIN_USERNAME="cnb" \ -e PLUGIN_PASSWORD=${{ secrets.GIT_PASSWORD }} \ -e PLUGIN_SYNC_MODE="rebase" \ - tencentcom/git-sync \ No newline at end of file + tencentcom/git-sync diff --git a/bun.config.mjs b/bun.config.mjs new file mode 100644 index 0000000..27d12c4 --- /dev/null +++ b/bun.config.mjs @@ -0,0 +1,20 @@ +// @ts-check +// https://bun.sh/docs/bundler +// @ts-ignore +import pkg from './package.json'; +import { resolvePath as rp } from '@kevisual/use-config/env'; +// bun run src/index.ts -- +await Bun.build({ + target: 'node', + format: 'esm', + entrypoints: [rp('./src/main.ts')], + outdir: rp('./dist'), + naming: { + entry: 'app.mjs', + }, + + define: { + VERSION: JSON.stringify(pkg.version), + }, + env: 'KEVISUAL_*', +}); diff --git a/package.json b/package.json index e4e68f1..5560800 100644 --- a/package.json +++ b/package.json @@ -13,11 +13,8 @@ ] }, "scripts": { - "watch": "rollup -c rollup.config.mjs -w", - "build": "rollup -c rollup.config.mjs", - "dev": "cross-env NODE_ENV=development nodemon --delay 2.5 -e js,cjs,mjs --exec node dist/app.mjs", - "dev:watch": "cross-env NODE_ENV=development concurrently -n \"Watch,Dev\" -c \"green,blue\" \"npm run watch\" \"sleep 1 && npm run dev\" ", - "bun:dev": "bun run dev", + "dev": "cross-env NODE_TLS_REJECT_UNAUTHORIZED=0 bun --watch src/dev.ts ", + "build": "rimraf dist && bun run bun.config.mjs", "test": "tsx test/**/*.ts", "clean": "rm -rf dist", "pub": "npm run build && envision pack -p -u", @@ -37,9 +34,8 @@ }, "dependencies": { "@kevisual/code-center-module": "0.0.18", - "@kevisual/use-config": "^1.0.12", - "@kevisual/mark": "0.0.7", "@kevisual/router": "0.0.13", + "@kevisual/use-config": "^1.0.12", "cookie": "^1.0.2", "dayjs": "^1.11.13", "formidable": "^3.5.4", @@ -48,12 +44,7 @@ "devDependencies": { "@kevisual/types": "^0.0.9", "@kevisual/use-config": "^1.0.12", - "@rollup/plugin-alias": "^5.1.1", - "@rollup/plugin-commonjs": "^28.0.3", - "@rollup/plugin-json": "^6.1.0", - "@rollup/plugin-node-resolve": "^16.0.1", - "@rollup/plugin-replace": "^6.0.2", - "@rollup/plugin-typescript": "^12.1.2", + "@types/bun": "^1.2.11", "@types/crypto-js": "^4.2.2", "@types/formidable": "^3.4.5", "@types/lodash-es": "^4.17.12", @@ -63,20 +54,11 @@ "cross-env": "^7.0.3", "inquire": "^0.4.8", "ioredis": "^5.6.1", - "jsrepo": "^1.47.1", "nodemon": "^3.1.10", "pg": "^8.15.6", - "pino": "^9.6.0", - "pino-pretty": "^13.0.0", - "pm2": "^6.0.5", "rimraf": "^6.0.1", - "rollup": "^4.40.1", - "rollup-plugin-copy": "^3.5.0", - "rollup-plugin-dts": "^6.2.1", - "rollup-plugin-esbuild": "^6.2.1", "sequelize": "^6.37.7", "tape": "^5.9.0", - "tsx": "^4.19.4", "typescript": "^5.8.3" }, "packageManager": "pnpm@10.10.0" diff --git a/rollup.config.mjs b/rollup.config.mjs deleted file mode 100644 index 7583dfe..0000000 --- a/rollup.config.mjs +++ /dev/null @@ -1,75 +0,0 @@ -import resolve from '@rollup/plugin-node-resolve'; -import commonjs from '@rollup/plugin-commonjs'; -import json from '@rollup/plugin-json'; -import path from 'path'; -import esbuild from 'rollup-plugin-esbuild'; -import alias from '@rollup/plugin-alias'; -import replace from '@rollup/plugin-replace'; -import pkgs from './package.json' with {type: 'json'}; - -const isDev = process.env.NODE_ENV === 'development'; -const input = isDev ? './src/dev.ts' : './src/main.ts'; -/** - * @type {import('rollup').RollupOptions} - */ -const config = { - input, - output: { - dir: './dist', - entryFileNames: 'app.mjs', - chunkFileNames: '[name]-[hash].mjs', - format: 'esm', - }, - plugins: [ - replace({ - preventAssignment: true, // 防止意外赋值 - DEV_SERVER: JSON.stringify(isDev), // 替换 process.env.NODE_ENV - APP_VERSION: JSON.stringify(pkgs.version), - }), - alias({ - // only esbuild needs to be configured - entries: [ - { find: '@', replacement: path.resolve('src') }, // 配置 @ 为 src 目录 - { find: 'http', replacement: 'node:http' }, - { find: 'https', replacement: 'node:https' }, - { find: 'fs', replacement: 'node:fs' }, - { find: 'path', replacement: 'node:path' }, - { find: 'crypto', replacement: 'node:crypto' }, - { find: 'zlib', replacement: 'node:zlib' }, - { find: 'stream', replacement: 'node:stream' }, - { find: 'net', replacement: 'node:net' }, - { find: 'tty', replacement: 'node:tty' }, - { find: 'tls', replacement: 'node:tls' }, - { find: 'buffer', replacement: 'node:buffer' }, - { find: 'timers', replacement: 'node:timers' }, - // { find: 'string_decoder', replacement: 'node:string_decoder' }, - { find: 'dns', replacement: 'node:dns' }, - { find: 'domain', replacement: 'node:domain' }, - { find: 'os', replacement: 'node:os' }, - { find: 'events', replacement: 'node:events' }, - { find: 'url', replacement: 'node:url' }, - { find: 'assert', replacement: 'node:assert' }, - { find: 'util', replacement: 'node:util' }, - ], - }), - resolve({ - preferBuiltins: true, // 强制优先使用内置模块 - }), - commonjs(), - esbuild({ - target: 'node22', // - minify: false, // 启用代码压缩 - tsconfig: 'tsconfig.json', - }), - json(), - ], - external: [ - /@kevisual\/router(\/.*)?/, //, // 路由 - /@kevisual\/use-config(\/.*)?/, // - - 'sequelize', // 数据库 orm - 'ioredis', // redis - 'pg', // pg - ], -}; -export default config; diff --git a/src/dev.ts b/src/dev.ts index c3e9636..f677f6e 100644 --- a/src/dev.ts +++ b/src/dev.ts @@ -1,6 +1,5 @@ import { app } from './index.ts'; -import { useConfig } from '@kevisual/use-config/env'; - +import { config } from './modules/config.ts'; app .route({ path: 'auth', @@ -38,7 +37,6 @@ app }) .addTo(app); -const config = useConfig(); const port = config.PORT || 4000; console.log('run demo: http://localhost:' + port + '/api/router?path=demo&key=demo'); diff --git a/src/modules/redis.ts b/src/modules/redis.ts index 399f9af..8a8eccc 100644 --- a/src/modules/redis.ts +++ b/src/modules/redis.ts @@ -1,27 +1,42 @@ import { Redis } from 'ioredis'; +import { config } from './config.ts'; +type initRedisOpts = { + onConnect?: () => void; // 连接成功的回调函数 + onError?: (msg?: any) => void; // 连接断开的回调函数 +}; +const initRedis = (config?: any, options?: initRedisOpts) => { + const redis = new Redis({ + host: config?.REDIS_HOST || 'localhost', // Redis 服务器的主机名或 IP 地址 + port: config?.REDIS_PORT || 6379, // Redis 服务器的端口号 + // password: 'your_password', // Redis 的密码 (如果有) + db: 0, // 要使用的 Redis 数据库索引 (0-15) + keyPrefix: '', // key 前缀 + retryStrategy(times) { + // 连接重试策略 + return Math.min(times * 50, 2000); // 每次重试时延迟增加 + }, + maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试) + ...config, // 其他配置项 + }); + // 监听连接事件 + redis.on('connect', () => { + if (options?.onConnect) { + options.onConnect(); + console.log('Redis 连接成功'); + } + }); + + redis.on('error', (err) => { + console.error('Redis 连接错误', err); + if (options?.onError) { + options.onError(err); + } + }); + return redis; +}; // 配置 Redis 连接 -export const redis = new Redis({ - host: 'localhost', // Redis 服务器的主机名或 IP 地址 - port: 6379, // Redis 服务器的端口号 - // password: 'your_password', // Redis 的密码 (如果有) - db: 0, // 要使用的 Redis 数据库索引 (0-15) - keyPrefix: '', // key 前缀 - retryStrategy(times) { - // 连接重试策略 - return Math.min(times * 50, 2000); // 每次重试时延迟增加 - }, - maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试) -}); - -// 监听连接事件 -redis.on('connect', () => { - console.log('Redis 连接成功'); -}); - -redis.on('error', (err) => { - console.error('Redis 连接错误', err); -}); +export const redis = useConfigKey('redis', () => initRedis(config)); // 初始化 Redis 客户端 export const redisPublisher = new Redis(); // 用于发布消息 diff --git a/src/modules/sequelize.ts b/src/modules/sequelize.ts index 27fe926..0a7daa6 100644 --- a/src/modules/sequelize.ts +++ b/src/modules/sequelize.ts @@ -1,5 +1,6 @@ import { Sequelize } from 'sequelize'; -import { config } from './config'; +import { config } from './config.ts'; +import { useContextKey } from '@kevisual/use-config/context'; export type PostgresConfig = { postgres: { @@ -21,9 +22,13 @@ const postgresConfig = { port: parseInt(config.POSTGRES_PORT || '5432'), database: config.POSTGRES_DB || 'postgres', }; -// connect to db -export const sequelize = new Sequelize({ - dialect: 'postgres', - ...postgresConfig, - // logging: false, -}); + +export const init = async () => { + return new Sequelize({ + dialect: 'postgres', + ...postgresConfig, + // logging: false, + }); +}; + +export const sequelize = useContextKey('sequelize', () => init());