postupgrade.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // # 执行 `pnpm upgrade` 后会升级 `uniapp` 相关依赖
  2. // # 在升级完后,会自动添加很多无用依赖,这需要删除以减小依赖包体积
  3. // # 只需要执行下面的命令即可
  4. import { exec } from 'node:child_process'
  5. import { promisify } from 'node:util'
  6. // 日志控制开关,设置为 true 可以启用所有日志输出
  7. const FG_LOG_ENABLE = true
  8. // 将 exec 转换为返回 Promise 的函数
  9. const execPromise = promisify(exec)
  10. // 定义要执行的命令
  11. const dependencies = [
  12. '@dcloudio/uni-app-harmony',
  13. // TODO: 如果不需要某个平台的小程序,请手动删除或注释掉
  14. '@dcloudio/uni-mp-alipay',
  15. '@dcloudio/uni-mp-baidu',
  16. '@dcloudio/uni-mp-jd',
  17. '@dcloudio/uni-mp-kuaishou',
  18. '@dcloudio/uni-mp-lark',
  19. '@dcloudio/uni-mp-qq',
  20. '@dcloudio/uni-mp-toutiao',
  21. '@dcloudio/uni-mp-xhs',
  22. '@dcloudio/uni-quickapp-webview',
  23. // i18n模板要注释掉下面的
  24. 'vue-i18n',
  25. ]
  26. /**
  27. * 带开关的日志输出函数
  28. * @param {string} message 日志消息
  29. * @param {string} type 日志类型 (log, error)
  30. */
  31. function log(message, type = 'log') {
  32. if (FG_LOG_ENABLE) {
  33. if (type === 'error') {
  34. console.error(message)
  35. }
  36. else {
  37. console.log(message)
  38. }
  39. }
  40. }
  41. /**
  42. * 卸载单个依赖包
  43. * @param {string} dep 依赖包名
  44. * @returns {Promise<boolean>} 是否成功卸载
  45. */
  46. async function uninstallDependency(dep) {
  47. try {
  48. log(`开始卸载依赖: ${dep}`)
  49. const { stdout, stderr } = await execPromise(`pnpm un ${dep}`)
  50. if (stdout) {
  51. log(`stdout [${dep}]: ${stdout}`)
  52. }
  53. if (stderr) {
  54. log(`stderr [${dep}]: ${stderr}`, 'error')
  55. }
  56. log(`成功卸载依赖: ${dep}`)
  57. return true
  58. }
  59. catch (error) {
  60. // 单个依赖卸载失败不影响其他依赖
  61. log(`卸载依赖 ${dep} 失败: ${error.message}`, 'error')
  62. return false
  63. }
  64. }
  65. /**
  66. * 串行卸载所有依赖包
  67. */
  68. async function uninstallAllDependencies() {
  69. log(`开始串行卸载 ${dependencies.length} 个依赖包...`)
  70. let successCount = 0
  71. let failedCount = 0
  72. // 串行执行所有卸载命令
  73. for (const dep of dependencies) {
  74. const success = await uninstallDependency(dep)
  75. if (success) {
  76. successCount++
  77. }
  78. else {
  79. failedCount++
  80. }
  81. // 为了避免命令执行过快导致的问题,添加短暂延迟
  82. await new Promise(resolve => setTimeout(resolve, 100))
  83. }
  84. log(`卸载操作完成: 成功 ${successCount} 个, 失败 ${failedCount} 个`)
  85. }
  86. // 执行串行卸载
  87. uninstallAllDependencies().catch((err) => {
  88. log(`串行卸载过程中出现未捕获的错误: ${err}`, 'error')
  89. })