@mamillastre/capacitor-environment
Capacitor plugin to get JSON based environment specific data
You can support this plugin to integrate the Capacitor Community by adding feedbacks or a 👍 reaction to this proposal.
This plugin takes advantage of the iOS schemes & Android product flavors to provide a JSON configuration to the running web application.
This extra configuration improves your environment management if you followed the Create Environment Specific Configuration guide.
The advantages of using this plugin instead of managing the environment inside the web application:
- One web application build instead of one per environment
- Better development experience in the native IDEs by only switching the scheme/flavor
- On Android, build all the applications with one command (ex: gradlew bundleRelease) instead of one per environment
Maintainer | GitHub | Social |
---|---|---|
Maxime Amillastré | mamillastre |
npm install @mamillastre/capacitor-environment
npx cap sync
This configuration guide conciders that you already followed the Create Environment Specific Configuration guide and created the Android product flavors & the iOS schemes.
Add your environment information in the Capacitor plugin configuration Capacitor plugin configuration.
Example in capacitor.config.ts
:
/// <reference types="@mamillastre/capacitor-environment" />
import { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
plugins: {
Environment: {
environments: {
default: { path: 'path/to/my/environment.production.json' },
otherEnvironmentName: { path: 'path/to/an/other/environment.json' },
},
},
},
};
export default config;
Note
On project running Capacitor < 6.1, add this Capacitor hook, in your package.json scripts. It copy the environment configuration JSON files in the native platforms. This hook is managed by the plugin on Capacitor 6.1 and above.
"scripts": {
...,
"capacitor:copy:after": "npx capacitor-environment copy"
}
Run:
npx cap copy
Open up the Capacitor application’s iOS project in Xcode by running: npx cap open ios
.
Create a new environment
group without folder (Right-click on the App
group, under the App target, and select New Group without Folder from the context menu).
In the Finder, open the ios/App/App/environment
folder.
It contains all the copied configuration files sorted into named folders.
For each of the environment.json
files in these folders:
- Drag & drop the JSON files from Finder into the new
environment
group in Xcode. - In the add to the project options (automatically displayed by Xcode):
- Uncheck the "Copy items if needed"
- Check ONLY the target that corresponds to the environment file
- Press "Finish"
To allow TypeScript autocompletion, you must override the EnvironmentData interface in your app with your expected data.
Example:
environment.d.ts
import '@mamillastre/capacitor-environment';
declare module '@mamillastre/capacitor-environment' {
/** My app environment data */
export interface EnvironmentData {
/** The environment name */
name: string;
/** The environment endpoint URL */
endpoint: string;
}
}
This plugin creates the environment.json
files in the native project folders. These files are generated during the Capacitor copy task and can be ignored by adding the following lines at the end of the .gitignore
files:
On android/.gitignore
:
# The web environment configuration. Generated during the copy
app/src/*/assets/environment.json
On ios/.gitignore
:
# The web environment configuration. Generated during the copy
App/App/environment/*/environment.json
An environment.json
file must be available at the root your web application.
You must manage this file depending on the environment on your own.
Example on an Angular app:
You must add the asset copy on the wanted Angular configurations
"assets": [
{
"glob": "environment.json",
"input": "path/to/my/environment",
"output": "/"
}
]
import { Environment } from '@mamillastre/capacitor-environment';
const printEnvironmentData = async () => {
// Setting the environment version may be optional depending the cache
// configuration you applied to the 'environment.json' file
if (Capacitor.getPlatform() === 'web') {
await Environment.setVersion({ version: '1.0.0' });
}
// Get the environment data
const env = await Environment.get();
console.log('Environment data:', env);
};
get(options?: GetEnvironmentOptions | undefined) => Promise<EnvironmentData>
Returns the environment configuration.
Param | Type |
---|---|
options |
GetEnvironmentOptions |
Returns: Promise<EnvironmentData>
setVersion(options: SetVersionOptions) => Promise<void>
Set the app version.
Only available on web.
Allow to force the environment.json refresh when the file is cached by the browser.
Param | Type |
---|---|
options |
SetVersionOptions |
The environment data as a JSON object.
To enable the autocompletion, this interface must be extended.
Prop | Type | Description |
---|---|---|
version |
string | number |
The version number of the app. Only used on web. Allow to force the environment.json refresh when the file is cached by the browser. You can also call the "setVersion()" method to avoid to specify this parameter on each call. |
Prop | Type | Description |
---|---|---|
version |
string | number |
The version number of the app. Provide this parameter to avoid to set this parameter on each "get()" call. |
Prop | Type | Description |
---|---|---|
environments |
EnvironmentConfigDeclarations |
The environment configuration declarations. List all project available environments. |
Prop | Type | Description |
---|---|---|
default |
EnvironmentConfigInfo |
The mandatory default environment configuration. Usually the production configuration. Correspond to the main flavor on Android an the App target on iOS. |
[environmentName: string] |
EnvironmentConfigInfo |
The other environment configuration. You can add as many other environments as you want. Must be named like the used Android product flavor names. |
Prop | Type | Description |
---|---|---|
path |
string |
The relative path of your JSON environment configuration file from the root of the project. |