Giter VIP home page Giter VIP logo

detect-env's Introduction

DETECT-ENV

npm npm

Install

npm install detect-env

Usage

Use default config

import env from 'detect-env';

const NEXT_PAGE_URL = env({
  // if you want to set NET_PAGE_URL = 'prod.example.com'
  // process.env.NODE_ENV can be 'prod' or 'production' according to alias setting
  // but key's name mast be 'prod' according to alias setting
  // if you want to change it, set alias to what you want
  // for better performance, the neat code, the key name must be static
  // rather than automatically correct according to the alias
  prod: 'prod.example.com',
  test: 'test.example.com',
  default: 'dev.example.com',
});

// env.detect() is equal to env()
// I recommend you use env.detect() because it is more readable
// so you can write it as well
const NEXT_PAGE_URL = env.detect({
  prod: 'prod.example.com',
  test: 'test.example.com',
  default: 'dev.example.com',
});


// Suppose process.env.NODE_ENV = 'prod'
console.log(env.is.prod); // true
console.log(env.is.dev); // false
console.log(env.is.test); // false
console.log(env.is.stage); // false
console.log(env.is.local); // false


// You no longer need to write these obscure code
let NEXT_PAGE_URL;
switch(process.env.NODE_ENV) {
  case 'prod':
    NEXT_PAGE_URL = 'prod.example.com';
    break;
  case 'test':
    NEXT_PAGE_URL = 'test.example.com';
    break;
  default:
    NEXT_PAGE_URL = 'dev.example.com';
}

// Also equal to
const NEXT_PAGE_URL = process.env.NODE_ENV === 'prod' ?
  'prod.example.com' : process.env.NODE_ENV === 'test' ?
  'test.example.com' : 'dev.example.com';

// More example
const getGoodsPageUrl = env.detect({
  prod: id => `prod.example.com?${id}`,
  staging: id => `staging.example.com?${id}`,
  default:id => `dev.example.com?${id}`,
});

Custom Config

Custom Config can be use at version >= 2.0.0. Default config may not be able to meet everyone's needs. You can custom alias, shortcut and env variable.

// env.js
import { create } from 'detect-env';


export default create()
  .alias({
    // accept RegExp or function
    // function must return true or false
    prod: /^production$/,
    dev: /^develop(ment)?$/,
    stage: /^st$/,
    local: (envName) => envName === undefined || envName === null || /(locale)|(^$)/.test(envName),
    your_key: your_regexp_or_function,
  })
  .shortcut({
    // The return value of the function will be used to set env.isProd
    // if process.env.NODE_ENV = 'production'
    // envName = 'prod' because alias setting
    prod: (envName) => envName === 'prod',
    stage: (envName) => envName === 'stage',
    dev: (envName) => envName === 'dev',
    test: (envName) => envName === 'test',
    local: (envName) => envName === 'loacal',
  })
  .envVariable(() => process.env.NODE_ENV)
  // also accept string
  // .envVariable(process.env.NODE_ENV)

Then, you can use env in another file:

// index.js
import env from './env';

const NEXT_PAGE_URL = env.detect({
  prod: 'prod.example.com',
  test: 'test.example.com',
  default: 'dev.example.com',
});

detect-env's People

Contributors

val-istar-guo avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.