0.2.2 • Published 5 years ago
@aminnairi/environment v0.2.2
@aminnairi/environment
Easily parse your environment
Summary
Requirements
Installation
$ npm install --save-exact @aminnairi/environment@0.2.2Usage
Simple
$ touch .envUSER=johndoe$ touch index.jsconst {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 .envUSER = johndoe
PASSWORD = robustpassword$ touch index.jsconst {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 .envUSER = johndoe
PASSWORD = robustpassword
HOST = domain
DATBASE = database
URL = "$USER:$PASSWORD@$DOMAIN/$DATABASE"$ touch index.jsconst {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 .envUSER = johndoe
PASSWORD = robustpassword
HOST = domain
DATBASE = database
URL = '$USER:$PASSWORD@$DOMAIN/$DATABASE'$ touch index.jsconst {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 .envMOTD = Hello everyone \
I Hope that you are okay today because \
I'm felling great!$ touch index.jsconst {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.jsconst {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 .envUSER = johndoe
EMAIL = johndoe@noreply.users.github.com$ touch index.jsconst {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.jsconst {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 .envUSER=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.jsconst {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.jsconst {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.