Coder Social home page Coder Social logo

environment's Introduction

@aminnairi/environment

Easily parse your environment

minified size of the package vulnerability count according to Snyk.io NPM package's download count per month support of TypeScript

Summary

Requirements

Installation

$ npm install --save-exact @aminnairi/[email protected]

Usage

Simple

$ touch .env
USER=johndoe
$ touch index.js
const {Environment} = require("@aminnairi/environment");

const main = async () => {
    const environment = await Environment.from(".env");

    console.log(environment.USER); // johndoe
};

main().catch(error => console.error(error.message));

Fancy

$ touch .env
USER        =   johndoe
PASSWORD    =   robustpassword
$ touch index.js
const {Environment} = require("@aminnairi/environment");

const main = async () => {
    const environment = await Environment.from(".env");

    console.log(environment.USER); // johndoe
    console.log(environment.PASSWORD); // robustpassword
};

main().catch(error => console.error(error.message));

Interpollation

$ touch .env
USER        =   johndoe
PASSWORD    =   robustpassword
HOST        =   domain
DATBASE     =   database
URL         =   "$USER:$PASSWORD@$DOMAIN/$DATABASE"
$ touch index.js
const {Environment} = require("@aminnairi/environment");

const main = async () => {
    const environment = await Environment.from(".env");

    console.log(environment.USER); // johndoe
    console.log(environment.PASSWORD); // robustpassword
    console.log(environment.DOMAIN); // domain
    console.log(environment.DATABASE); // database
    console.log(environment.URL); // johndoe:robustpassword@domain/database
};

main().catch(error => console.error(error.message));

Non-interpollation

$ touch .env
USER        =   johndoe
PASSWORD    =   robustpassword
HOST        =   domain
DATBASE     =   database
URL         =   '$USER:$PASSWORD@$DOMAIN/$DATABASE'
$ touch index.js
const {Environment} = require("@aminnairi/environment");

const main = async () => {
    const environment = await Environment.from(".env");

    console.log(environment.USER); // johndoe
    console.log(environment.PASSWORD); // robustpassword
    console.log(environment.DOMAIN); // domain
    console.log(environment.DATABASE); // database
    console.log(environment.URL); // $USER:$PASSWORD@$DOMAIN/$DATABASE
};

main().catch(error => console.error(error.message));

Multiline

$ touch .env
MOTD = Hello everyone \
        I Hope that you are okay today because \
        I'm felling great!
$ touch index.js
const {Environment} = require("@aminnairi/environment");

const main = async () => {
    const environment = await Environment.from(".env");

    console.log(environment.MOTD); // Hello everyone I Hope that you are okay today because I'm felling great!
};

main().catch(error => console.error(error.message));

Comments

$ touch .env
# This is our guild's name
GUILD_NAME = Outlawed

# This is our guild's main goal
GUILD_MAIN_OBJECTIVE = PVP

# This is our guild's secondary goal
GUILD_SECONDARY_OBJECTIVE = MM/HM
$ touch index.js
const {Environment} = require("@aminnairi/environment");

const main = async () => {
    const environment = await Environment.from(".env");

    console.log(environment.GUILD_NAME); // Outlawed
    console.log(environment.GUILD_MAIN_OBJECTIVE); // PVP
    console.log(environment.GUILD_SECONDARY_OBJECTIVE); // MM/HM
};

main().catch(error => console.error(error.message));

Undefined environment variables

$ touch .env
USER = johndoe
EMAIL = [email protected]
$ touch index.js
const {Environment} = require("@aminnairi/environment");

const main = async () => {
    const environment = await Environment.from(".env");

    console.log(environment.USER_NAME); // undefined
    console.log(environment.USER_EMAIL); // undefined
};

main().catch(error => console.error(error.message));

Multiple environments

$ touch index.js
const {Environment} = require("./environment.min.js");

const main = async () => {
    const [local, prod, test] = await Promise.all([
        Environment.from(".env.local"),
        Environment.from(".env.prod"),
        Environment.from(".env.test")
    ]);

    console.log(local);
    console.log(prod);
    console.log(test);
};

main().catch(error => console.error(error.message));

Preview

$ touch .env
USER=johndoe
PASSWORD=password
HOST=host
DATABASE=database
URL="$USER:$PASSWORD@$HOST/$DATABASE"
$ npx @aminnairi/environment .env
"USER" "johndoe"
"PASSWORD" "password"
"HOST" "host"
"DATABASE" "database"
"URL" "johndoe:password@host/database"

Error handling (async/await)

$ touch index.js
const {Environment} = require("@aminnairi/environment");

const main = async () => {
    try {
        const environment = await Environment.from("not-a-file.env");

        console.log(environment);
    } catch (error) {
        console.log("Something went wrong");
        console.error(error.message);
    }
};

main();

Error handling (Promise)

$ touch index.js
const {Environment} = require("@aminnairi/environment");

Environment.from("not-a-file.env").then(environment => {
    console.log(environment);
}).catch(error => {
    console.log("Something went wrong");
    console.error(error.message);
});

CHANGELOG

See CHANGELOG.md.

CONTRIBUTING

See CONTRIBUTING.md.

LICENSE

See LICENSE.

environment's People

Watchers

 avatar  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.