@mamillastre/capacitor-environment
v6.1.0
Published
Capacitor plugin to get JSON based environment specific data
Downloads
32
Maintainers
Readme
Capacitor community
You can support this plugin to integrate the Capacitor Community by adding feedbacks or a 👍 reaction to this proposal.
Description
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
Maintainers
| Maintainer | GitHub | Social | | ----------------- | --------------------------------------------- | ------ | | Maxime Amillastré | mamillastre | |
Installation
npm install @mamillastre/capacitor-environment
npx cap sync
Configuration
This configuration guide conciders that you already followed the Create Environment Specific Configuration guide and created the Android product flavors & the iOS schemes.
Capacitor
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
iOS
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"
TypeScript
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;
}
}
Note for the Web
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": "/"
}
]
Example
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);
};
API
get(...)
get(options?: GetEnvironmentOptions | undefined) => Promise<EnvironmentData>
Returns the environment configuration.
| Param | Type |
| ------------- | ----------------------------------------------------------------------- |
| options
| GetEnvironmentOptions |
Returns: Promise<EnvironmentData>
Since: 1.0.0
setVersion(...)
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 |
Since: 1.0.0
Interfaces
EnvironmentData
The environment data as a JSON object.
To enable the autocompletion, this interface must be extended.
GetEnvironmentOptions
| Prop | Type | Description | Since |
| ------------- | ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| 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. | 1.0.0 |
SetVersionOptions
| Prop | Type | Description | Since |
| ------------- | ----------------------------- | ---------------------------------------------------------------------------------------------------------- | ----- |
| version
| string | number | The version number of the app. Provide this parameter to avoid to set this parameter on each "get()" call. | 1.0.0 |
CapacitorPluginConfiguration
| Prop | Type | Description |
| ------------------ | --------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
| environments
| EnvironmentConfigDeclarations | The environment configuration declarations.List all project available environments. |
EnvironmentConfigDeclarations
| 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. |
EnvironmentConfigInfo
| Prop | Type | Description |
| ---------- | ------------------- | ------------------------------------------------------------------------------------------- |
| path
| string | The relative path of your JSON environment configuration file from the root of the project. |