env-to-env

0.2.0 • Public • Published

env-to-env

Last Release Version License Build Status Coverage Downloads

Include environment variables to be automatically set on start.

Installation

$ npm install -g env-to-env

Usage

As Command-line Tool

You will need to install env-to-env as a global dependency.

Before deployment, run env-to-env inject --keys [...] with the environment variables you want to deploy as arguments. If running without options, env-to-env inject will parse ENV_TO_ENV environment variable and treat value as a JSON Array with environment variable names. You can also set a JSON Object with key-values to inject. The options --keys expects a string with comma-separated environment variable names.

$ env-to-env inject [options]

For example,

$ env-to-env inject --keys DEBUG,PORT,HOST_IP

With Travis CI

Create a .travis.yml file in your repository and setup env-to-env according to command-line usage.

language: node_js
script:
  - npm test
env:
  global:
    - ENV_TO_ENV=['ENV_KEY_1', 'ENV_KEY_2', '...', 'ENV_KEY_N']
    - ENV_KEY_1='value 1'
    - ENV_KEY_2='value 2'
    # ...
    - ENV_KEY_N='value n'
before_deploy: 
  - npm install -g env-to-env
  - env-to-env
deploy:
  # Your deployment configuration

As Package Dependency

Install env-to-env as a package dependency. Save using --save or --save-dev depending on your needs.

$ npm install env-to-env --save-dev

Require env-to-env dependency

import env2env from 'env-to-env';

/* TO BE WRITTEN */

API

Constructor

You may create your env-to-env instance by passing an array of environment variable names (let's call them keys)

import env2env from 'env-to-env';

const env = env2env(['ENV_KEY_1', 'ENV_KEY_2', /* ..., */ 'ENV_KEY_N']);

or by passing an Object with options.

import env2env from 'env-to-env';

const envOptions = { 
  keys: ['ENV_KEY_1', 'ENV_KEY_2', /* ..., */ 'ENV_KEY_N']
};
const env = env2env(options);

Options

Here is an example that creates an Object with all the options available and their defaults.

const defaultOptions = {

  // The list of environment variable names to include in deployment.
  // E.g. ['ENV_KEY_1', 'ENV_KEY_2', /* ..., */ 'ENV_KEY_N']
  keys : [],

  // The name of the script that will prepare the deployment environment

  setEnvScript: '.env.sh',

  // The name of the script that will call the application startup script.
  startAppScript : '.start.sh',

  // The access point for environment variables object.
  env: process.env


};

Methods

This is a list of all public methods of an env-to-env instance.

env2env.object()

Returns an object with all the environment information to be transferred.

This object has environment variable names as keys and strings as values.

Example:

{
  'ENV_KEY_1' : 'value 1',
  'ENV_KEY_2' : 'value 2',
  /* ... */
  'ENV_KEY_N' : 'value N'
}

env2env.json()

Returns a String with a JSON Object with all the environment information to be transferred.

Is the result of stringifying env2env.object() method output.

Example:

'{ "ENV_KEY_1" : "value 1", "ENV_KEY_2" : "value 2", "ENV_KEY_N" : "value N" }'

env2env.bash()

Returns a String with the bash script to setup environment.

#!/bin/bash
ENV_KEY_1='value 1'
ENV_KEY_2='value 2'
# ...
ENV_KEY_N='value N'

env2env.script()

Returns a String with the bash script to setup environment.

Right now this is just an alias for env2env.bash() but it should become cross-platform in a future.

env2env.inject()

Creates options.setEnvScript and options.startAppScript files and modifies package.json to execute options.startAppScript file as start script.

Questions

If you are having difficulties using env-to-env, please ask a question on Stack Overflow.

Examples

The examples folder has basic and advanced examples.

License

This library is licensed under Apache 2.0. Full license text is available in COPYING.

Contributing

See CONTRIBUTING.

Package Sidebar

Install

npm i env-to-env

Weekly Downloads

3

Version

0.2.0

License

Apache-2.0

Last publish

Collaborators

  • arypbatista