🐛 Fix pnpm packaging errors and refactor

This commit is contained in:
Tw93
2025-08-26 19:32:54 +08:00
parent 02463a72f6
commit 88690b15ea
6 changed files with 311 additions and 324 deletions

View File

@@ -187,26 +187,88 @@ export default abstract class BaseBuilder {
abstract getFileName(): string;
protected getBuildCommand(packageManager: string = 'pnpm'): string {
// 架构映射配置
protected static readonly ARCH_MAPPINGS: Record<
string,
Record<string, string>
> = {
darwin: {
arm64: 'aarch64-apple-darwin',
x64: 'x86_64-apple-darwin',
universal: 'universal-apple-darwin',
},
win32: {
arm64: 'aarch64-pc-windows-msvc',
x64: 'x86_64-pc-windows-msvc',
},
linux: {
arm64: 'aarch64-unknown-linux-gnu',
x64: 'x86_64-unknown-linux-gnu',
},
};
// 架构名称映射(用于文件名生成)
protected static readonly ARCH_DISPLAY_NAMES: Record<string, string> = {
arm64: 'aarch64',
x64: 'x64',
universal: 'universal',
};
/**
* 解析目标架构
*/
protected resolveTargetArch(requestedArch?: string): string {
if (requestedArch === 'auto' || !requestedArch) {
return process.arch;
}
return requestedArch;
}
/**
* 获取Tauri构建目标
*/
protected getTauriTarget(
arch: string,
platform: NodeJS.Platform = process.platform,
): string | null {
const platformMappings = BaseBuilder.ARCH_MAPPINGS[platform];
if (!platformMappings) return null;
return platformMappings[arch] || null;
}
/**
* 获取架构显示名称(用于文件名)
*/
protected getArchDisplayName(arch: string): string {
return BaseBuilder.ARCH_DISPLAY_NAMES[arch] || arch;
}
/**
* 构建基础构建命令
*/
protected buildBaseCommand(
packageManager: string,
configPath: string,
target?: string,
): string {
const baseCommand = this.options.debug
? `${packageManager} run build:debug`
: `${packageManager} run build`;
// Use temporary config directory to avoid modifying source files
const configPath = path.join(
npmDirectory,
'src-tauri',
'.pake',
'tauri.conf.json',
);
let fullCommand = `${baseCommand} -- -c "${configPath}"`;
const argSeparator = packageManager === 'npm' ? ' --' : '';
let fullCommand = `${baseCommand}${argSeparator} -c "${configPath}"`;
// For macOS, use app bundles by default unless DMG is explicitly requested
if (IS_MAC && this.options.targets === 'app') {
fullCommand += ' --bundles app';
if (target) {
fullCommand += ` --target ${target}`;
}
// Add features
return fullCommand;
}
/**
* 获取构建特性列表
*/
protected getBuildFeatures(): string[] {
const features = ['cli-build'];
// Add macos-proxy feature for modern macOS (Darwin 23+ = macOS 14+)
@@ -217,6 +279,27 @@ export default abstract class BaseBuilder {
}
}
return features;
}
protected getBuildCommand(packageManager: string = 'pnpm'): string {
// Use temporary config directory to avoid modifying source files
const configPath = path.join(
npmDirectory,
'src-tauri',
'.pake',
'tauri.conf.json',
);
let fullCommand = this.buildBaseCommand(packageManager, configPath);
// For macOS, use app bundles by default unless DMG is explicitly requested
if (IS_MAC && this.options.targets === 'app') {
fullCommand += ' --bundles app';
}
// Add features
const features = this.getBuildFeatures();
if (features.length > 0) {
fullCommand += ` --features ${features.join(',')}`;
}

View File

@@ -17,7 +17,7 @@ export default class LinuxBuilder extends BaseBuilder {
this.buildArch = 'arm64';
} else {
this.buildFormat = target;
this.buildArch = 'auto';
this.buildArch = this.resolveTargetArch('auto');
}
// Set targets to format for Tauri
@@ -28,14 +28,18 @@ export default class LinuxBuilder extends BaseBuilder {
const { name, targets } = this.options;
const version = tauriConfig.version;
// Determine architecture based on explicit target or auto-detect
// Determine architecture display name
let arch: string;
if (this.buildArch === 'arm64') {
arch = targets === 'rpm' || targets === 'appimage' ? 'aarch64' : 'arm64';
} else {
// Auto-detect or default to current architecture
arch = process.arch === 'x64' ? 'amd64' : process.arch;
if (arch === 'arm64' && (targets === 'rpm' || targets === 'appimage')) {
const resolvedArch = this.buildArch === 'x64' ? 'amd64' : this.buildArch;
arch = resolvedArch;
if (
resolvedArch === 'arm64' &&
(targets === 'rpm' || targets === 'appimage')
) {
arch = 'aarch64';
}
}
@@ -59,21 +63,22 @@ export default class LinuxBuilder extends BaseBuilder {
}
protected getBuildCommand(packageManager: string = 'pnpm'): string {
const baseCommand = this.options.debug
? `${packageManager} run build:debug`
: `${packageManager} run build`;
// Use temporary config directory to avoid modifying source files
const configPath = path.join('src-tauri', '.pake', 'tauri.conf.json');
let fullCommand = `${baseCommand} -- -c "${configPath}"`;
// Add ARM64 target if explicitly specified
if (this.buildArch === 'arm64') {
fullCommand += ' --target aarch64-unknown-linux-gnu';
}
// Only add target if it's ARM64
const buildTarget =
this.buildArch === 'arm64'
? this.getTauriTarget(this.buildArch, 'linux')
: undefined;
let fullCommand = this.buildBaseCommand(
packageManager,
configPath,
buildTarget,
);
// Add features
const features = ['cli-build'];
const features = this.getBuildFeatures();
if (features.length > 0) {
fullCommand += ` --features ${features.join(',')}`;
}
@@ -85,7 +90,8 @@ export default class LinuxBuilder extends BaseBuilder {
const basePath = this.options.debug ? 'debug' : 'release';
if (this.buildArch === 'arm64') {
return `src-tauri/target/aarch64-unknown-linux-gnu/${basePath}/bundle/`;
const target = this.getTauriTarget(this.buildArch, 'linux');
return `src-tauri/target/${target}/${basePath}/bundle/`;
}
return super.getBasePath();

View File

@@ -11,7 +11,12 @@ export default class MacBuilder extends BaseBuilder {
super(options);
// Store the original targets value for architecture selection
this.buildArch = options.targets || 'auto';
// For macOS, targets can be architecture names or format names
// Filter out non-architecture values
const validArchs = ['intel', 'apple', 'universal', 'auto', 'x64', 'arm64'];
this.buildArch = validArchs.includes(options.targets || '')
? options.targets
: 'auto';
// Use DMG by default for distribution
// Only create app bundles for testing to avoid user interaction
@@ -43,95 +48,51 @@ export default class MacBuilder extends BaseBuilder {
arch = 'x64';
} else {
// Auto-detect based on current architecture
arch = process.arch === 'arm64' ? 'aarch64' : process.arch;
arch = this.getArchDisplayName(this.resolveTargetArch(this.buildArch));
}
return `${name}_${tauriConfig.version}_${arch}`;
}
protected getBuildCommand(packageManager: string = 'pnpm'): string {
// Determine if we need universal build
const needsUniversal =
this.buildArch === 'universal' || this.options.multiArch;
if (needsUniversal) {
const baseCommand = this.options.debug
? `${packageManager} run tauri build -- --debug`
: `${packageManager} run tauri build --`;
// Use temporary config directory to avoid modifying source files
const configPath = path.join('src-tauri', '.pake', 'tauri.conf.json');
let fullCommand = `${baseCommand} --target universal-apple-darwin -c "${configPath}"`;
// Add features
const features = ['cli-build'];
// Add macos-proxy feature for modern macOS (Darwin 23+ = macOS 14+)
const macOSVersion = this.getMacOSMajorVersion();
if (macOSVersion >= 23) {
features.push('macos-proxy');
}
if (features.length > 0) {
fullCommand += ` --features ${features.join(',')}`;
}
return fullCommand;
private getActualArch(): string {
if (this.buildArch === 'universal' || this.options.multiArch) {
return 'universal';
} else if (this.buildArch === 'apple') {
// Build for Apple Silicon only
const baseCommand = this.options.debug
? `${packageManager} run tauri build -- --debug`
: `${packageManager} run tauri build --`;
const configPath = path.join('src-tauri', '.pake', 'tauri.conf.json');
let fullCommand = `${baseCommand} --target aarch64-apple-darwin -c "${configPath}"`;
// Add features
const features = ['cli-build'];
const macOSVersion = this.getMacOSMajorVersion();
if (macOSVersion >= 23) {
features.push('macos-proxy');
}
if (features.length > 0) {
fullCommand += ` --features ${features.join(',')}`;
}
return fullCommand;
return 'arm64';
} else if (this.buildArch === 'intel') {
// Build for Intel only
const baseCommand = this.options.debug
? `${packageManager} run tauri build -- --debug`
: `${packageManager} run tauri build --`;
const configPath = path.join('src-tauri', '.pake', 'tauri.conf.json');
let fullCommand = `${baseCommand} --target x86_64-apple-darwin -c "${configPath}"`;
return 'x64';
}
return this.resolveTargetArch(this.buildArch);
}
// Add features
const features = ['cli-build'];
const macOSVersion = this.getMacOSMajorVersion();
if (macOSVersion >= 23) {
features.push('macos-proxy');
}
if (features.length > 0) {
fullCommand += ` --features ${features.join(',')}`;
}
protected getBuildCommand(packageManager: string = 'pnpm'): string {
const configPath = path.join('src-tauri', '.pake', 'tauri.conf.json');
const actualArch = this.getActualArch();
return fullCommand;
const buildTarget = this.getTauriTarget(actualArch, 'darwin');
if (!buildTarget) {
throw new Error(`Unsupported architecture: ${actualArch} for macOS`);
}
return super.getBuildCommand();
let fullCommand = this.buildBaseCommand(
packageManager,
configPath,
buildTarget,
);
// Add features
const features = this.getBuildFeatures();
if (features.length > 0) {
fullCommand += ` --features ${features.join(',')}`;
}
return fullCommand;
}
protected getBasePath(): string {
const needsUniversal =
this.buildArch === 'universal' || this.options.multiArch;
const basePath = this.options.debug ? 'debug' : 'release';
const actualArch = this.getActualArch();
const target = this.getTauriTarget(actualArch, 'darwin');
if (needsUniversal) {
return `src-tauri/target/universal-apple-darwin/${basePath}/bundle`;
} else if (this.buildArch === 'apple') {
return `src-tauri/target/aarch64-apple-darwin/${basePath}/bundle`;
} else if (this.buildArch === 'intel') {
return `src-tauri/target/x86_64-apple-darwin/${basePath}/bundle`;
}
return super.getBasePath();
return `src-tauri/target/${target}/${basePath}/bundle`;
}
}

View File

@@ -9,61 +9,35 @@ export default class WinBuilder extends BaseBuilder {
constructor(options: PakeAppOptions) {
super(options);
// Store the original targets value for architecture selection
this.buildArch = options.targets || 'auto';
// Set targets to msi format for Tauri
this.buildArch = this.resolveTargetArch(options.targets);
this.options.targets = this.buildFormat;
}
getFileName(): string {
const { name } = this.options;
const language = tauriConfig.bundle.windows.wix.language[0];
// Determine architecture name based on explicit targets option or auto-detect
let targetArch: string;
if (this.buildArch === 'arm64') {
targetArch = 'aarch64';
} else if (this.buildArch === 'x64') {
targetArch = 'x64';
} else {
// Auto-detect based on current architecture if no explicit target
const archMap: { [key: string]: string } = {
x64: 'x64',
arm64: 'aarch64',
};
targetArch = archMap[process.arch] || process.arch;
}
const targetArch = this.getArchDisplayName(this.buildArch);
return `${name}_${tauriConfig.version}_${targetArch}_${language}`;
}
protected getBuildCommand(packageManager: string = 'pnpm'): string {
const baseCommand = this.options.debug
? `${packageManager} run build:debug`
: `${packageManager} run build`;
// Use temporary config directory to avoid modifying source files
const configPath = path.join('src-tauri', '.pake', 'tauri.conf.json');
let fullCommand = `${baseCommand} -- -c "${configPath}"`;
const buildTarget = this.getTauriTarget(this.buildArch, 'win32');
// Determine build target based on explicit targets option or auto-detect
let buildTarget: string;
if (this.buildArch === 'arm64') {
buildTarget = 'aarch64-pc-windows-msvc';
} else if (this.buildArch === 'x64') {
buildTarget = 'x86_64-pc-windows-msvc';
} else {
// Auto-detect based on current architecture if no explicit target
buildTarget =
process.arch === 'arm64'
? 'aarch64-pc-windows-msvc'
: 'x86_64-pc-windows-msvc';
if (!buildTarget) {
throw new Error(
`Unsupported architecture: ${this.buildArch} for Windows`,
);
}
fullCommand += ` --target ${buildTarget}`;
let fullCommand = this.buildBaseCommand(
packageManager,
configPath,
buildTarget,
);
// Add features
const features = ['cli-build'];
const features = this.getBuildFeatures();
if (features.length > 0) {
fullCommand += ` --features ${features.join(',')}`;
}
@@ -73,21 +47,7 @@ export default class WinBuilder extends BaseBuilder {
protected getBasePath(): string {
const basePath = this.options.debug ? 'debug' : 'release';
// Determine target based on explicit targets option or auto-detect
let target: string;
if (this.buildArch === 'arm64') {
target = 'aarch64-pc-windows-msvc';
} else if (this.buildArch === 'x64') {
target = 'x86_64-pc-windows-msvc';
} else {
// Auto-detect based on current architecture if no explicit target
target =
process.arch === 'arm64'
? 'aarch64-pc-windows-msvc'
: 'x86_64-pc-windows-msvc';
}
const target = this.getTauriTarget(this.buildArch, 'win32');
return `src-tauri/target/${target}/${basePath}/bundle/`;
}
}