diff --git a/app.js b/app.js index 1c51332..4b757b4 100644 --- a/app.js +++ b/app.js @@ -1 +1,16 @@ -import './index.js' +import { execSync, spawn } from 'child_process' +execSync('tailwindcss -i ./src/input.css -o ./public/output.css') +const argv = [...process.argv].splice(2) +const argvs = argv.join(' ').replace(/(\S+\.js|\S+\.ts)/g, '') +const child = spawn( + 'node --no-warnings=ExperimentalWarning --loader ts-node/esm src/main.ts', + argvs.split(' '), + { + shell: true, + stdio: 'inherit' + } +) +process.on('SIGINT', () => { + if (child.pid) process.kill(child.pid) + if (process.pid) process.exit() +}) diff --git a/docker-compose.yaml b/docker-compose.yaml deleted file mode 100644 index a697a67..0000000 --- a/docker-compose.yaml +++ /dev/null @@ -1,51 +0,0 @@ -version: "3" -services: - miao-yunzai: - container_name: miao-yunzai - build: # 使用 Dockerfile 本地构建 - context: ./docker - args: - BUNDLE_FFMPEG: 'false' # 是否在构建时打包 ffmpeg - BUNDLE_POETRY: 'false' # 是否在构建时打包 poetry - USE_APT_MIRROR: 'true' # 是否在构建时使用 apt 镜像 - USE_NPM_MIRROR: 'true' # 是否在构建时使用 npm 镜像 - USE_PYPI_MIRROR: 'true' # 是否在构建时使用 pypi 镜像 - restart: always - # ports: - # - "50831:50831" # 映射锅巴插件端口,格式"主机端口:容器内部端口" - volumes: - - ./yunzai/config/:/app/Miao-Yunzai/config/config/ # Bot基础配置文件 - - ./yunzai/genshin_config:/app/Miao-Yunzai/plugins/genshin/config # 公共Cookie,云崽功能配置文件 - - ./yunzai/logs:/app/Miao-Yunzai/logs # 日志文件 - - ./yunzai/data:/app/Miao-Yunzai/data # 数据文件 - - ./yunzai/temp:/app/Miao-Yunzai/temp # 临时文件 - # 以下目录是插件目录,安装完插件后需要手动添加映射(如有其他插件请自行添加) - # 映射格式:./yunzai/plugins/<插件目录名>:/app/Miao-Yunzai/plugins/<插件目录名> - # - ./yunzai/plugins/example:/app/Miao-Yunzai/plugins/example # 单js插件目录 - - ./yunzai/plugins/miao-plugin:/app/Miao-Yunzai/plugins/miao-plugin # 默认映射喵喵插件 - # - ./yunzai/plugins/py-plugin:/app/Miao-Yunzai/plugins/py-plugin # 新py插件 - # - ./yunzai/plugins/xiaoyao-cvs-plugin:/app/Miao-Yunzai/plugins/xiaoyao-cvs-plugin # 图鉴插件 - #### [警告] 受云崽架构和docker特性限制,使用锅巴插件安装的插件无法持久化,销毁容器后新安装的插件会消失,请谨慎使用 #### - # - ./yunzai/plugins/Guoba-Plugin:/app/Miao-Yunzai/plugins/Guoba-Plugin # 锅巴插件 - depends_on: - redis: { condition: service_healthy } - deploy: - resources: - limits: - memory: 2G - - redis: - container_name: miao-redis - image: redis:alpine - restart: always - volumes: - # 前往 https://download.redis.io/redis-stable/redis.conf 下载配置文件,放入 ./redis/config 文件夹中 - # - ./redis/config:/etc/redis/ # Redis配置文件 - - ./redis/data:/data - - ./redis/logs:/logs - # command: /etc/redis/redis.conf # 取消注释以应用Redis配置文件 - healthcheck: - test: [ "CMD", "redis-cli", "PING" ] - start_period: 10s - interval: 5s - timeout: 1s diff --git a/docker/Dockerfile b/docker/Dockerfile deleted file mode 100644 index 71f9087..0000000 --- a/docker/Dockerfile +++ /dev/null @@ -1,94 +0,0 @@ -FROM busybox:latest AS resource - -COPY docker-entrypoint.sh /res/entrypoint.sh - -RUN dos2unix /res/entrypoint.sh \ - && chmod +x /res/entrypoint.sh - - -FROM node:lts-bullseye-slim AS runtime - -ARG BUNDLE_FFMPEG true -ARG BUNDLE_POETRY true -ARG USE_APT_MIRROR true -ARG USE_NPM_MIRROR true -ARG USE_PYPI_MIRROR true -ARG REPO_URL https://gitee.com/yoimiya-kokomi/Miao-Yunzai.git -ARG REPO_BRANCH master - -RUN export BUNDLE_FFMPEG=${BUNDLE_FFMPEG:-true} \ - && export BUNDLE_POETRY=${BUNDLE_POETRY:-true} \ - && export USE_APT_MIRROR=${USE_APT_MIRROR:-true} \ - && export USE_NPM_MIRROR=${USE_NPM_MIRROR:-true} \ - && export USE_PYPI_MIRROR=${USE_PYPI_MIRROR:-true} \ - \ - && ((test "$USE_APT_MIRROR"x = "true"x \ - && sed -i "s/deb.debian.org/mirrors.ustc.edu.cn/g" /etc/apt/sources.list) || true) \ - && apt-get update \ - && apt-get upgrade -y \ - && apt-get install -y wget xz-utils dos2unix \ - && ((test "$BUNDLE_FFMPEG"x = "true"x \ - && wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-$(dpkg --print-architecture)-static.tar.xz \ - && mkdir -p /res/ffmpeg \ - && tar -xvf ./ffmpeg-git-$(dpkg --print-architecture)-static.tar.xz -C /res/ffmpeg --strip-components 1 \ - && cp /res/ffmpeg/ffmpeg /usr/bin/ffmpeg \ - && cp /res/ffmpeg/ffprobe /usr/bin/ffprobe) || true) \ - \ - && apt-get update \ - && apt-get upgrade -y \ - && apt-get install -y curl wget gnupg git fonts-wqy-microhei xfonts-utils chromium fontconfig libxss1 libgl1 \ - && apt-get autoremove \ - && apt-get clean \ - \ - && fc-cache -f -v \ - \ - && git config --global --add safe.directory '*' \ - && git config --global pull.rebase false \ - && git config --global user.email "Yunzai@yunzai.bot" \ - && git config --global user.name "Yunzai" \ - \ - && _NPM_MIRROR_FLAG="" \ - && if [ "$USE_NPM_MIRROR"x = "true"x ]; then _NPM_MIRROR_FLAG="--registry=https://registry.npmmirror.com"; fi \ - && npm install pnpm -g $_NPM_MIRROR_FLAG \ - \ - && ((test "$BUNDLE_POETRY"x = "true"x \ - && apt-get update \ - && apt-get install -y python3-pip python3-venv \ - && apt-get autoremove \ - && apt-get clean \ - && ln -s /usr/bin/python3 /usr/bin/python \ - && POETRY_HOME=$HOME/venv-poetry \ - && python -m venv $POETRY_HOME \ - && _PYPI_MIRROR_FLAG="" \ - && if [ "$USE_PYPI_MIRROR"x = "true"x ]; then _PYPI_MIRROR_FLAG="-i https://pypi.tuna.tsinghua.edu.cn/simple"; fi \ - && $POETRY_HOME/bin/pip install --upgrade pip setuptools $_PYPI_MIRROR_FLAG \ - && $POETRY_HOME/bin/pip install poetry $_PYPI_MIRROR_FLAG \ - && ln -s $POETRY_HOME/bin/poetry /usr/bin \ - && poetry config virtualenvs.in-project true) || true) \ - \ - && rm -rf /var/cache/* \ - && rm -rf /tmp/* - - -FROM runtime AS prod - -ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \ - PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium - -RUN REPO_URL=${REPO_URL:-"https://gitee.com/yoimiya-kokomi/Miao-Yunzai.git"} \ - && REPO_BRANCH=${REPO_BRANCH:-master} \ - && USE_NPM_MIRROR=${USE_NPM_MIRROR:-true} \ - \ - && _NPM_MIRROR_FLAG="" \ - && if [ "$USE_NPM_MIRROR"x = "true"x ]; then _NPM_MIRROR_FLAG="--registry=https://registry.npmmirror.com"; fi \ - && git clone --depth=1 --branch $REPO_BRANCH $REPO_URL /app/Miao-Yunzai \ - && cd /app/Miao-Yunzai \ - && sed -i 's/127.0.0.1/redis/g' ./config/default_config/redis.yaml \ - && pnpm install -P $_NPM_MIRROR_FLAG \ - && git remote set-url origin https://gitee.com/yoimiya-kokomi/Miao-Yunzai.git - -COPY --from=resource /res/entrypoint.sh /app/Miao-Yunzai/entrypoint.sh - -WORKDIR /app/Miao-Yunzai - -ENTRYPOINT ["/app/Miao-Yunzai/entrypoint.sh"] diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh deleted file mode 100644 index 022bdc2..0000000 --- a/docker/docker-entrypoint.sh +++ /dev/null @@ -1,194 +0,0 @@ -#!/usr/bin/env bash - -set +e - -GreenBG="\\033[42;37m" -YellowBG="\\033[43;37m" -BlueBG="\\033[44;37m" -Font="\\033[0m" - -Version="${BlueBG}[版本]${Font}" -Info="${GreenBG}[信息]${Font}" -Warn="${YellowBG}[提示]${Font}" - -WORK_DIR="/app/Miao-Yunzai" -MIAO_PLUGIN_PATH="/app/Miao-Yunzai/plugins/miao-plugin" -XIAOYAO_CVS_PATH="/app/Miao-Yunzai/plugins/xiaoyao-cvs-plugin" -PY_PLUGIN_PATH="/app/Miao-Yunzai/plugins/py-plugin" - -if [[ ! -d "$HOME/.ovo" ]]; then - mkdir ~/.ovo -fi - -echo -e "\n ================ \n ${Info} ${GreenBG} 拉取 Miao-Yunzai 更新 ${Font} \n ================ \n" - -cd $WORK_DIR - -if [[ -z $(git status -s) ]]; then - echo -e " ${Warn} ${YellowBG} 当前工作区有修改,尝试暂存后更新。${Font}" - git add . - git stash - git pull origin master --allow-unrelated-histories --rebase - git stash pop -else - git pull origin master --allow-unrelated-histories -fi - -if [[ ! -f "$HOME/.ovo/yunzai.ok" ]]; then - set -e - echo -e "\n ================ \n ${Info} ${GreenBG} 更新 Miao-Yunzai 运行依赖 ${Font} \n ================ \n" - pnpm install -P - touch ~/.ovo/yunzai.ok - set +e -fi - -echo -e "\n ================ \n ${Version} ${BlueBG} Miao-Yunzai 版本信息 ${Font} \n ================ \n" - -git log -1 --pretty=format:"%h - %an, %ar (%cd) : %s" - -if [ ! -d $MIAO_PLUGIN_PATH"/.git" ]; then - echo -e "\n ${Warn} ${YellowBG} 由于喵版云崽依赖miao-plugin,检测到目前没有安装,开始自动下载 ${Font} \n" - git clone --depth=1 https://gitee.com/yoimiya-kokomi/miao-plugin.git ./plugins/miao-plugin/ -fi - - -if [ -d $MIAO_PLUGIN_PATH"/.git" ]; then - - echo -e "\n ================ \n ${Info} ${GreenBG} 拉取 喵喵插件 更新 ${Font} \n ================ \n" - - cd $MIAO_PLUGIN_PATH - - if [[ -n $(git status -s) ]]; then - echo -e " ${Warn} ${YellowBG} 当前工作区有修改,尝试暂存后更新。${Font}" - git add . - git stash - git pull origin master --allow-unrelated-histories --rebase - git stash pop - else - git pull origin master --allow-unrelated-histories - fi - - if [[ ! -f "$HOME/.ovo/miao.ok" ]]; then - set -e - echo -e "\n ================ \n ${Info} ${GreenBG} 更新 喵喵插件 运行依赖 ${Font} \n ================ \n" - pnpm install -P - touch ~/.ovo/miao.ok - set +e - fi - - echo -e "\n ================ \n ${Version} ${BlueBG} 喵喵插件版本信息 ${Font} \n ================ \n" - git log -1 --pretty=format:"%h - %an, %ar (%cd) : %s" - -fi - -if [ -d $PY_PLUGIN_PATH"/.git" ]; then - - echo -e "\n ================ \n ${Info} ${GreenBG} 拉取 py-plugin 插件更新 ${Font} \n ================ \n" - - cd $PY_PLUGIN_PATH - - if [[ -n $(git status -s) ]]; then - echo -e " ${Warn} ${YellowBG} 当前工作区有修改,尝试暂存后更新。${Font}" - git add . - git stash - git pull origin v3 --allow-unrelated-histories --rebase - git stash pop - else - git pull origin v3 --allow-unrelated-histories - fi - - if [[ ! -f "$HOME/.ovo/py.ok" ]]; then - set -e - echo -e "\n ================ \n ${Info} ${GreenBG} 更新 py-plugin 运行依赖 ${Font} \n ================ \n" - pnpm install iconv-lite @grpc/grpc-js @grpc/proto-loader -w - poetry config virtualenvs.in-project true - poetry install - touch ~/.ovo/py.ok - set +e - fi - - echo -e "\n ================ \n ${Version} ${BlueBG} py-plugin 插件版本信息 ${Font} \n ================ \n" - - git log -1 --pretty=format:"%h - %an, %ar (%cd) : %s" - -fi - -if [ -d $XIAOYAO_CVS_PATH"/.git" ]; then - - echo -e "\n ================ \n ${Info} ${GreenBG} 拉取 xiaoyao-cvs 插件更新 ${Font} \n ================ \n" - - cd $XIAOYAO_CVS_PATH - - if [[ -n $(git status -s) ]]; then - echo -e " ${Warn} ${YellowBG} 当前工作区有修改,尝试暂存后更新。${Font}" - git add . - git stash - git pull origin master --allow-unrelated-histories --rebase - git stash pop - else - git pull origin master --allow-unrelated-histories - fi - - if [[ ! -f "$HOME/.ovo/xiaoyao.ok" ]]; then - set -e - echo -e "\n ================ \n ${Info} ${GreenBG} 更新 xiaoyao-cvs 插件运行依赖 ${Font} \n ================ \n" - pnpm add promise-retry superagent -w - touch ~/.ovo/xiaoyao.ok - set +e - fi - - echo -e "\n ================ \n ${Version} ${BlueBG} xiaoyao-cvs 插件版本信息 ${Font} \n ================ \n" - - git log -1 --pretty=format:"%h - %an, %ar (%cd) : %s" -fi - -if [ -d $GUOBA_PLUGIN_PATH"/.git" ]; then - - echo -e "\n ================ \n ${Info} ${GreenBG} 拉取 Guoba-Plugin 插件更新 ${Font} \n ================ \n" - - cd $GUOBA_PLUGIN_PATH - - if [[ -n $(git status -s) ]]; then - echo -e " ${Warn} ${YellowBG} 当前工作区有修改,尝试暂存后更新。${Font}" - git add . - git stash - git pull origin master --allow-unrelated-histories --rebase - git stash pop - else - git pull origin master --allow-unrelated-histories - fi - - if [[ ! -f "$HOME/.ovo/guoba.ok" ]]; then - set -e - echo -e "\n ================ \n ${Info} ${GreenBG} 更新 Guoba-Plugin 插件运行依赖 ${Font} \n ================ \n" - pnpm add multer body-parser jsonwebtoken -w - touch ~/.ovo/guoba.ok - set +e - fi - - echo -e "\n ================ \n ${Version} ${BlueBG} Guoba-Plugin 插件版本信息 ${Font} \n ================ \n" - - git log -1 --pretty=format:"%h - %an, %ar (%cd) : %s" -fi - -set -e - -cd $WORK_DIR - -echo -e "\n ================ \n ${Info} ${GreenBG} 初始化 Docker 环境 ${Font} \n ================ \n" - -if [ -f "./config/config/redis.yaml" ]; then - sed -i 's/127.0.0.1/redis/g' ./config/config/redis.yaml - echo -e "\n 修改Redis地址完成~ \n" -fi - -echo -e "\n ================ \n ${Info} ${GreenBG} 启动 Miao-Yunzai ${Font} \n ================ \n" - -set +e -node app -EXIT_CODE=$? - -if [[ $EXIT_CODE != 0 ]]; then - echo -e "\n ================ \n ${Warn} ${YellowBG} 启动 Miao-Yunzai 失败 ${Font} \n ================ \n" - tail -f /dev/null -fi diff --git a/image.js b/image.js index 12a604a..7012d9b 100644 --- a/image.js +++ b/image.js @@ -1,6 +1,4 @@ import { spawn } from 'child_process' -const argv = [...process.argv].splice(2) -const argvs = argv.join(' ').replace(/(\S+\.js|\S+\.ts)/g, '') const child1 = spawn( 'tailwindcss -i ./src/input.css -o ./public/output.css --watch', [], @@ -9,6 +7,8 @@ const child1 = spawn( stdio: 'inherit' } ) +const argv = [...process.argv].splice(2) +const argvs = argv.join(' ').replace(/(\S+\.js|\S+\.ts)/g, '') const child2 = spawn( 'node --no-warnings=ExperimentalWarning --loader ts-node/esm src/server.ts', argvs.split(' '), diff --git a/index.js b/index.js deleted file mode 100644 index 31532aa..0000000 --- a/index.js +++ /dev/null @@ -1,16 +0,0 @@ -import { execSync, spawn } from 'child_process' -const argv = [...process.argv].splice(2) -const argvs = argv.join(' ').replace(/(\S+\.js|\S+\.ts)/g, '') -execSync('tailwindcss -i ./src/input.css -o ./public/output.css') -const child = spawn( - 'node --no-warnings=ExperimentalWarning --loader ts-node/esm src/main.ts', - argvs.split(' '), - { - shell: true, - stdio: 'inherit' - } -) -process.on('SIGINT', () => { - if (child.pid) process.kill(child.pid) - if (process.pid) process.exit() -}) diff --git a/package.json b/package.json index b3760b9..e9527ad 100644 --- a/package.json +++ b/package.json @@ -3,10 +3,11 @@ "version": "4.0.0-rc.0", "author": "Yoimiya-Kokomi, Le-niao", "description": "QQ Group Bot", - "main": "./index.js", + "main": "./app.js", "type": "module", "scripts": { - "app": "node index.js", + "app": "node app.js", + "dev": "ts-node-dev src/main.ts", "login": "node index.js login", "build": "rollup --config rollup.config.js", "start": "pm2 startOrRestart pm2.config.cjs", @@ -22,17 +23,18 @@ "prepare": "husky" }, "dependencies": { - "@loadable/component": "^5.16.4", "art-template": "^4.13.2", "chalk": "^5.3.0", "chokidar": "^3.6.0", - "express": "^4.19.2", - "express-art-template": "^1.0.1", + "dotenv": "^16.4.5", "https-proxy-agent": "7.0.4", "icqq": "^0.6.10", "image-size": "^1.1.1", "inquirer": "^9.2.16", + "koa": "^2.15.3", "koa-mount": "^4.0.0", + "koa-router": "^12.0.1", + "koa-static": "^5.0.0", "lodash": "^4.17.21", "log4js": "^6.9.1", "md5": "^2.3.0", @@ -71,16 +73,13 @@ "eslint-plugin-n": "^16.6.2", "eslint-plugin-promise": "^6.1.1", "husky": "^9.0.11", - "koa": "^2.15.3", - "koa-router": "^12.0.1", - "koa-static": "^5.0.0", "nodemon": "^3.0.1", "preline": "^2.3.0", "prettier": "^3.0.3", "rollup": "^4.16.4", "tailwindcss": "^3.4.3", - "ts-node": "^10.9.1", - "typescript": "^5.0.4" + "ts-node": "^10.9.2", + "typescript": "^5.4.5" }, "exports": { "./config": { diff --git a/pm2.config.cjs b/pm2.config.cjs index a3511b8..45b1e4e 100644 --- a/pm2.config.cjs +++ b/pm2.config.cjs @@ -3,7 +3,7 @@ module.exports = { apps: [ { name: 'Miao-Yunzai', - script: './index.js', + script: './app.js', args: argv, // 超时时间内进程仍未终止,则 PM2 将强制终止该进程 kill_timeout: 5000, @@ -20,7 +20,9 @@ module.exports = { // 不监听文件变化 watch: false, env: { + // 生产环境 NODE_ENV: 'production' + // 此环境变量,都称之为开发环境 } } ] diff --git a/src/init/env.ts b/src/init/env.ts new file mode 100644 index 0000000..0d2da85 --- /dev/null +++ b/src/init/env.ts @@ -0,0 +1,4 @@ +import { configDotenv } from 'dotenv' +configDotenv({ + path: '.env' +}) diff --git a/src/init/index.ts b/src/init/index.ts index a6d9fc2..9838651 100644 --- a/src/init/index.ts +++ b/src/init/index.ts @@ -1,3 +1,4 @@ +import './env.js' import './require.js' import './config.js' import './logger.js' diff --git a/src/utils/module.ts b/src/utils/module.ts index 4bd2221..88567cc 100644 --- a/src/utils/module.ts +++ b/src/utils/module.ts @@ -27,7 +27,6 @@ const now = () => `?t=${Date.now()}` /** * @param basePath import.meta.url - * @param T 默认开启动态,可自定设置系统量关闭 * @returns * *********** * 创建动态模块 @@ -36,17 +35,20 @@ const now = () => `?t=${Date.now()}` * 如果动态模块内包含动态模块, * 内部模块也会跟着重新加载, * *********** - * 请确保你的模块是可预测 - * *********** - * 请确保当前模块是可被执行的 + * 在env.NODE_ENV=='production'下禁用 + * @deprecated 实验性的,请勿烂用 */ -export const createDynamic = (basePath: string, T = true) => { +export const createDynamic = (basePath: string) => { /** * 与import作用相同 * @param path 相对路径 - * @param TT 默认开启动态,可自定设置系统量关闭 * @returns */ - return (path: string, TT = true) => - import(new URL(`${path}${TT && T ? now() : ''}`, basePath).href) + return (path: string) => + import( + new URL( + `${path}${process.env.NODE_ENV == 'NODE_ENV' ? '' : now()}`, + basePath + ).href + ) }