6

I have an electron application.

My package.json has some app-specific settings like serverUrl, type_of_application etc. I am using these settings in the application.

When building an application with electron-builder I want to override these configs key values with different values.

I will be building same application with different serverUrl for different type_of_application. Also there is possibility that these will be installed on same machine so I also want to change package name and productName key values for different builds.

So my target is to have separate config files with all these settings to build application with different settings. And when building I will use one of the the config file and that should override default configs in package.json file.

One possibility is to have complete package.json for different kind of builds, but in that case dependencies etc. keys will also be duplicated which will be hard to manage when a new dependency is added.

Is there any way I can achieve this?

Harshveer Singh
  • 4,037
  • 7
  • 37
  • 45
  • One option may be to insert the volatile metadata in the build process with the `extraMetadata` electron-builder option. If you start the build programmatically (javascript) instead of using the command line you could work dynamically with different configurations. – Rhayene Mar 12 '18 at 12:37

1 Answers1

1

One strategy would be to keep all app specific configuration in one or more separate config files which your app loads on startup.

I tend to keep such files under APP_HOMEDIR/etc (a nod to my Unix roots), named for each app environment you require.

Update

For Electron app development using electron-builder, a configuration key exists extraMetadata that allows you to inject values set in your package.json per build.

See electron-builder configuration for further info.

(Thanks to the OP Harshveer Singh for suggesting this update.)

Rob Raisch
  • 17,040
  • 4
  • 48
  • 58
  • This will apply after application is installed, right? I need to have different configurations and application using one of them. So that I can quickly build with different settings(without manually updating `package.json`), using something like: `npm build use-config1.json` – Harshveer Singh Mar 09 '18 at 19:11
  • Then you might wish to use a build tool, like Gulp, to manage building for different runtime configurations. – Rob Raisch Mar 10 '18 at 12:42
  • Yes, I think that's the way. For Electron app building, I found a build configuration key `extraMetadata` which overrides keys defined in `package.json`, so for for my case I am using that. If you can modify your response to answer build time configuration, I will accept that. – Harshveer Singh Mar 12 '18 at 06:45
  • @HarshveerSingh How does `extraMetadata` work exactly? The docs don't explain it at all. – Driezzz Sep 23 '19 at 15:07