From 5bfa13112aa97530f22155204bfe026363e78927 Mon Sep 17 00:00:00 2001 From: kangfenmao Date: Sat, 19 Apr 2025 19:22:36 +0800 Subject: [PATCH] feat(locales): add locale cleanup functionality to after-pack script - Introduced a new `remove-locales.js` script to handle the removal of unnecessary locale files based on the platform. - Integrated the locale cleanup process into the `after-pack.js` script to ensure locales are managed during packaging. --- scripts/after-pack.js | 2 ++ scripts/remove-locales.js | 58 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 scripts/remove-locales.js diff --git a/scripts/after-pack.js b/scripts/after-pack.js index 073120e5..b226e8a2 100644 --- a/scripts/after-pack.js +++ b/scripts/after-pack.js @@ -1,8 +1,10 @@ const { Arch } = require('electron-builder') +const { default: removeLocales } = require('./remove-locales') const fs = require('fs') const path = require('path') exports.default = async function (context) { + await removeLocales(context) const platform = context.packager.platform.name const arch = context.arch diff --git a/scripts/remove-locales.js b/scripts/remove-locales.js new file mode 100644 index 00000000..1dc0b3cc --- /dev/null +++ b/scripts/remove-locales.js @@ -0,0 +1,58 @@ +const fs = require('fs') +const path = require('path') + +exports.default = async function (context) { + const platform = context.packager.platform.name + + // 根据平台确定 locales 目录位置 + let resourceDirs = [] + if (platform === 'mac') { + // macOS 的语言文件位置 + resourceDirs = [ + path.join(context.appOutDir, 'Cherry Studio.app', 'Contents', 'Resources'), + path.join( + context.appOutDir, + 'Cherry Studio.app', + 'Contents', + 'Frameworks', + 'Electron Framework.framework', + 'Resources' + ) + ] + } else { + // Windows 和 Linux 的语言文件位置 + resourceDirs = [path.join(context.appOutDir, 'locales')] + } + + // 处理每个资源目录 + for (const resourceDir of resourceDirs) { + if (!fs.existsSync(resourceDir)) { + console.log(`Resource directory not found: ${resourceDir}, skipping...`) + continue + } + + // 读取所有文件和目录 + const items = fs.readdirSync(resourceDir) + + // 遍历并删除不需要的语言文件 + for (const item of items) { + if (platform === 'mac') { + // 在 macOS 上检查 .lproj 目录 + if (item.endsWith('.lproj') && !item.match(/^(en|zh|ru)/)) { + const dirPath = path.join(resourceDir, item) + fs.rmSync(dirPath, { recursive: true, force: true }) + console.log(`Removed locale directory: ${item} from ${resourceDir}`) + } + } else { + // 其他平台处理 .pak 文件 + if (!item.match(/^(en|zh|ru)/)) { + const filePath = path.join(resourceDir, item) + fs.unlinkSync(filePath) + console.log(`Removed locale file: ${item} from ${resourceDir}`) + } + } + } + } + + console.log('Locale cleanup completed!') +}