mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-09 14:59:27 +08:00
* refactor: use pnpm install instead of manual download for prebuild packages Replace manual tgz download with pnpm install for architecture-specific prebuild binaries, simplifying the build process. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * delete utils * update after pack * udpate before pack * use optional deps * refactor: use js-yaml to modify pnpm-workspace.yaml for cross-platform builds - Add all prebuild packages to optionalDependencies in package.json - Use js-yaml to parse and modify pnpm-workspace.yaml - Add target platform to supportedArchitectures.os and cpu - Restore original config after pnpm install Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * fix version * refactor: streamline package management and filtering logic in before… (#12370) refactor: streamline package management and filtering logic in before-pack.js - Consolidated architecture-specific package definitions into a single array for better maintainability. - Simplified the logic for determining target platform and architecture. - Enhanced the filtering process for excluding and including packages based on architecture and platform. - Improved console logging for clarity during package installation. This refactor aims to improve the readability and efficiency of the prebuild package handling process. * refactor: update package filtering logic in before-pack.js to read from electron-builder.yml - Modified the package filtering process to load configuration directly from electron-builder.yml, reducing potential errors from multiple overrides. - Enhanced maintainability by centralizing the file configuration management. This change aims to streamline the prebuild package handling and improve configuration clarity. --------- Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com> Co-authored-by: 亢奋猫 <kangfenmao@qq.com>
120 lines
4.4 KiB
JavaScript
120 lines
4.4 KiB
JavaScript
const { Arch } = require('electron-builder')
|
|
const { execSync } = require('child_process')
|
|
const fs = require('fs')
|
|
const path = require('path')
|
|
const yaml = require('js-yaml')
|
|
|
|
const workspaceConfigPath = path.join(__dirname, '..', 'pnpm-workspace.yaml')
|
|
|
|
// if you want to add new prebuild binaries packages with different architectures, you can add them here
|
|
// please add to allX64 and allArm64 from pnpm-lock.yaml
|
|
const packages = [
|
|
'@img/sharp-darwin-arm64',
|
|
'@img/sharp-darwin-x64',
|
|
'@img/sharp-linux-arm64',
|
|
'@img/sharp-linux-x64',
|
|
'@img/sharp-win32-arm64',
|
|
'@img/sharp-win32-x64',
|
|
'@img/sharp-libvips-darwin-arm64',
|
|
'@img/sharp-libvips-darwin-x64',
|
|
'@img/sharp-libvips-linux-arm64',
|
|
'@img/sharp-libvips-linux-x64',
|
|
'@libsql/darwin-arm64',
|
|
'@libsql/darwin-x64',
|
|
'@libsql/linux-arm64-gnu',
|
|
'@libsql/linux-x64-gnu',
|
|
'@libsql/win32-x64-msvc',
|
|
'@napi-rs/system-ocr-darwin-arm64',
|
|
'@napi-rs/system-ocr-darwin-x64',
|
|
'@napi-rs/system-ocr-win32-arm64-msvc',
|
|
'@napi-rs/system-ocr-win32-x64-msvc',
|
|
'@strongtz/win32-arm64-msvc'
|
|
]
|
|
|
|
const platformToArch = {
|
|
mac: 'darwin',
|
|
windows: 'win32',
|
|
linux: 'linux'
|
|
}
|
|
|
|
exports.default = async function (context) {
|
|
const arch = context.arch === Arch.arm64 ? 'arm64' : 'x64'
|
|
const platformName = context.packager.platform.name
|
|
const platform = platformToArch[platformName]
|
|
|
|
const downloadPackages = async () => {
|
|
// Skip if target platform and architecture match current system
|
|
if (platform === process.platform && arch === process.arch) {
|
|
console.log(`Skipping install: target (${platform}/${arch}) matches current system`)
|
|
return
|
|
}
|
|
|
|
console.log(`Installing packages for target platform=${platform} arch=${arch}...`)
|
|
|
|
// Backup and modify pnpm-workspace.yaml to add target platform support
|
|
const originalWorkspaceConfig = fs.readFileSync(workspaceConfigPath, 'utf-8')
|
|
const workspaceConfig = yaml.load(originalWorkspaceConfig)
|
|
|
|
// Add target platform to supportedArchitectures.os
|
|
if (!workspaceConfig.supportedArchitectures.os.includes(platform)) {
|
|
workspaceConfig.supportedArchitectures.os.push(platform)
|
|
}
|
|
|
|
// Add target architecture to supportedArchitectures.cpu
|
|
if (!workspaceConfig.supportedArchitectures.cpu.includes(arch)) {
|
|
workspaceConfig.supportedArchitectures.cpu.push(arch)
|
|
}
|
|
|
|
const modifiedWorkspaceConfig = yaml.dump(workspaceConfig)
|
|
console.log('Modified workspace config:', modifiedWorkspaceConfig)
|
|
fs.writeFileSync(workspaceConfigPath, modifiedWorkspaceConfig)
|
|
|
|
try {
|
|
execSync(`pnpm install`, { stdio: 'inherit' })
|
|
} finally {
|
|
// Restore original pnpm-workspace.yaml
|
|
fs.writeFileSync(workspaceConfigPath, originalWorkspaceConfig)
|
|
}
|
|
}
|
|
|
|
await downloadPackages()
|
|
|
|
const excludePackages = async (packagesToExclude) => {
|
|
// 从项目根目录的 electron-builder.yml 读取 files 配置,避免多次覆盖配置导致出错
|
|
const electronBuilderConfigPath = path.join(__dirname, '..', 'electron-builder.yml')
|
|
const electronBuilderConfig = yaml.load(fs.readFileSync(electronBuilderConfigPath, 'utf-8'))
|
|
let filters = electronBuilderConfig.files
|
|
|
|
// add filters for other architectures (exclude them)
|
|
filters.push(...packagesToExclude)
|
|
|
|
context.packager.config.files[0].filter = filters
|
|
}
|
|
|
|
const arm64KeepPackages = packages.filter((p) => p.includes('arm64') && p.includes(platform))
|
|
const arm64ExcludePackages = packages
|
|
.filter((p) => !arm64KeepPackages.includes(p))
|
|
.map((p) => '!node_modules/' + p + '/**')
|
|
|
|
const x64KeepPackages = packages.filter((p) => p.includes('x64') && p.includes(platform))
|
|
const x64ExcludePackages = packages
|
|
.filter((p) => !x64KeepPackages.includes(p))
|
|
.map((p) => '!node_modules/' + p + '/**')
|
|
|
|
const excludeRipgrepFilters = ['arm64-darwin', 'arm64-linux', 'x64-darwin', 'x64-linux', 'x64-win32']
|
|
.filter((f) => {
|
|
// On Windows ARM64, also keep x64-win32 for emulation compatibility
|
|
if (platform === 'win32' && context.arch === Arch.arm64 && f === 'x64-win32') {
|
|
return false
|
|
}
|
|
return f !== `${arch}-${platform}`
|
|
})
|
|
.map((f) => '!node_modules/@anthropic-ai/claude-agent-sdk/vendor/ripgrep/' + f + '/**')
|
|
|
|
if (context.arch === Arch.arm64) {
|
|
await excludePackages([...arm64ExcludePackages, ...excludeRipgrepFilters])
|
|
} else {
|
|
await excludePackages([...x64ExcludePackages, ...excludeRipgrepFilters])
|
|
}
|
|
}
|