Skip to content
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

fix(pkg): provide BundlePreInstallScriptPath and/or BundlePostInstallScriptPath #8071

Merged
merged 1 commit into from Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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