From c024051db80bcb8d6ca2d56b5d2d0f00ef27dac6 Mon Sep 17 00:00:00 2001 From: Jerry Date: Fri, 2 May 2025 20:29:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 自动安装依赖&自动构建 --- src/modules/image/file.service.ts | 2 +- src/utils/core/autoUpdater.ts | 34 ++++++++++++++++++++++++++++++- src/utils/core/path.ts | 2 ++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/modules/image/file.service.ts b/src/modules/image/file.service.ts index 600a17e..46732de 100644 --- a/src/modules/image/file.service.ts +++ b/src/modules/image/file.service.ts @@ -17,7 +17,7 @@ class FileService { } const filePath = path.join(this.filePath, relativePath); - logger.debug(`尝试访问图像路径: ${filePath}`); + logger.debug(`尝试访问文件路径: ${filePath}`); return fs.existsSync(filePath) ? filePath : null; } diff --git a/src/utils/core/autoUpdater.ts b/src/utils/core/autoUpdater.ts index 48aeaa2..7f6d7fd 100644 --- a/src/utils/core/autoUpdater.ts +++ b/src/utils/core/autoUpdater.ts @@ -1,6 +1,11 @@ import simpleGit, { SimpleGit } from 'simple-git'; import paths from './path'; import logger from './logger'; +import { exec } from 'child_process'; +import { promisify } from 'util'; +import fs from 'fs'; + +const execAsync = promisify(exec); class AutoUpdater { private git: SimpleGit; @@ -41,7 +46,10 @@ class AutoUpdater { return false; } - logger.info('更新成功!'); + logger.info('代码更新成功,开始更新依赖..'); + await this.updateDependencies(); + + logger.info('自动更新流程完成。'); return true; } else { logger.info('远程仓库没有新变化..'); @@ -52,6 +60,30 @@ class AutoUpdater { return false; } } + + /** + * 自动更新依赖和构建 + */ + private async updateDependencies(): Promise { + try { + logger.info('执行 pnpm install...'); + await execAsync('pnpm install', { cwd: this.repoPath }); + logger.info('依赖安装完成。'); + + const pkgPath = paths.get('package'); + const pkgJson = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')); + + if (pkgJson.scripts?.build) { + logger.info('检测到 build 脚本,执行 pnpm build...'); + await execAsync('pnpm build', { cwd: this.repoPath }); + logger.info('构建完成。'); + } else { + logger.info('未检测到 build 脚本,跳过构建。'); + } + } catch (error) { + logger.error('更新依赖或构建失败: ', error); + } + } } const autoUpdater = new AutoUpdater(); diff --git a/src/utils/core/path.ts b/src/utils/core/path.ts index 3f6678b..86e65fb 100644 --- a/src/utils/core/path.ts +++ b/src/utils/core/path.ts @@ -35,6 +35,7 @@ class PathManager { userData: path.join(this.baseDir, 'private/data'), files: path.join(this.baseDir, 'public/files'), media: path.join(this.baseDir, 'public/files/media'), + package: path.join(this.baseDir, 'package.json'), }; return type ? mappings[type] : this.baseDir; @@ -81,6 +82,7 @@ type PathType = | 'temp' | 'userData' | 'files' + | 'package' | 'media'; const paths = PathManager.getInstance();