node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

hackee

hackee

Module to build node.js configuration using files, environment variables and command line arguments

require("hackee")

Example

module.exports = {
  host: 'localhost',
  port: 80,
  db: { connection: undefined },
  date: new Date('1999-12-31T00:00:00.000Z'),
  object: { a: 1, b: undefined, c: 3 },
  array: ['1', '2', undefined]
};
module.exports = {
  host: '127.0.0.1',
  port: 8080,
  db: { connection: 'string', client: 'client' },
  date: new Date('2017-01-01T00:00:00.000Z'),
  object: { a: 1, b: 2, c: 30 },
  array: ['1', '20', '3']
};
  • set environment variables
port=8080
db#connection=string
object#b=2
object#c=30
  • run app
node app --host=127.0.0.1 --date=2017-01-01T00:00:00.000Z --array[1]="""20""" --array[2]="""3"""
const hackee = require('hackee');
const assert = require('assert');
// initialize
const conf = hackee(require('./conf'));
conf.db.client = 'client';
assert.deepStrictEqual(conf, require('./expected'));
// use
require('./module');
const conf = require('hackee');
const assert = require('assert');
const expected = require('./expected');
const {
  host,
  port,
  db: { connection, client },
  date,
  object: { a, b, c },
  array: [first, second, third] 
= conf;
assert.strictEqual(host, expected.host);
assert.strictEqual(port, expected.port);
assert.strictEqual(connection, expected.db.connection);
assert.strictEqual(client, expected.db.client);
assert.deepStrictEqual(date, expected.date);
assert.strictEqual(a, expected.object.a);
assert.strictEqual(b, expected.object.b);
assert.strictEqual(c, expected.object.c);
assert.strictEqual(first, expected.array[0]);
assert.strictEqual(second, expected.array[1]);
assert.strictEqual(third, expected.array[2]);

Configuration factory

require("hackee")(json, opts)
@param {Object|Object[]} [json] - configuration object[s]
@param {Object} [opts] - options
@param {string|string[]} [opts.priority = ['argv', 'env', 'json']] - [process.argv, process.env, json]
@param {string|string[]} [opts.ignore] - 'argv' | 'env' | ['argv', 'env'] | ['env', 'argv']
@param {string} [opts.separator = '#'] - separator used for nested keys of process.env and process.argv ('a#b#c')
@param {string} [opts.argvPrefix = '--'] - prefix used for keys of process.argv (--arg)
@param {string} [opts.argvSeparator = '='] - separator used for keys of process.argv (--arg=value)
@param {string|number|boolean|null|undefined} [opts.required = undefined] - flag to throw error if value === opts.required
@param {string} [opts.indexPrefix = '['] - prefix used for indexes (--arg[0]=value)
@param {string} [opts.indexSuffix = ']'] - suffix used for indexes (--arg[0]=value)
@param {boolean} [opts.silent = true] - throw error `'${key}' required` if opts.silent === false && value === opts.required
@returns {Object}