I am currently building a Backbone app wrapped up in Cordova and I have the need to change some variables for different environments. At present I need to go into the code and change the code manually for 3 or 4 variables depending on the environment I am attempting to build (different endpoints, etc.). It would be great if I could run
cordova build --environment staging
or something along those lines, then in the code I could check for the and use different settings depending on the environment command. Has anyone ever heard of this or if its possible? If not any suggestions besides my manual approach?
When you are creating a Cordova app > 3.0, you can create hooks on build events.
see a blog post on How to use hooks to automatically set environment variables in code.
It is also possible to create your own hook and copy the correct file with environment variables to the project folders (of Android, iOS, etc.). When you take this approach it is easiest if your environment variables are bundled in a single file of course.