Skip to content

refactor: improve resource directory handling for macOS #9038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 3, 2025
5 changes: 5 additions & 0 deletions .changeset/three-foxes-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-builder-lib": patch
---

refactor: improve resource directory handling for macOS
30 changes: 16 additions & 14 deletions packages/app-builder-lib/src/electron/ElectronFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,29 @@ async function removeUnusedLanguagesIfNeeded(options: BeforeCopyExtraFilesOption
return
}

const { dir, langFileExt } = getLocalesConfig(options)
const { dirs, langFileExt } = getLocalesConfig(options)
// noinspection SpellCheckingInspection
await asyncPool(MAX_FILE_REQUESTS, await readdir(dir), async file => {
if (!file.endsWith(langFileExt)) {
return
}
const deletedFiles = async (dir: string) => {
await asyncPool(MAX_FILE_REQUESTS, await readdir(dir), async file => {
if (path.extname(file) !== langFileExt) {
return
}

const language = file.substring(0, file.length - langFileExt.length)
if (!wantedLanguages.includes(language)) {
return fs.rm(path.join(dir, file), { recursive: true, force: true })
}
return
})
const language = path.basename(file, langFileExt)
if (!wantedLanguages.includes(language)) {
return fs.rm(path.join(dir, file), { recursive: true, force: true })
}
return
})
}
await Promise.all(dirs.map(deletedFiles))

function getLocalesConfig(options: BeforeCopyExtraFilesOptions) {
const { appOutDir, packager } = options
if (packager.platform === Platform.MAC) {
return { dir: packager.getResourcesDir(appOutDir), langFileExt: ".lproj" }
} else {
return { dir: path.join(packager.getResourcesDir(appOutDir), "..", "locales"), langFileExt: ".pak" }
return { dirs: [packager.getResourcesDir(appOutDir), packager.getMacOsElectronFrameworkResourcesDir(appOutDir)], langFileExt: ".lproj" }
}
return { dirs: [path.join(packager.getResourcesDir(appOutDir), "..", "locales")], langFileExt: ".pak" }
}
}

Expand Down
10 changes: 7 additions & 3 deletions packages/app-builder-lib/src/platformPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,13 +608,17 @@ export abstract class PlatformPackager<DC extends PlatformSpecificBuildOptions>
getResourcesDir(appOutDir: string): string {
if (this.platform === Platform.MAC) {
return this.getMacOsResourcesDir(appOutDir)
} else if (isElectronBased(this.info.framework)) {
}
if (isElectronBased(this.info.framework)) {
return path.join(appOutDir, "resources")
} else {
return appOutDir
}
return appOutDir
}

public getMacOsElectronFrameworkResourcesDir(appOutDir: string): string {
const electronFrameworkName = path.basename(this.info.framework.distMacOsAppName, ".app") + " " + "Framework.framework"
return path.join(appOutDir, `${this.appInfo.productFilename}.app`, "Contents", "Frameworks", electronFrameworkName, "Resources")
}
public getMacOsResourcesDir(appOutDir: string): string {
return path.join(appOutDir, `${this.appInfo.productFilename}.app`, "Contents", "Resources")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "TestApp",
"version": "1.1.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "TestApp",
"version": "1.1.0",
"license": "MIT",
"dependencies": {
"debug": "3.1.0",
"edge-cs": "1.2.1",
"foo": "file:C:\\Users\\payne\\AppData\\Local\\Temp\\t-W7LwTJ\\k\\foo"
}
},
"../k/foo": {
"version": "9.0.0",
"license": "MIT"
},
"node_modules/debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"license": "MIT",
"dependencies": {
"ms": "2.0.0"
}
},
"node_modules/edge-cs": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/edge-cs/-/edge-cs-1.2.1.tgz",
"integrity": "sha512-N5KAeMBhhCbCGmb5oiHJ0KcuTksZzo8lg+uByEUCZAdnLPHizHkk6ZIuuTD63eez7+W25ZSvsGS5+FtymxFoKw==",
"hasInstallScript": true,
"engines": {
"node": ">= 0.8"
}
},
"node_modules/foo": {
"resolved": "../k/foo",
"link": true
},
"node_modules/ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==",
"license": "MIT"
}
}
}
49 changes: 46 additions & 3 deletions test/snapshots/mac/macPackagerTest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,20 @@ exports[`two-package 3`] = `
`;

exports[`two-package 4`] = `
[
"Info.plist",
"MainMenu.nib",
"bn.lproj",
"chrome_100_percent.pak",
"chrome_200_percent.pak",
"en.lproj",
"icudtl.dat",
"resources.pak",
"v8_context_snapshot.x86_64.bin",
]
`;

exports[`two-package 5`] = `
{
"CFBundleDisplayName": "TestApp",
"CFBundleExecutable": "TestApp",
Expand Down Expand Up @@ -483,7 +497,7 @@ exports[`two-package 4`] = `
}
`;

exports[`two-package 5`] = `
exports[`two-package 6`] = `
[
"app-update.yml",
"app.asar",
Expand All @@ -493,7 +507,21 @@ exports[`two-package 5`] = `
]
`;

exports[`two-package 6`] = `
exports[`two-package 7`] = `
[
"Info.plist",
"MainMenu.nib",
"bn.lproj",
"chrome_100_percent.pak",
"chrome_200_percent.pak",
"en.lproj",
"icudtl.dat",
"resources.pak",
"v8_context_snapshot.arm64.bin",
]
`;

exports[`two-package 8`] = `
{
"CFBundleDisplayName": "TestApp",
"CFBundleExecutable": "TestApp",
Expand Down Expand Up @@ -540,7 +568,7 @@ exports[`two-package 6`] = `
}
`;

exports[`two-package 7`] = `
exports[`two-package 9`] = `
[
"app-update.yml",
"app.asar",
Expand All @@ -550,6 +578,21 @@ exports[`two-package 7`] = `
]
`;

exports[`two-package 10`] = `
[
"Info.plist",
"MainMenu.nib",
"bn.lproj",
"chrome_100_percent.pak",
"chrome_200_percent.pak",
"en.lproj",
"icudtl.dat",
"resources.pak",
"v8_context_snapshot.arm64.bin",
"v8_context_snapshot.x86_64.bin",
]
`;

exports[`yarn two package.json w/ native module 1`] = `
{
"mac": [
Expand Down
3 changes: 3 additions & 0 deletions test/src/mac/macPackagerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ test.ifMac("two-package", ({ expect }) =>
checkMacApp: async appDir => {
const resources = await fs.readdir(path.join(appDir, "Contents", "Resources"))
expect(resources.filter(it => !it.startsWith(".")).sort()).toMatchSnapshot()

const electronFrameworkResources = await fs.readdir(path.join(appDir, "Contents", "Frameworks", "Electron Framework.framework", "Resources"))
expect(electronFrameworkResources.filter(it => !it.startsWith(".")).sort()).toMatchSnapshot()
},
}
)
Expand Down
Loading