differentiate between make and package in Electron Forge config

Viewed 1492

I have an Electron Forge config file set up with many options and it all works automagically and beautifully (thanks Forge team!!). But I have found certain situations where I might want to handle a bare npm run package differently than a full npm run make (which as I understand it runs the package script as part of its process). Is there any way to programmatically detect whether the package action was run direct from the command line rather than as part of the make process, so that I can invoke different Forge configuration options depending? For example sometimes I just want to do a quick build for local testing and skip certain unnecessary time-consuming steps such as notarizing on macOS and some prePackage/postPackage hook functions. Ideally I'm looking for a way to do something like this in my Forge config file:

//const isMake = ???
module.exports = {
    packagerConfig: {
        osxNotarize: isMake ? {appleId: "...", appleIdPassword: "..."} : undefined
    },
    hooks: {
        prePackage: isMake ? someFunction : differentFunction
    }
}
1 Answers

You can do it by process.argv[1]:

let isMake;
if (process.argv[1].endsWith('electron-forge-make.js') {
  isMake = true;
} else {
  isMake = false;
}
module.exports = {
  // ...
}

When calling process.argv, it returns an array with two strings: The first one with node.js directory and the second one with electron forge module directory. The make module ends with electron-forge-make.js and package module ends with electron-forge-package.js. So you can look at the end of it and determine whether it's package or make.

Related