1.0.0 • Published 8 years ago

@evoja/packageoid v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 years ago

packageoid npm version Build Status

Reads package.json as object and overrides it with process.env.npm_package_... values which could be defined in .npmrc files

packageoid

Reads config of the module and applies environment variables as user's replacement.

Assume our package.json has following config:

{
  "config": {
    "a": 1,
    "b": "2",
    "c": "m"
  },
  "x": "y",
  "script": {
    "sandbox": "node sandbox.js"
  }
}

And sandbox.js contains:

var packageoid = require('@evoja/packageoid')
var package_json = packageoid(module)

Let's call npm run sandbox. In this case package_json object will be equals

{
  config: {
    a: 1,
    b: '1',
    c: 'm'
  },
  x: 'y',
  script: {
    sandbox: 'node sandbox.js'
  }
}

The most interesting part is package_json.config which equals to

{
  a: 1,
  b: '1',
  c: 'm'
}

If we have the .npmrc file with following content:

fooproject:a=10
fooproject:b=10
fooproject:d=10

Then we have package_json.config equal to

{
  a: 10,
  b: '10',
  c: 'm'
}

We also may call the command: npm run sandbox --fooproject:a=20 --fooproject:b=20 package_json gets the content:

{
  a: 20,
  b: '20',
  c: 'm'
}

It tries to keep types of original fields.

package,    env => result

'a',        'b' => 'b'
'a',        '2' => '2'
 1,         'b' =>  1
 1,         '2' =>  2
{a: 10},    '2' => {a: 10}
[10, 'hi'], '2' => [10, 'hi']

Actually when config is set to null or undefined, then environment variable equals to empty string. In this case we get following:

 1,  '' => 0
'a', '' => ''

packageoid.merge

Takes two arguments default_conf and user_conf it makes a deep copy of the default_conf, and applies the user_conf to it. The user_conf overrides mentioned fields of the default_conf.

var packageoid = require('@evoja/packageoid')
var default_conf = {...}
var user_conf = {...}
var conf = packageoid.merge(default_conf, user_conf)

It has some rules. It does not replace anything by anything. It tries to keep types of original fields.

defaultuserresult
stringstringuser's string
stringnumber, object, arraytype cast user's value to string 1 -> '1'{} => '[object Object]'[10, 'hi'] => '10,hi'
numbernumberuser number
numberstringconverts user's string to number or keeps default on failure
numberobject, arraykeeps default
objectobjectdeep merge with user's object
objectnumber, string, arraykeeps default
arraynumber, stringkeeps default
arrayarrayreplace whole array with user's array, does not merge array values
arrayobjectbuilds new array where specific inicies were replaces with merges with user's values
valueundefinedkeeps default
valuenullis not specified yet, hovewer behaves somehow
undefined/unexistingvalueuser's value. Does not make deep copy

Examples

'a', 'b'        => 'b'
'a',  1         => '1'
'a', {}         => '[object Object]'
'a', [10, 'hi'] => '10,hi'`
 1,   2         =>  2
 1,  'a'|{}|[]  =>  1

{a: 10, b: '20', c: 30}, {a: '1', b: 2, d: 'x'} => {a: 1, b: '2', c: 30, d: 'x'}

{a: 10},             1|'a'|[]     => {a: 10}
[10, 'hi'],          1|'a'        => [10, 'hi']
[10, 'hi', {a: 30}], [1]          => [1]
[10, 'hi', {a: 30}], {2: {b: 40}} => [10, 'hi', {a: 30, b: 40}]