Electron-builder: afterPack Usage in Build Config

Created on 1 Nov 2017  路  6Comments  路  Source: electron-userland/electron-builder


  • Version: 19.43.3

  • Target: Windows


We need to call a script that generates and saves binary signatures for several Electron files after packing but before it gets packaged into an distributable installer.

Is the afterPack option only available when using the programmatic API, or can it be called from a build configuration specified in a package.json file? The description for that option is a little unclear:

The function (or path to file or module id) to be run after pack (but before pack into distributable format and sign).

By saying "path to file", it seems one could set the path to a script file; however, I received the error configuration.afterPack should pass "typeof" keyword validation when I specified the option in our existing build config.

"build": {
    "appId": "com.app.id",
    "asar": true,
    "copyright": "漏 Company Info",
    "directories": {
      "app": "./",
      "buildResources": "resources/",
      "output": "dist/"
    },
    "extends": null,
    "forceCodeSigning": true,
    "files": [
      "package.json",
      ".babelrc",
      "build-timestamp.json",
      "init.js",
      "main.js",
      "build/",
      "js/",
      "scripts/",
      "node_modules/"
    ],
    "afterPack": "./update-signatures.js",
    "publish": [
      {
        "provider": "s3",
        "bucket": "company-app-bucket",
        "path": "releases/desktop/updates"
      }
    ],
    "win": {
      "target": [
        "nsis",
        "zip"
      ],
      "artifactName": "installer_v${version}_${arch}.${ext}",
      "icon": "resources/icons/icon.ico"
    },
    "nsis": {
      "oneClick": false,
      "perMachine": true,
      "allowElevation": true,
      "allowToChangeInstallationDirectory": true,
      "deleteAppDataOnUninstall": true,
      "artifactName": "installer_v${version}_${arch}_installer.${ext}",
      "installerHeader": "resources/images/installerHeader.bmp",
      "installerHeaderIcon": "resources/icons/icon.ico",
      "installerSidebar": "resources/images/installerSidebar.bmp",
      "uninstallerSidebar": "resources/images/installerSidebar.bmp"
    }
}
bug

Most helpful comment

Function must be exported:

exports.default = async function(context) {
  console.log("\n- [INFO] Testing afterPack functionality\n");
}

All 6 comments

Thanks for donation, fixed in 19.43.4

@develar Thanks! However, now code signing fails immediately afterwards, regardless of the script executed by afterPack. I've included the content of the file called by afterPack as well as the debug build log where the error occurred:

update-signatures.js:

console.log("\n- [INFO] Testing afterPack functionality\n");

Output:

[...]

electron-builder 7za.exe (45192) exited with exit code 0 +2s

- [INFO] Testing afterPack functionality

electron-builder Executing powershell.exe (Get-PfxData "C:\Users\dpesses\Documents\Electron\CodeSignCertificate.pfx" -Password (ConvertTo-SecureString -String 35a4015d3eea1f349bf8e40215b75d320a2610df6fec7e386e1077f429022d15 (sha256 hash) -Force -AsPlainText)).EndEntityCertificates.Subject +21s
electron-builder CN=Redacted Company Name Inc., O=Redacted Company Name Inc., STREET=1938 Sullivan Lane, L=Metropolis, S=DE, PostalCode=33866, C=US

electron-builder  +360ms
TypeError: it is not a function
  at default.each.it (C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\src\packager.ts:354:49)
From previous event:
  at Packager.afterPack (C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\src\packager.ts:354:28)
  at C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\src\platformPackager.ts:207:21
  at Generator.next (<anonymous>)
  at runCallback (timers.js:672:20)
  at tryOnImmediate (timers.js:645:5)
  at processImmediate [as _immediateCallback] (timers.js:617:5)
From previous event:
  at WinPackager.doPack (C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\out\platformPackager.js:267:11)
  at C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\src\platformPackager.ts:115:16
  at Generator.next (<anonymous>)
From previous event:
  at WinPackager.pack (C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\out\platformPackager.js:190:11)
  at C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\src\packager.ts:260:24
From previous event:
  at Packager.doBuild (C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\out\packager.js:332:11)
  at C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\src\packager.ts:186:52
  at Generator.next (<anonymous>)
From previous event:
  at Packager.build (C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\out\packager.js:261:11)
  at C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\src\builder.ts:283:40
  at Generator.next (<anonymous>)
  at build (C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\out\builder.js:63:21)
  at then (C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\src\cli\cli.ts:49:4)
From previous event:
  at build (C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\out\builder.js:63:21)
  at then (C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\out\cli\cli.js:127:128)
  at runCallback (timers.js:672:20)
  at tryOnImmediate (timers.js:645:5)
  at processImmediate [as _immediateCallback] (timers.js:617:5)
From previous event:
  at Object.args [as handler] (C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\out\cli\cli.js:127:117)
  at Object.runCommand (C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\yargs\lib\command.js:228:22)
  at Object.parseArgs [as _parseArgs] (C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\yargs\yargs.js:1041:24)
  at Object.get [as argv] (C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\yargs\yargs.js:957:21)
  at Object.<anonymous> (C:\Users\dpesses\Documents\Electron\Electron-App\node_modules\electron-builder\out\cli\cli.js:123:441)
  at Module._compile (module.js:571:32)
  at Object.Module._extensions..js (module.js:580:10)
  at Module.load (module.js:488:32)
  at tryModuleLoad (module.js:447:12)
  at Function.Module._load (module.js:439:3)
  at Module.runMain (module.js:605:10)
  at run (bootstrap_node.js:427:7)
  at startup (bootstrap_node.js:151:9)
  at bootstrap_node.js:542:3

Function must be exported:

exports.default = async function(context) {
  console.log("\n- [INFO] Testing afterPack functionality\n");
}

Ah, of course. Thank you @develar!

We have docs and example code for win.sign, but not for these functions :(

As a second user in the universe attempting to do this, I am seeing a similar issue on 19.55.3 while attempting a npm run dist.
Package.json:

{
    "name"          : "[...]",
    "description"   : "[...]",
    "version"       : "1.0.0",
    "author"        : "[...]",
    "main"          : "main.js",

    "devDependencies"   : {
        "electron" : "^1.8.1",
        "electron-builder" : "^19.55.3",
        "electron-packager" : "^10.1.2"
    },

    "productName"   : "[...]",

    "build" : { 
        "appId"         : "[...]",
        "copyright"     : "[...]",
        "asar"          : "true",
        "productName"   : "[...]",
        "win"       : {
            "target"    : "nsis"
        },
        "nsis"      : {
            "perMachine"    : true,
            "runAfterFinish"    : false,

            "include"       : "build/installer.nsh"
        },
        "afterPack": "build/afterPack.js",
        "compression"   : "maximum"
    },

    "scripts"   : {
        "pack"  : "build --dir",
        "dist"  : "build --win --x64"
    },

    "directories"   : {

    }
}

and, after the considerable wait in the initial packing (the custom after-pack and installer.nsh steps are to separate out and re-insert resources too large to fit in a typical installer exe), I get

TypeError: afterPack is not a function
at [...]\node_modules\electron-builder\src\platformPackager.ts:254:7

afterPack.js is formatted as recommended earlier in the thread:

const fs = require('fs');
exports.default = async function(afterPackContext) {
    console.log("\n- [INFO] Testing afterPack functionality - "+process.cwd+"\n");
    console.log("\n- [INFO] outDir: "+afterPackContext.outDir+"\n");
    console.log("\n- [INFO] appOutDir: "+afterPackContext.appOutDir+"\n");
    // [additional lines to perform file copies]
}

I have tried with both -g and locally installed electron-builder. This project is a refresh of a setup that originated pre-yarn, so it's all fairly vanilla npm (5.5.1)

Is this a regression, or am I doing something wrong here?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mstralka picture mstralka  路  3Comments

popod picture popod  路  3Comments

JohnWeisz picture JohnWeisz  路  3Comments

NPellet picture NPellet  路  3Comments

antonycourtney picture antonycourtney  路  3Comments