@@ -9,6 +9,7 @@ import { filterCFBundleIdentifier } from "../appInfo"
99import { findIdentity , Identity } from "../codeSign/macCodeSign"
1010import { Target } from "../core"
1111import MacPackager from "../macPackager"
12+ import { readdirSync } from "fs"
1213
1314const certType = "Developer ID Installer"
1415
@@ -144,8 +145,9 @@ export class PkgTarget extends Target {
144145 const plistInfo = ( await executeAppBuilderAsJson < Array < any > > ( [ "decode-plist" , "-f" , propertyListOutputFile ] ) ) [ 0 ] . filter (
145146 ( it : any ) => it . RootRelativeBundlePath !== "Electron.dSYM"
146147 )
148+ let packageInfo : any = { }
147149 if ( plistInfo . length > 0 ) {
148- const packageInfo = plistInfo [ 0 ]
150+ packageInfo = plistInfo [ 0 ]
149151
150152 // ChildBundles lists all of electron binaries within the .app.
151153 // There is no particular reason for removing that key, except to be as close as possible to
@@ -167,22 +169,35 @@ export class PkgTarget extends Target {
167169 if ( options . overwriteAction != null ) {
168170 packageInfo . BundleOverwriteAction = options . overwriteAction
169171 }
170-
171- await executeAppBuilderAndWriteJson ( [ "encode-plist" ] , { [ propertyListOutputFile ] : plistInfo } )
172172 }
173173
174174 // now build the package
175175 const args = [ "--root" , rootPath , "--identifier" , this . packager . appInfo . id , "--component-plist" , propertyListOutputFile ]
176176
177177 use ( this . options . installLocation || "/Applications" , it => args . push ( "--install-location" , it ) )
178- if ( options . scripts != null ) {
179- args . push ( "--scripts" , path . resolve ( this . packager . info . buildResourcesDir , options . scripts ) )
180- } else if ( options . scripts !== null ) {
181- const dir = path . join ( this . packager . info . buildResourcesDir , "pkg-scripts" )
182- const stat = await statOrNull ( dir )
183- if ( stat != null && stat . isDirectory ( ) ) {
184- args . push ( "--scripts" , dir )
185- }
178+
179+ // nasty nested ternary-statement, probably should optimize
180+ const scriptsDir =
181+ // user-provided scripts dir
182+ options . scripts != null
183+ ? path . resolve ( this . packager . info . buildResourcesDir , options . scripts )
184+ : // fallback to default unless user explicitly sets null
185+ options . scripts !== null
186+ ? path . join ( this . packager . info . buildResourcesDir , "pkg-scripts" )
187+ : null
188+ if ( scriptsDir && ( await statOrNull ( scriptsDir ) ) ?. isDirectory ( ) ) {
189+ const dirContents = readdirSync ( scriptsDir )
190+ dirContents . forEach ( name => {
191+ if ( name . includes ( "preinstall" ) ) {
192+ packageInfo . BundlePreInstallScriptPath = name
193+ } else if ( name . includes ( "postinstall" ) ) {
194+ packageInfo . BundlePostInstallScriptPath = name
195+ }
196+ } )
197+ args . push ( "--scripts" , scriptsDir )
198+ }
199+ if ( plistInfo . length > 0 ) {
200+ await executeAppBuilderAndWriteJson ( [ "encode-plist" ] , { [ propertyListOutputFile ] : plistInfo } )
186201 }
187202
188203 args . push ( packageOutputFile )
0 commit comments