Skip to content

Commit eb296c9

Browse files
authored
fix(pkg): provide BundlePreInstallScriptPath and/or BundlePostInstallScriptPath when a pre/postinstall script is provided to pkg installer (#8071)
1 parent 538dd86 commit eb296c9

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

.changeset/mighty-pumpkins-battle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"app-builder-lib": patch
3+
---
4+
5+
fix(pkg): provide `BundlePreInstallScriptPath` and/or `BundlePostInstallScriptPath` when a pre/postinstall script is provided to pkg installer

packages/app-builder-lib/src/platformPackager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ async function resolveModule<T>(type: string | undefined, name: string): Promise
762762
const isModuleType = type === "module"
763763
try {
764764
if (extension === ".mjs" || (extension === ".js" && isModuleType)) {
765-
const fileUrl = pathToFileURL(name).href;
765+
const fileUrl = pathToFileURL(name).href
766766
return await eval("import('" + fileUrl + "')")
767767
}
768768
} catch (error) {

packages/app-builder-lib/src/targets/pkg.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { filterCFBundleIdentifier } from "../appInfo"
99
import { findIdentity, Identity } from "../codeSign/macCodeSign"
1010
import { Target } from "../core"
1111
import MacPackager from "../macPackager"
12+
import { readdirSync } from "fs"
1213

1314
const 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

Comments
 (0)