spring
spring

Reputation: 18487

Env vars to change app name in project.json and set runtime vars?

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.

Upvotes: 2

Views: 912

Answers (2)

Taha
Taha

Reputation: 135

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.

Upvotes: 0

Akinjide
Akinjide

Reputation: 2753

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

Upvotes: 1

Related Questions