pigura
v1.0.1
Published
[](https://badge.fury.io/js/pigura)
Maintainers
Readme
Pigura
Simple configuration file loader.
What is It?
Let's say you have a file named
config.jsoncontaining your app settings.You have a file named
config.prod.jsoncontaining your app settings for PRODUCTION.Running your app on development mode, will only load
config.json.Running your app in production, will load both
config.json, MERGED withconfig.prod.jsonYou dont need to copy all the settings from
config.jsontoconfig.prod.json. Just the settings to override. Removing the need to maintain both files in sync.
Install
Choose 1 of 3 options:
Install from NPM:
npm install piguraInstall latest from GitHub:
npm install github:kosinix/piguraTied to a specific version/release from GitHub:
npm install github:kosinix/pigura#1.0.0Quickstart
You only need to provide 3 parameters:
configName - The path to your config file relative to your app's root dir
appDir - App's root dir
env - The env variable. The default value is "dev". Any other value will load the config file + config.env file. See Convention.
logging (optional) - true to use console.log
const pigura = require('pigura'); let configLoader = new pigura.ConfigLoader({ configName: './config.json', appDir: path.resolve(__dirname), env: 'dev' }) let config = configLoader.getConfig(); console.log(config)
Examples
The recommended way to switch between environments is to use NODE_ENV. There are other ways as well but pigura does not care how you get the env variable.
Production Environment using NPM Script
In your package.json scripts section
{
...
"scripts": {
"prod": "set NODE_ENV=prod&& node index.js",
}
...
}In the command line you type:
npm run prodThen in your main file:
let configLoader = new pigura.ConfigLoader({
configName: 'config.json',
appDir: path.resolve(__dirname),
env: process.env.NODE_ENV // Pass to pigura
})
let config = configLoader.getConfig();
console.log(config)This will:
- Read the contents of
config.json - Read
config.prod.jsonand merged it withconfig.json. The same settings inconfig.prod.jsonwill override the settings inconfig.json
Get Base Config Only
You can get the base config only, disregarding the env variable, and child configs:
let config = configLoader.getConfig(true);Convention
Naming
Base config:
`${configName}.json` - config.jsonChild configs:
`${configName}.${env}.json` - config.sandbox.jsonWhy "." as separator for env?
So you can name your configs with dash separators:
config-foo-bar.json
config-foo-bar.sandbox.json
config-foo-bar.prod.jsonOr:
configFooBar.json
configFooBar.sandbox.json
configFooBar.prod.jsonTest
Install mocha globally
npm install mocha -gRun test
npm test