优化初始化流程

自动安装依赖&自动构建
This commit is contained in:
Jerry 2025-05-02 20:29:07 +08:00
parent 0ea93bb670
commit c024051db8
3 changed files with 36 additions and 2 deletions

View File

@ -17,7 +17,7 @@ class FileService {
} }
const filePath = path.join(this.filePath, relativePath); const filePath = path.join(this.filePath, relativePath);
logger.debug(`尝试访问图像路径: ${filePath}`); logger.debug(`尝试访问文件路径: ${filePath}`);
return fs.existsSync(filePath) ? filePath : null; return fs.existsSync(filePath) ? filePath : null;
} }

View File

@ -1,6 +1,11 @@
import simpleGit, { SimpleGit } from 'simple-git'; import simpleGit, { SimpleGit } from 'simple-git';
import paths from './path'; import paths from './path';
import logger from './logger'; import logger from './logger';
import { exec } from 'child_process';
import { promisify } from 'util';
import fs from 'fs';
const execAsync = promisify(exec);
class AutoUpdater { class AutoUpdater {
private git: SimpleGit; private git: SimpleGit;
@ -41,7 +46,10 @@ class AutoUpdater {
return false; return false;
} }
logger.info('更新成功!'); logger.info('代码更新成功,开始更新依赖..');
await this.updateDependencies();
logger.info('自动更新流程完成。');
return true; return true;
} else { } else {
logger.info('远程仓库没有新变化..'); logger.info('远程仓库没有新变化..');
@ -52,6 +60,30 @@ class AutoUpdater {
return false; return false;
} }
} }
/**
*
*/
private async updateDependencies(): Promise<void> {
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(); const autoUpdater = new AutoUpdater();

View File

@ -35,6 +35,7 @@ class PathManager {
userData: path.join(this.baseDir, 'private/data'), userData: path.join(this.baseDir, 'private/data'),
files: path.join(this.baseDir, 'public/files'), files: path.join(this.baseDir, 'public/files'),
media: path.join(this.baseDir, 'public/files/media'), media: path.join(this.baseDir, 'public/files/media'),
package: path.join(this.baseDir, 'package.json'),
}; };
return type ? mappings[type] : this.baseDir; return type ? mappings[type] : this.baseDir;
@ -81,6 +82,7 @@ type PathType =
| 'temp' | 'temp'
| 'userData' | 'userData'
| 'files' | 'files'
| 'package'
| 'media'; | 'media';
const paths = PathManager.getInstance(); const paths = PathManager.getInstance();