Skip to content

Environments

Matthieu Lemoine edited this page Oct 7, 2018 · 2 revisions

Immersive has built-in support for environments.

When working on a project, you often have to switch between several environments e.g development, staging and production.

Immersive allows your commands to be executed against the right environment without breaking a sweat.

Helpers

You can leverage the environments feature through the use of helpers. During Immersive setup, each helper is called once by environment and the result is hoisted. The right reference will be passed to your commands depending on the current environment.

Define environments

import immersive from 'immersive';

const config = {
  // Will be accessible from commands as argument
  helpers: {
    db, // will be called within the right environment
  },
  // Configuration will be passed to helpers based on the current environment
  environments: {
    development: { database: 'devdb' },
    staging: { database: 'stagingdb' },
    production: { database: 'proddb' },
  },
  // Define the current environment on CLI start
  // The current environment can be changed using the `env <envName>` command
  defaultEnvironment: 'development',
}

immersive(config);

// Will be called 3 times, once for each environment
function db(envConfig) {
  // Connect to the right database once during Immersive setup
  const database = connect(envConfig.database)
  return {
    get: id => database.users.get(id)
  };
}

Current environment

You can see in which environment your command will be executed directly in the prompt:

matthieu:development >

Switching environment

To switch from an environment to another, you have to call the env command:

env staging
Clone this wiki locally