fix(install,update,uninstall): 修复日志进度计数不准确问题

- 重构日志step方法,支持自定义总步数
- 更新install、update、uninstall脚本中日志调用,传入正确的步骤总数
- 移除硬编码的INSTALL_STEPS.length依赖,让进度展示更灵活准确
This commit is contained in:
zhang1106
2026-05-12 09:12:10 +08:00
parent 2f8e95be49
commit 0cf158ae40
3 changed files with 22 additions and 17 deletions
+7 -2
View File
@@ -58,13 +58,18 @@ function progressBar(current, total, width = 20) {
return bar; return bar;
} }
let currentStepTotal = INSTALL_STEPS.length;
const log = { const log = {
info: (msg) => console.log(` ${colors.cyan}${ICONS.info}${colors.reset} ${msg}`), info: (msg) => console.log(` ${colors.cyan}${ICONS.info}${colors.reset} ${msg}`),
success: (msg) => console.log(` ${colors.green}${ICONS.success}${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}`), warning: (msg) => console.log(` ${colors.yellow}${ICONS.warning}${colors.reset} ${msg}`),
error: (msg) => console.log(` ${colors.red}${ICONS.error}${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) { if (stepIndex !== undefined) {
currentStepIndex = stepIndex; currentStepIndex = stepIndex;
} else { } else {
@@ -74,7 +79,7 @@ const log = {
} }
} }
const current = currentStepIndex + 1; const current = currentStepIndex + 1;
const total = INSTALL_STEPS.length; const total = currentStepTotal;
const bar = progressBar(current, total, 15); const bar = progressBar(current, total, 15);
const progress = `${colors.dim}[${current}/${total}]${colors.reset} ${bar}`; const progress = `${colors.dim}[${current}/${total}]${colors.reset} ${bar}`;
console.log(''); console.log('');
+8 -8
View File
@@ -175,7 +175,7 @@ function isRootUser() {
} }
async function stopAndDeleteServices() { async function stopAndDeleteServices() {
log.step('停止服务'); log.step('停止服务', 0, 7);
if (!commandExists('pm2')) { if (!commandExists('pm2')) {
log.warning('未检测到 PM2,跳过服务停止步骤'); log.warning('未检测到 PM2,跳过服务停止步骤');
@@ -253,7 +253,7 @@ async function stopAndDeleteServices() {
} }
async function cleanupNginxConfig(cmdArgs) { async function cleanupNginxConfig(cmdArgs) {
log.step('清理 Nginx'); log.step('清理 Nginx', 1, 7);
const isWindows = process.platform === 'win32'; const isWindows = process.platform === 'win32';
const isLinux = process.platform === 'linux'; const isLinux = process.platform === 'linux';
@@ -511,7 +511,7 @@ async function cleanupNginxConfig(cmdArgs) {
} }
async function cleanupConfigFiles() { async function cleanupConfigFiles() {
log.step('清理配置'); log.step('清理配置', 2, 7);
const filesToDelete = [ const filesToDelete = [
{ path: path.join(__dirname, 'backend', '.env'), name: '后端环境变量 (.env)', type: '配置文件' }, { path: path.join(__dirname, 'backend', '.env'), name: '后端环境变量 (.env)', type: '配置文件' },
@@ -553,7 +553,7 @@ async function cleanupConfigFiles() {
} }
async function cleanupDatabase(cmdArgs) { async function cleanupDatabase(cmdArgs) {
log.step('数据库清理'); log.step('数据库清理', 3, 7);
const dbConfig = getDatabaseConfig(); const dbConfig = getDatabaseConfig();
const sqliteDbPath = dbConfig.sqliteDbPath; const sqliteDbPath = dbConfig.sqliteDbPath;
@@ -661,7 +661,7 @@ async function cleanupDatabase(cmdArgs) {
} }
async function cleanupDependencies(cmdArgs) { async function cleanupDependencies(cmdArgs) {
log.step('依赖和构建产物清理'); log.step('依赖和构建产物清理', 6, 7);
log.warning('此操作将删除 node_modules 和构建产物,需要重新安装依赖才能再次运行'); log.warning('此操作将删除 node_modules 和构建产物,需要重新安装依赖才能再次运行');
const confirm = cmdArgs?.force ? 'y' : await ask('是否删除依赖和构建产物? (y/N)', 'N'); const confirm = cmdArgs?.force ? 'y' : await ask('是否删除依赖和构建产物? (y/N)', 'N');
@@ -697,7 +697,7 @@ async function cleanupDependencies(cmdArgs) {
} }
async function cleanupLogs(cmdArgs) { async function cleanupLogs(cmdArgs) {
log.step('日志清理'); log.step('日志清理', 4, 7);
const logsDir = path.join(__dirname, 'backend', 'logs'); const logsDir = path.join(__dirname, 'backend', 'logs');
if (fs.existsSync(logsDir)) { if (fs.existsSync(logsDir)) {
@@ -743,7 +743,7 @@ async function cleanupLogs(cmdArgs) {
} }
async function cleanupUploads(cmdArgs) { async function cleanupUploads(cmdArgs) {
log.step('上传清理'); log.step('上传清理', 5, 7);
const uploadsDir = path.join(__dirname, 'backend', 'uploads'); const uploadsDir = path.join(__dirname, 'backend', 'uploads');
if (fs.existsSync(uploadsDir)) { if (fs.existsSync(uploadsDir)) {
@@ -770,7 +770,7 @@ async function cleanupUploads(cmdArgs) {
} }
async function backupDatabase() { async function backupDatabase() {
log.step('备份数据库'); log.step('备份数据库', 0, 7);
const dbConfig = getDatabaseConfig(); const dbConfig = getDatabaseConfig();
const sqliteDbPath = dbConfig.sqliteDbPath; const sqliteDbPath = dbConfig.sqliteDbPath;
+7 -7
View File
@@ -801,37 +801,37 @@ async function main() {
process.exit(130); process.exit(130);
}); });
log.step('备份数据', 0); log.step('备份数据', 0, 7);
results.backup = backupDatabase(options); results.backup = backupDatabase(options);
if (!results.backup.success && !options.dryRun) { if (!results.backup.success && !options.dryRun) {
throw new Error('数据库备份失败'); throw new Error('数据库备份失败');
} }
log.step('拉取代码', 1); log.step('拉取代码', 1, 7);
results.git = pullCode(options); results.git = pullCode(options);
log.step('安装依赖', 2); log.step('安装依赖', 2, 7);
results.deps = installDependencies(options); results.deps = installDependencies(options);
if (!results.deps.success && !options.dryRun) { if (!results.deps.success && !options.dryRun) {
throw new Error('依赖安装失败'); throw new Error('依赖安装失败');
} }
log.step('数据库迁移', 3); log.step('数据库迁移', 3, 7);
results.migrate = runMigrations(options); results.migrate = runMigrations(options);
log.step('构建前端', 4); log.step('构建前端', 4, 7);
results.build = buildFrontend(options); results.build = buildFrontend(options);
if (!results.build.success && !options.dryRun) { if (!results.build.success && !options.dryRun) {
throw new Error('前端构建失败'); throw new Error('前端构建失败');
} }
log.step('重启服务', 5); log.step('重启服务', 5, 7);
results.restart = restartServices(options); results.restart = restartServices(options);
if (!results.restart.success && !options.dryRun) { if (!results.restart.success && !options.dryRun) {
throw new Error('服务重启失败'); throw new Error('服务重启失败');
} }
log.step('健康检查', 6); log.step('健康检查', 6, 7);
results.health = await healthCheck(); results.health = await healthCheck();
log.divider(); log.divider();