archappenv
v1.0.16
Published
Provides Application Environment Variables from *.appenv.js / .env and Utilities
Downloads
104
Readme
archappenv ·
Provides Application Environment Variables from *.appenv.js / .env and Utilities
Installation
npm i archappenv
Overview
Structure
The archappenv provides three modules: AppEnv, Services, and Utilities.
To access all modules:
// load it as an entry point of the whole module:
const { AppEnv } = require('archappenv');
const Services = AppEnv.Services;
const Utilities = AppEnv.Util;
To access each module:
// load it as separated module:
const AppEnv = require('archappenv/appenv');
const Services = require('archappenv/services');
const Utilities = require('archappenv/util');
Modules
AppEnv
The AppEnv module provides services to Application Environment Variables: load() and bind().
AppEnv.load(options)
It loads Application Environment Variables using Options or Configuration.
Options:
Options is an object that allows to customize how AppEnv is loaded.
- dir: defines the directory contained *.appenv.js files and appenv.config.json file. It can be absolute or relative path. If omitted, it'll use [application]/appenv/ as default directory.
- type: defined the [type].appenv.js file in dir directory. If omitted, it'll resolve a value from appenv.config.json file in dir directory.
appenv.config.json
It defines type depended to the name of server where it's located. If the name cannot be resolved, it'll use default.appenv.js.
module.exports = {
prod: [
"hostname.01",
"hostname.02",
"hostname.03",
],
dev: [
"hostname.04",
"hostname.05",
"hostname.06",
],
};
If the server name, either hostname.01, hostname.02, or hostname.03, , it'll resolve prod as type, and expect prod.appenv.js file to be resolved.
However, if the server name, either hostname.04, hostname.05, or hostname.06, it'll resolve dev as type, and expect dev.appenv.js file to be resolved.
Example:
Files:
In [application]/configs:
- appenv.config.json (as above)
- prod.appenv.js
- dev.appenv.js
- custom.appenv.js
Note: If these four files were located in [application]/appenv directory, the options.dir could be omitted. However, they are, in this case, located in [application]/configs directory, the options.dir need to be defined.
Usage
// loads [application]/configs/prod.appenv.js
const prod = AppEnv.load({ type:'prod', dir: './configs' });
// loads [application]/configs/dev.appenv.js
const dev = AppEnv.load({ type:'dev', dir: './configs' });
// loads [application]/configs/custom.appenv.js
const custom = AppEnv.load({ type:'custom', dir: './configs' });
// loads [application]/configs/prod.appenv.js in "hostname.01" server
const prod = AppEnv.load({ dir: './configs' });
// loads [application]/appenv/prod.appenv.js in "hostname.01" server
const prod = AppEnv.load();
// loads [application]/appenv/dev.appenv.js in "hostname.04" server
const dev = AppEnv.load();
// loads [application]/appenv/default.appenv.js in "not.in.list.hostname" server
const default = AppEnv.load();
AppEnv.bind()
It binds the resolved Application Environment Variables to process.env.
// in resolved appenv.js file:
module.exports = {
APP_TITLE: "Application Title",
APP_USERS: [
"user.01",
"user.02",
"user.03",
],
};
Usage
AppEnv.bind();
const title = process.env.APP_TITLE;
// title: "Application Title"
const users = process.env.APP_USERS;
// users: ["user.01", "user.02", "user.03"]