问题
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:
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
来源:https://stackoverflow.com/questions/56878082/env-vars-to-change-app-name-in-project-json-and-set-runtime-vars