11import { asArray , copyOrLinkFile , getPlatformIconFileName , InvalidConfigurationError , log , unlinkIfExists } from "builder-util"
22import { rename , utimes } from "fs/promises"
33import * as path from "path"
4+ import * as fs from "fs"
45import { filterCFBundleIdentifier } from "../appInfo"
56import { AsarIntegrity } from "../asar/integrity"
67import { MacPackager } from "../macPackager"
78import { normalizeExt } from "../platformPackager"
8- import { executeAppBuilderAndWriteJson , executeAppBuilderAsJson } from "../util/appBuilder "
9+ import { savePlistFile , parsePlistFile , PlistObject , PlistValue } from "../util/plist "
910import { createBrandingOpts } from "./ElectronFramework"
1011
1112function doRename ( basePath : string , oldName : string , newName : string ) {
@@ -22,11 +23,11 @@ function moveHelpers(helperSuffixes: Array<string>, frameworksPath: string, appN
2223}
2324
2425function getAvailableHelperSuffixes (
25- helperEHPlist : string | null ,
26- helperNPPlist : string | null ,
27- helperRendererPlist : string | null ,
28- helperPluginPlist : string | null ,
29- helperGPUPlist : string | null
26+ helperEHPlist : PlistObject | null ,
27+ helperNPPlist : PlistObject | null ,
28+ helperRendererPlist : PlistObject | null ,
29+ helperPluginPlist : PlistObject | null ,
30+ helperGPUPlist : PlistObject | null
3031) {
3132 const result = [ " Helper" ]
3233 if ( helperEHPlist != null ) {
@@ -69,38 +70,26 @@ export async function createMacApp(packager: MacPackager, appOutDir: string, asa
6970 const helperGPUPlistFilename = path . join ( frameworksPath , `${ electronBranding . productName } Helper (GPU).app` , "Contents" , "Info.plist" )
7071 const helperLoginPlistFilename = path . join ( loginItemPath , `${ electronBranding . productName } Login Helper.app` , "Contents" , "Info.plist" )
7172
72- const plistContent : Array < any > = await executeAppBuilderAsJson ( [
73- "decode-plist" ,
74- "-f" ,
75- appPlistFilename ,
76- "-f" ,
77- helperPlistFilename ,
78- "-f" ,
79- helperEHPlistFilename ,
80- "-f" ,
81- helperNPPlistFilename ,
82- "-f" ,
83- helperRendererPlistFilename ,
84- "-f" ,
85- helperPluginPlistFilename ,
86- "-f" ,
87- helperGPUPlistFilename ,
88- "-f" ,
89- helperLoginPlistFilename ,
90- ] )
73+ const safeParsePlistFile = async ( filePath : string ) : Promise < PlistObject | null > => {
74+ if ( ! fs . existsSync ( filePath ) ) {
75+ return null
76+ }
77+ return await parsePlistFile ( filePath )
78+ }
9179
92- if ( plistContent [ 0 ] == null ) {
80+ const appPlist = ( await safeParsePlistFile ( appPlistFilename ) ) !
81+ if ( appPlist == null ) {
9382 throw new Error ( "corrupted Electron dist" )
9483 }
9584
96- const appPlist = plistContent [ 0 ] !
97- const helperPlist = plistContent [ 1 ] !
98- const helperEHPlist = plistContent [ 2 ]
99- const helperNPPlist = plistContent [ 3 ]
100- const helperRendererPlist = plistContent [ 4 ]
101- const helperPluginPlist = plistContent [ 5 ]
102- const helperGPUPlist = plistContent [ 6 ]
103- const helperLoginPlist = plistContent [ 7 ]
85+ // Replace the multiple parsePlistFile calls with:
86+ const helperPlist = await safeParsePlistFile ( helperPlistFilename )
87+ const helperEHPlist = await safeParsePlistFile ( helperEHPlistFilename )
88+ const helperNPPlist = await safeParsePlistFile ( helperNPPlistFilename )
89+ const helperRendererPlist = await safeParsePlistFile ( helperRendererPlistFilename )
90+ const helperPluginPlist = await safeParsePlistFile ( helperPluginPlistFilename )
91+ const helperGPUPlist = await safeParsePlistFile ( helperGPUPlistFilename )
92+ const helperLoginPlist = await safeParsePlistFile ( helperLoginPlistFilename )
10493
10594 const buildMetadata = packager . config
10695
@@ -133,10 +122,12 @@ export async function createMacApp(packager: MacPackager, appOutDir: string, asa
133122 configureLocalhostAts ( appPlist )
134123 }
135124
136- helperPlist . CFBundleExecutable = `${ appFilename } Helper`
137- helperPlist . CFBundleDisplayName = `${ appInfo . productName } Helper`
138- helperPlist . CFBundleIdentifier = helperBundleIdentifier
139- helperPlist . CFBundleVersion = appPlist . CFBundleVersion
125+ if ( helperPlist != null ) {
126+ helperPlist . CFBundleExecutable = `${ appFilename } Helper`
127+ helperPlist . CFBundleDisplayName = `${ appInfo . productName } Helper`
128+ helperPlist . CFBundleIdentifier = helperBundleIdentifier
129+ helperPlist . CFBundleVersion = appPlist . CFBundleVersion
130+ }
140131
141132 /**
142133 * Configure bundleIdentifier for Electron 5+ Helper processes
@@ -222,39 +213,55 @@ export async function createMacApp(packager: MacPackager, appOutDir: string, asa
222213 )
223214
224215 // `CFBundleDocumentTypes` may be defined in `mac.extendInfo`, so we need to merge it in that case
225- appPlist . CFBundleDocumentTypes = [ ...( appPlist . CFBundleDocumentTypes || [ ] ) , ...documentTypes ]
216+ appPlist . CFBundleDocumentTypes = [ ...( ( appPlist . CFBundleDocumentTypes as PlistValue [ ] ) || [ ] ) , ...documentTypes ]
226217 }
227218
228- if ( asarIntegrity != null ) {
229- appPlist . ElectronAsarIntegrity = asarIntegrity
219+ const toPlistObject = ( asarIntegrity : AsarIntegrity ) : PlistObject => {
220+ const result : PlistObject = { }
221+ for ( const [ filePath , headerHash ] of Object . entries ( asarIntegrity ) ) {
222+ result [ filePath ] = {
223+ algorithm : headerHash . algorithm ,
224+ hash : headerHash . hash ,
225+ }
226+ }
227+ return result
230228 }
231229
232- const plistDataToWrite : any = {
233- [ appPlistFilename ] : appPlist ,
234- [ helperPlistFilename ] : helperPlist ,
230+ if ( asarIntegrity != null ) {
231+ appPlist . ElectronAsarIntegrity = toPlistObject ( asarIntegrity )
235232 }
233+
236234 if ( helperEHPlist != null ) {
237- plistDataToWrite [ helperEHPlistFilename ] = helperEHPlist
235+ await savePlistFile ( helperEHPlistFilename , helperEHPlist )
238236 }
237+
239238 if ( helperNPPlist != null ) {
240- plistDataToWrite [ helperNPPlistFilename ] = helperNPPlist
239+ await savePlistFile ( helperNPPlistFilename , helperNPPlist )
241240 }
241+
242242 if ( helperRendererPlist != null ) {
243- plistDataToWrite [ helperRendererPlistFilename ] = helperRendererPlist
243+ await savePlistFile ( helperRendererPlistFilename , helperRendererPlist )
244244 }
245+
245246 if ( helperPluginPlist != null ) {
246- plistDataToWrite [ helperPluginPlistFilename ] = helperPluginPlist
247+ await savePlistFile ( helperPluginPlistFilename , helperPluginPlist )
247248 }
249+
248250 if ( helperGPUPlist != null ) {
249- plistDataToWrite [ helperGPUPlistFilename ] = helperGPUPlist
251+ await savePlistFile ( helperGPUPlistFilename , helperGPUPlist )
250252 }
253+
251254 if ( helperLoginPlist != null ) {
252- plistDataToWrite [ helperLoginPlistFilename ] = helperLoginPlist
255+ await savePlistFile ( helperLoginPlistFilename , helperLoginPlist )
256+ }
257+
258+ await savePlistFile ( appPlistFilename , appPlist )
259+ if ( helperPlist != null ) {
260+ await savePlistFile ( helperPlistFilename , helperPlist )
253261 }
254262
255263 await Promise . all ( [
256- executeAppBuilderAndWriteJson ( [ "encode-plist" ] , plistDataToWrite ) ,
257- doRename ( path . join ( contentsPath , "MacOS" ) , electronBranding . productName , appPlist . CFBundleExecutable ) ,
264+ doRename ( path . join ( contentsPath , "MacOS" ) , electronBranding . productName , appPlist . CFBundleExecutable as string ) ,
258265 unlinkIfExists ( path . join ( appOutDir , "LICENSE" ) ) ,
259266 unlinkIfExists ( path . join ( appOutDir , "LICENSES.chromium.html" ) ) ,
260267 ] )
0 commit comments