zhou zhou
15 小时以前 fec285d150b377d004e47f0973d298b92fe4c711
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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)
})