Diana Vallverdu
Diana Vallverdu

Reputation: 381

What is the best way to configure the pkg option scripts in electron-builder?

I am using the pkg option in my package.json configuration for electron-builder. I have some preinstall scripts that I want to run upon installation. I have followed the electron-builder instructions in order to set where the scripts are, but npm can't seem to find the files correctly.

I have tried changing ownership of files and folders with chmod +x file command, but the error persits.

This is the bit of code about pkg scripts configuration:

"build": {
    "mac": {
      "target":[
        "pkg"
      ]
    },
    "pkg":{
      "scripts":"build/pkg-scripts"
    }
 }

The error message I get is:

Error: Exit code: 1. Command failed: pkgbuild --root /Users/user/myApp/myApp-out/electron-builder/mac --component-plist /Users/user/myApp/myApp/electron-builder/com.dessci.myApp.plist --install-location /Applications --scripts /Users/user/myApp/build/build/pkg-scripts /Users/user/mtdesktop/mtdesktop-out/electron-builder/com.dessci.myApp.pkg
pkgbuild: error: Cannot write package to "/Users/user/mtdesktop/mtdesktop-out/electron-builder/com.dessci.myApp.pkg". (The file “pkg-scripts” couldn’t be opened.)

pkgbuild: Reading components from /Users/user/mtdesktop/mtdesktop-out/electron-builder/com.dessci.myApp.plist
pkgbuild: Adding component at myApp.app

pkgbuild: error: Cannot write package to "/Users/user/mtdesktop/mtdesktop-out/electron-builder/com.dessci.myApp.pkg". (The file “pkg-scripts” couldn’t be opened.)

    at /Users/user/mtdesktop/node_modules/builder-util/src/util.ts:126:16
    at ChildProcess.exithandler (child_process.js:306:5)
    at ChildProcess.emit (events.js:193:13)
    at maybeClose (internal/child_process.js:999:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:266:5)
From previous event:
    at /Users/user/mtdesktop/node_modules/graceful-fs/graceful-fs.js:111:16
    at /Users/user/mtdesktop/node_modules/graceful-fs/graceful-fs.js:45:10
    at FSReqCallback.args [as oncomplete] (fs.js:145:20)
From previous event:
    at PkgTarget.buildComponentPackage (/Users/user/mtdesktop/node_modules/app-builder-lib/src/targets/pkg.ts:115:113)
    at /Users/user/mtdesktop/node_modules/app-builder-lib/src/targets/pkg.ts:57:12
    at Generator.next (<anonymous>)
From previous event:
    at PkgTarget.build (/Users/user/mtdesktop/node_modules/app-builder-lib/src/targets/pkg.ts:31:42)
    at Function.buildAsyncTargets (/Users/user/mtdesktop/node_modules/app-builder-lib/src/platformPackager.ts:140:36)
    at MacPackager.packageInDistributableFormat (/Users/user/mtdesktop/node_modules/app-builder-lib/src/platformPackager.ts:119:24)
    at nonMasPromise.then.then (/Users/user/mtdesktop/node_modules/app-builder-lib/src/macPackager.ts:90:26)
    at processImmediate (internal/timers.js:443:21)
From previous event:
    at /Users/user/mtdesktop/node_modules/app-builder-lib/src/macPackager.ts:90:10
    at Generator.next (<anonymous>)
From previous event:
    at MacPackager.pack (/Users/user/mtdesktop/node_modules/app-builder-lib/src/macPackager.ts:80:95)
    at /Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:430:24
    at Generator.next (<anonymous>)
    at xfs.stat (/Users/user/mtdesktop/node_modules/fs-extra-p/node_modules/fs-extra/lib/mkdirs/mkdirs.js:56:16)
    at /Users/user/mtdesktop/node_modules/graceful-fs/polyfills.js:285:20
    at FSReqCallback.oncomplete (fs.js:159:5)
From previous event:
    at Packager.doBuild (/Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:396:24)
    at /Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:366:57
    at Generator.next (<anonymous>)
    at /Users/user/mtdesktop/node_modules/graceful-fs/graceful-fs.js:111:16
    at /Users/user/mtdesktop/node_modules/graceful-fs/graceful-fs.js:45:10
    at FSReqCallback.args [as oncomplete] (fs.js:145:20)
From previous event:
    at Packager._build (/Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:335:133)
    at /Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:331:23
    at Generator.next (<anonymous>)
    at processImmediate (internal/timers.js:443:21)
From previous event:
    at Packager.build (/Users/user/mtdesktop/node_modules/app-builder-lib/src/packager.ts:288:14)
    at build (/Users/user/mtdesktop/node_modules/app-builder-lib/src/index.ts:59:28)
    at build (/Users/user/mtdesktop/node_modules/electron-builder/src/builder.ts:227:10)
    at then (/Users/user/mtdesktop/node_modules/electron-builder/src/cli/cli.ts:46:19)

Upvotes: 4

Views: 4557

Answers (2)

Sabunkar Tejas Sahailesh
Sabunkar Tejas Sahailesh

Reputation: 4926

You need to provide the relative path of pkg-scripts for scripts value.

package.json

"build": {
"appId": "com.audio.application",
"productName": "Audio-App",
"artifactName": "${productName}-Setup-${version}.${ext}",
"copyright": "Copyright © 2020 Audio Corp",
"mac": {
  "category": "com.audio.application",
  "target": [
    "pkg"
  ],
  "icon": "dist",
  "identity": "identity",
  "darkModeSupport": true,
  "hardenedRuntime": true,
  "gatekeeperAssess": false,
  "artifactName": "${productName}.${ext}"
},
"pkg": {
  "scripts": "../build/pkg-scripts",
  "installLocation": "/Applications",
  "background": {
    "file": "build/icon/sound.png",
    "alignment": "bottomleft"
  },
  "allowAnywhere": true,
  "allowCurrentUserHome": true,
  "allowRootDirectory": true,
  "license": "build/license.html",
  "welcome": "build/resources/welcome.txt",
  "conclusion": "build/resources/conclusion.txt",
  "isVersionChecked": true,
  "isRelocatable": false,
  "overwriteAction": "upgrade"
},
"directories": {
  "buildResources": "release",
  "output": "release"
}
},

folder structure of build directory -
(which at root level of project)

enter image description here


preinstall.sh -
(make sure you have run chmod +x on this file to make it executable)

#!/bin/sh
echo "Executing preinstaller script for custom installer"

# Deleting App components from Application folder.
echo "Deleting Audio-Configration Logs"
logsPath=~/Library/Logs/Audio-Configration
if [ -d "$logsPath" ]
then
rm -rf ~/Library/Logs/Audio-Configration
echo "***Deleted Audio-Configration Logs Successfully***"
fi

echo "Deleting Audio-Configration Application Support"
applicationSupportPath=~/Library/Application\ Support/Audio-Configration
if [ -d "$applicationSupportPath" ]
then
rm -rf ~/Library/Application\ Support/Audio-Configration
echo "***Deleted Audio-Configration Application Support Successfully***"
fi

echo "Finished:preflight"
exit 0

Above pre-installation script is to make sure when user re-install the app, need to delete the previous Logs and application related content in Application Support directory

Upvotes: 5

vietthang
vietthang

Reputation: 1

"scripts":"build/pkg-scripts"

You don't need this line as it is a default directory for storing scripts commands, in the case you have define it, you need to create a directory name in ../build/pkg-scripts/ in your app directory and only file name "preinstall" and "postinstall" are allowed in this directory, otherwise electron-builder will get error why running the release function.

Upvotes: 0

Related Questions