1.0.0 • Published 2 months ago

@mamillastre/capacitor-environment v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

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

MaintainerGitHubSocial
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 configuration of the plugin.

PropTypeDescription
environmentsEnvironmentConfigDeclarationsThe environment configuration declarations.List all project available environments.

EnvironmentConfigDeclarations

PropTypeDescription
defaultEnvironmentConfigInfoThe mandatory default environment configuration. Usually the production configuration.Correspond to the main flavor on Android an the App target on iOS.
[environmentName: string]EnvironmentConfigInfoThe other environment configuration.You can add as many other environments as you want.Must be named like the used Android product flavor names.

EnvironmentConfigInfo

PropTypeDescription
pathstringThe relative path of your JSON environment configuration file from the root of the project.

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;

Add the environment configuration files copy to you package.json scripts:

"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. Right-click the App group (under the App target) and select New Group from the context menu. Name this new group environment.

In the Finder, open the ios/App/App/environment folder. It contains all the copied configuration sorted into named folders. For each of the environment.json files in this folder:

  • Drag & drop the file into the new created group environment 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": "/"
  }
]

Usage

init(...)

init(options: InitEnvironmentOptions) => Promise<void>

Initialize the Environment plugin.

The call to this method is optional.

Only available on web.

ParamType
optionsInitEnvironmentOptions

get(...)

get(options?: GetEnvironmentOptions | undefined) => Promise<EnvironmentData>

Returns the environment configuration.

ParamType
optionsGetEnvironmentOptions

Returns: Promise<EnvironmentData>


Interfaces

InitEnvironmentOptions

PropTypeDescription
versionstring | numberThe version number of the app. Provide this parameter to avoid to set this parameter on each "get()" call.

EnvironmentData

The environment data as a JSON object.

To enable the autocompletion, this interface must be extended.

GetEnvironmentOptions

PropTypeDescription
versionstring | numberThe 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 "init()" method to avoid to specify this parameter.