From 0cf158ae40ab133e76dead3167512e4762030eb8 Mon Sep 17 00:00:00 2001 From: zhang1106 <849185023@qq.com> Date: Tue, 12 May 2026 09:12:10 +0800 Subject: [PATCH] =?UTF-8?q?fix(install,update,uninstall):=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E6=97=A5=E5=BF=97=E8=BF=9B=E5=BA=A6=E8=AE=A1=E6=95=B0?= =?UTF-8?q?=E4=B8=8D=E5=87=86=E7=A1=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重构日志step方法,支持自定义总步数 - 更新install、update、uninstall脚本中日志调用,传入正确的步骤总数 - 移除硬编码的INSTALL_STEPS.length依赖,让进度展示更灵活准确 --- install/logger.js | 9 +++++++-- uninstall.js | 16 ++++++++-------- update.js | 14 +++++++------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/install/logger.js b/install/logger.js index 9bd0793..98f342a 100644 --- a/install/logger.js +++ b/install/logger.js @@ -58,13 +58,18 @@ function progressBar(current, total, width = 20) { return bar; } +let currentStepTotal = INSTALL_STEPS.length; + const log = { info: (msg) => console.log(` ${colors.cyan}${ICONS.info}${colors.reset} ${msg}`), success: (msg) => console.log(` ${colors.green}${ICONS.success}${colors.reset} ${msg}`), warning: (msg) => console.log(` ${colors.yellow}${ICONS.warning}${colors.reset} ${msg}`), error: (msg) => console.log(` ${colors.red}${ICONS.error}${colors.reset} ${msg}`), - step: (msg, stepIndex) => { + step: (msg, stepIndex, stepTotal) => { + if (stepTotal !== undefined) { + currentStepTotal = stepTotal; + } if (stepIndex !== undefined) { currentStepIndex = stepIndex; } else { @@ -74,7 +79,7 @@ const log = { } } const current = currentStepIndex + 1; - const total = INSTALL_STEPS.length; + const total = currentStepTotal; const bar = progressBar(current, total, 15); const progress = `${colors.dim}[${current}/${total}]${colors.reset} ${bar}`; console.log(''); diff --git a/uninstall.js b/uninstall.js index a9df827..e49c356 100644 --- a/uninstall.js +++ b/uninstall.js @@ -175,7 +175,7 @@ function isRootUser() { } async function stopAndDeleteServices() { - log.step('停止服务'); + log.step('停止服务', 0, 7); if (!commandExists('pm2')) { log.warning('未检测到 PM2,跳过服务停止步骤'); @@ -253,7 +253,7 @@ async function stopAndDeleteServices() { } async function cleanupNginxConfig(cmdArgs) { - log.step('清理 Nginx'); + log.step('清理 Nginx', 1, 7); const isWindows = process.platform === 'win32'; const isLinux = process.platform === 'linux'; @@ -511,7 +511,7 @@ async function cleanupNginxConfig(cmdArgs) { } async function cleanupConfigFiles() { - log.step('清理配置'); + log.step('清理配置', 2, 7); const filesToDelete = [ { path: path.join(__dirname, 'backend', '.env'), name: '后端环境变量 (.env)', type: '配置文件' }, @@ -553,7 +553,7 @@ async function cleanupConfigFiles() { } async function cleanupDatabase(cmdArgs) { - log.step('数据库清理'); + log.step('数据库清理', 3, 7); const dbConfig = getDatabaseConfig(); const sqliteDbPath = dbConfig.sqliteDbPath; @@ -661,7 +661,7 @@ async function cleanupDatabase(cmdArgs) { } async function cleanupDependencies(cmdArgs) { - log.step('依赖和构建产物清理'); + log.step('依赖和构建产物清理', 6, 7); log.warning('此操作将删除 node_modules 和构建产物,需要重新安装依赖才能再次运行'); const confirm = cmdArgs?.force ? 'y' : await ask('是否删除依赖和构建产物? (y/N)', 'N'); @@ -697,7 +697,7 @@ async function cleanupDependencies(cmdArgs) { } async function cleanupLogs(cmdArgs) { - log.step('日志清理'); + log.step('日志清理', 4, 7); const logsDir = path.join(__dirname, 'backend', 'logs'); if (fs.existsSync(logsDir)) { @@ -743,7 +743,7 @@ async function cleanupLogs(cmdArgs) { } async function cleanupUploads(cmdArgs) { - log.step('上传清理'); + log.step('上传清理', 5, 7); const uploadsDir = path.join(__dirname, 'backend', 'uploads'); if (fs.existsSync(uploadsDir)) { @@ -770,7 +770,7 @@ async function cleanupUploads(cmdArgs) { } async function backupDatabase() { - log.step('备份数据库'); + log.step('备份数据库', 0, 7); const dbConfig = getDatabaseConfig(); const sqliteDbPath = dbConfig.sqliteDbPath; diff --git a/update.js b/update.js index 3236f33..7c65af0 100644 --- a/update.js +++ b/update.js @@ -801,37 +801,37 @@ async function main() { process.exit(130); }); - log.step('备份数据', 0); + log.step('备份数据', 0, 7); results.backup = backupDatabase(options); if (!results.backup.success && !options.dryRun) { throw new Error('数据库备份失败'); } - log.step('拉取代码', 1); + log.step('拉取代码', 1, 7); results.git = pullCode(options); - log.step('安装依赖', 2); + log.step('安装依赖', 2, 7); results.deps = installDependencies(options); if (!results.deps.success && !options.dryRun) { throw new Error('依赖安装失败'); } - log.step('数据库迁移', 3); + log.step('数据库迁移', 3, 7); results.migrate = runMigrations(options); - log.step('构建前端', 4); + log.step('构建前端', 4, 7); results.build = buildFrontend(options); if (!results.build.success && !options.dryRun) { throw new Error('前端构建失败'); } - log.step('重启服务', 5); + log.step('重启服务', 5, 7); results.restart = restartServices(options); if (!results.restart.success && !options.dryRun) { throw new Error('服务重启失败'); } - log.step('健康检查', 6); + log.step('健康检查', 6, 7); results.health = await healthCheck(); log.divider();