Skip to content

Commit

Permalink
fix(pkg): provide BundlePreInstallScriptPath and/or `BundlePostInst…
Browse files Browse the repository at this point in the history
…allScriptPath` when a pre/postinstall script is provided to pkg installer
  • Loading branch information
mmaietta committed Feb 22, 2024
1 parent 538dd86 commit 5a9b062
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-pumpkins-battle.md
@@ -0,0 +1,5 @@
---
"app-builder-lib": patch
---

fix(pkg): provide `BundlePreInstallScriptPath` and/or `BundlePostInstallScriptPath` when a pre/postinstall script is provided to pkg installer
2 changes: 1 addition & 1 deletion packages/app-builder-lib/src/platformPackager.ts
Expand Up @@ -762,7 +762,7 @@ async function resolveModule<T>(type: string | undefined, name: string): Promise
const isModuleType = type === "module"
try {
if (extension === ".mjs" || (extension === ".js" && isModuleType)) {
const fileUrl = pathToFileURL(name).href;
const fileUrl = pathToFileURL(name).href
return await eval("import('" + fileUrl + "')")
}
} catch (error) {
Expand Down
37 changes: 26 additions & 11 deletions packages/app-builder-lib/src/targets/pkg.ts
Expand Up @@ -9,6 +9,7 @@ import { filterCFBundleIdentifier } from "../appInfo"
import { findIdentity, Identity } from "../codeSign/macCodeSign"
import { Target } from "../core"
import MacPackager from "../macPackager"
import { readdirSync } from "fs"

const certType = "Developer ID Installer"

Expand Down Expand Up @@ -144,8 +145,9 @@ export class PkgTarget extends Target {
const plistInfo = (await executeAppBuilderAsJson<Array<any>>(["decode-plist", "-f", propertyListOutputFile]))[0].filter(
(it: any) => it.RootRelativeBundlePath !== "Electron.dSYM"
)
let packageInfo: any = {}
if (plistInfo.length > 0) {
const packageInfo = plistInfo[0]
packageInfo = plistInfo[0]

// ChildBundles lists all of electron binaries within the .app.
// There is no particular reason for removing that key, except to be as close as possible to
Expand All @@ -167,22 +169,35 @@ export class PkgTarget extends Target {
if (options.overwriteAction != null) {
packageInfo.BundleOverwriteAction = options.overwriteAction
}

await executeAppBuilderAndWriteJson(["encode-plist"], { [propertyListOutputFile]: plistInfo })
}

// now build the package
const args = ["--root", rootPath, "--identifier", this.packager.appInfo.id, "--component-plist", propertyListOutputFile]

use(this.options.installLocation || "/Applications", it => args.push("--install-location", it))
if (options.scripts != null) {
args.push("--scripts", path.resolve(this.packager.info.buildResourcesDir, options.scripts))
} else if (options.scripts !== null) {
const dir = path.join(this.packager.info.buildResourcesDir, "pkg-scripts")
const stat = await statOrNull(dir)
if (stat != null && stat.isDirectory()) {
args.push("--scripts", dir)
}

// nasty nested ternary-statement, probably should optimize
const scriptsDir =
// user-provided scripts dir
options.scripts != null
? path.resolve(this.packager.info.buildResourcesDir, options.scripts)
: // fallback to default unless user explicitly sets null
options.scripts !== null
? path.join(this.packager.info.buildResourcesDir, "pkg-scripts")
: null
if (scriptsDir && (await statOrNull(scriptsDir))?.isDirectory()) {
const dirContents = readdirSync(scriptsDir)
dirContents.forEach(name => {
if (name.includes("preinstall")) {
packageInfo.BundlePreInstallScriptPath = name
} else if (name.includes("postinstall")) {
packageInfo.BundlePostInstallScriptPath = name
}
})
args.push("--scripts", scriptsDir)
}
if (plistInfo.length > 0) {
await executeAppBuilderAndWriteJson(["encode-plist"], { [propertyListOutputFile]: plistInfo })
}

args.push(packageOutputFile)
Expand Down

0 comments on commit 5a9b062

Please sign in to comment.