|
| 1 | +import { existsSync } from "node:fs"; |
| 2 | +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | + |
| 7 | +// @ts-expect-error The electron-builder hook is an ESM script without declarations. |
| 8 | +import * as pruningHook from "../scripts/prune-packaged-architectures.mjs"; |
| 9 | + |
| 10 | +const { |
| 11 | + default: prunePackagedArchitectures, |
| 12 | + packageDirectoriesToPrune, |
| 13 | +} = pruningHook as unknown as { |
| 14 | + default: (context: { |
| 15 | + appOutDir: string; |
| 16 | + arch: number; |
| 17 | + electronPlatformName: string; |
| 18 | + packager: { appInfo: { productFilename: string } }; |
| 19 | + }) => Promise<void>; |
| 20 | + packageDirectoriesToPrune: (arch: number) => string[]; |
| 21 | +}; |
| 22 | + |
| 23 | +const temporaryDirectories: string[] = []; |
| 24 | + |
| 25 | +afterEach(async () => { |
| 26 | + await Promise.all( |
| 27 | + temporaryDirectories.splice(0).map((directory) => |
| 28 | + rm(directory, { recursive: true, force: true }), |
| 29 | + ), |
| 30 | + ); |
| 31 | +}); |
| 32 | + |
| 33 | +describe("desktop package architecture pruning", () => { |
| 34 | + it("maps each single-architecture macOS build to the opposite native packages", () => { |
| 35 | + expect(packageDirectoriesToPrune(3)).toEqual([ |
| 36 | + "@anthropic-ai/claude-agent-sdk-darwin-x64", |
| 37 | + "@esbuild/darwin-x64", |
| 38 | + "@remotion/compositor-darwin-x64", |
| 39 | + ]); |
| 40 | + expect(packageDirectoriesToPrune(1)).toEqual([ |
| 41 | + "@anthropic-ai/claude-agent-sdk-darwin-arm64", |
| 42 | + "@esbuild/darwin-arm64", |
| 43 | + "@remotion/compositor-darwin-arm64", |
| 44 | + ]); |
| 45 | + expect(packageDirectoriesToPrune(4)).toEqual([]); |
| 46 | + }); |
| 47 | + |
| 48 | + it("removes only the opposite-architecture packages from the staged macOS app", async () => { |
| 49 | + const appOutDir = await mkdtemp( |
| 50 | + join(tmpdir(), "clash-package-pruning-"), |
| 51 | + ); |
| 52 | + temporaryDirectories.push(appOutDir); |
| 53 | + const nodeModules = join( |
| 54 | + appOutDir, |
| 55 | + "Clash.app", |
| 56 | + "Contents", |
| 57 | + "Resources", |
| 58 | + "app.asar.unpacked", |
| 59 | + "node_modules", |
| 60 | + ); |
| 61 | + const x64Directories = packageDirectoriesToPrune(3); |
| 62 | + const preservedArm64 = join( |
| 63 | + nodeModules, |
| 64 | + "@anthropic-ai", |
| 65 | + "claude-agent-sdk-darwin-arm64", |
| 66 | + ); |
| 67 | + |
| 68 | + for (const packageName of [...x64Directories, "@anthropic-ai/claude-agent-sdk-darwin-arm64"]) { |
| 69 | + const packageDirectory = join(nodeModules, ...packageName.split("/")); |
| 70 | + await mkdir(packageDirectory, { recursive: true }); |
| 71 | + await writeFile(join(packageDirectory, "native-binary"), packageName); |
| 72 | + } |
| 73 | + |
| 74 | + await prunePackagedArchitectures({ |
| 75 | + appOutDir, |
| 76 | + arch: 3, |
| 77 | + electronPlatformName: "darwin", |
| 78 | + packager: { appInfo: { productFilename: "Clash" } }, |
| 79 | + }); |
| 80 | + |
| 81 | + for (const packageName of x64Directories) { |
| 82 | + expect( |
| 83 | + existsSync(join(nodeModules, ...packageName.split("/"))), |
| 84 | + ).toBe(false); |
| 85 | + } |
| 86 | + expect(existsSync(preservedArm64)).toBe(true); |
| 87 | + }); |
| 88 | +}); |
0 commit comments