@aminnairi/environment
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

@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/environment@0.2.2

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 = johndoe@noreply.users.github.com
$ 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.

Readme

Keywords

Package Sidebar

Install

npm i @aminnairi/environment

Weekly Downloads

0

Version

0.2.2

License

MIT

Unpacked Size

11.6 kB

Total Files

6

Last publish

Collaborators

  • aminnairi