import fs from 'node:fs/promises' import path from 'node:path' import readline from 'node:readline/promises' import { stdin as input, stdout as output } from 'node:process' import { CLEAN_DEV_TARGETS, ROUTE_MODULES_TO_REMOVE, buildMinimalRouteModuleFiles, createMinimalChangeLogContent, createMinimalFastEnterContent, createMinimalRoutesAliasContent, pruneLocaleMessages, rewriteMenuApiContent } from './clean-dev.helpers.mjs' const projectRoot = process.cwd() async function pathExists(relativePath) { try { await fs.access(path.resolve(projectRoot, relativePath)) return true } catch { return false } } async function countTargetEntries(targets) { let count = 0 for (const target of targets) { if (await pathExists(target)) { count += 1 } } return count } async function removeTarget(relativePath) { await fs.rm(path.resolve(projectRoot, relativePath), { recursive: true, force: true }) } async function writeText(relativePath, content) { await fs.writeFile(path.resolve(projectRoot, relativePath), content, 'utf8') } async function rewriteLocale(relativePath) { const fullPath = path.resolve(projectRoot, relativePath) const locale = JSON.parse(await fs.readFile(fullPath, 'utf8')) const nextLocale = pruneLocaleMessages(locale) await fs.writeFile(fullPath, `${JSON.stringify(nextLocale, null, 2)}\n`, 'utf8') } async function rewriteMenuApi() { const relativePath = 'src/api/system-manage.js' const fullPath = path.resolve(projectRoot, relativePath) const currentContent = await fs.readFile(fullPath, 'utf8') const nextContent = rewriteMenuApiContent(currentContent) await fs.writeFile(fullPath, nextContent, 'utf8') } function printSummary(existingTargetCount) { console.log('Art Design Pro clean:dev') console.log() console.log('将执行最小开发版裁剪:') console.log('- 删除演示页面、演示资源和多余 mock 数据') console.log('- 重写路由模块,只保留 dashboard/system/result/exception') console.log('- 清理语言包中的演示菜单项') console.log('- 重写快速入口、路由别名、更新日志数据和菜单接口') console.log() console.log(`检测到 ${existingTargetCount} 个待清理目标存在于当前仓库中。`) console.log('输入 yes 继续,按 Enter 取消。') console.log() } async function confirmExecution() { const rl = readline.createInterface({ input, output }) try { const answer = await rl.question('> ') return answer.trim().toLowerCase() === 'yes' } finally { rl.close() } } async function runCleanup() { const existingTargetCount = await countTargetEntries(CLEAN_DEV_TARGETS) printSummary(existingTargetCount) const confirmed = await confirmExecution() if (!confirmed) { console.log('已取消 clean:dev。') return } for (const target of CLEAN_DEV_TARGETS) { await removeTarget(target) } const routeFiles = buildMinimalRouteModuleFiles() for (const moduleName of ROUTE_MODULES_TO_REMOVE) { await removeTarget(path.posix.join('src/router/modules', moduleName)) } await writeText('src/router/modules/dashboard.js', routeFiles.dashboard) await writeText('src/router/modules/system.js', routeFiles.system) await writeText('src/router/modules/index.js', routeFiles.index) await writeText('src/router/routesAlias.js', createMinimalRoutesAliasContent()) await writeText('src/mock/upgrade/changeLog.js', createMinimalChangeLogContent()) await writeText('src/config/modules/fastEnter.js', createMinimalFastEnterContent()) await rewriteLocale('src/locales/langs/zh.json') await rewriteLocale('src/locales/langs/en.json') await rewriteMenuApi() console.log() console.log('clean:dev 执行完成。') console.log('当前仓库已切换为最小开发版结构。') } runCleanup().catch((error) => { console.error() console.error('clean:dev 执行失败。') console.error(error) process.exit(1) })