2

I need to generate two apps from the same codebase (e.g. "pro" and "lite" versions). There are a lot of questions here about this but none I found involve node or electron.

I have only used env in development in very simple ways and after searching around, I haven't seen any mention of being able to use them in a deployed application.

So two tasks:
1. Changing the name of the app

So, using the package.json file with electron builder, I've tried to change the productName like this:

  "productName": process.env.APP_NAME,
  "main": "main.js",
  "scripts": {
    "package-mac": process.env.APP_NAME='Bingo' electron-packager . --overwrite  --platform=darwin --arch=x64  --prune=true --out=release-builds"
}

But that didn't work. Also saw this construction but it also didn't work:

  "productName": '${process.env.APP_NAME}',

Am I on the wrong track here?

2. Vars for use at runtime
To do the "pro" & "lite" thing, I need at least a flag to know how to configure things.
Are env vars in anyway suitable for this?

I guess if I am able to change the app name, I can access that at runtime but it seems like I am missing something important with all this.

1
  • 1
    As far as I'm aware you would have to build a shim/pass-through program that would alternate versions based on input. a.e. a .bat file or a separate node app that would determine and run the build differently. This is because there's no JS involved when it gets up and running for distribution, it's all configuration, so setting a flag in the config isn't possible. Commented Jul 3, 2019 at 21:29

2 Answers 2

1

Using dot-json, you can have npm scripts like:

  "productName": "Bingo",
  "main": "main.js",
  "scripts": {
    "package-mac": "echo $APP_NAME; dot-json package.json productName $APP_NAME --indent 2; electron-packager . --overwrite  --platform=darwin --arch=x64  --prune=true --out=release-builds"
  }

In Terminal, maybe, you can run

 $ APP_NAME='Bingo Pro' npm run package-mac
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply. I wasn't able to get that to work – got the "Usage" man page from dot-json.
0

You can use npm pkg scripts to get/set package.json attributes, its available natively and serve the exact purpose https://docs.npmjs.com/cli/v10/commands/npm-pkg

In package.json you can have custom scripts to update productName, something like

"scripts": {
  "build:pro":"npm pkg set productName=app-pro && electron-packager .",
  "build:lite":"npm pkg set productName=app-lite && electron-packager ."
}

and finally use the specific script in your CICD pipeline or on your computer locally.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.