resolver-revolver
Resolves and validates configurations.
Last updated 5 years ago by jpwesselink .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install resolver-revolver 
SYNC missed versions from official npm registry.

resolver-revolver

Codeship Status for sytac/gulp-commonjs-tasks

revolver

Description

Resolves values from and validates by trying to find values in given contexts in a lazy way.

Installation

$ npm install --save resolver-revolver

Since that is pretty generic, here's an example which resolves NODE_ENV from either the command line, then from the system environment, and if all else fails it will set it to a default of development.

If you'd like to try these examples, please run gulp prepare-examples first

// ./examples/easy/index.js

'use strict';

var resolverRevolver = require('resolver-revolver');
var argv = require('yargs').argv;
var isString = require('lodash.isstring');

var resolved = resolverRevolver.parse({
  context: {
    argv: argv,
    env: process.env
  },
  resolvables: {
    'environment': {
      from: ['argv.NODE_ENV', 'env.NODE_ENV'],
      default: 'development'
    }
  }
});

var defaults = {
  environment: resolved.environment()
};

console.log(JSON.stringify(defaults, null, 4));

Default value

Since argv.NODE_ENV and env.NODE_ENV can not be resolved round-robin style, the default will be resolved.

$ node examples/easy
{
    "environment": "development"
}

Use a cli argument

Since the first

$ node examples/easy --NODE_ENV=production
{
    "environment": "production"
}

Export a system variable

$ export NODE_ENV=coocoo
$ node examples/easy                           
{
    "environment": "coocoo"
}

Provide a cli argument and export a system variable

Since argv.NODE_ENV has a higher priority than env.NODE_ENV, it will be resolved.

$ export NODE_ENV=coocoo
$ node examples/easy --NODE_ENV=production                          
{
    "environment": "production"
}

Savvy? Here's a more elaborate example.

// ./examples/elaborate/index.js

'use strict';

var resolverRevolver = require('resolver-revolver');
var argv = require('yargs').argv;
var negate = require('lodash.negate');
var isUndefined = require('lodash.isundefined');
var isDefined = negate(isUndefined);

var resolved = resolverRevolver.parse({
  // Add a console for logging
  // defaults to undefined
  console: console,

  // Our context
  // defaults to {}
  context: {
    argv: argv,
    env: process.env
  },

  // when set to true it returns the default without validating
  lenientDefaults: false,

  // Resolvables
  resolvables: {
    'environment': {
      // defaults to []
      from: ['argv.NODE_ENV', 'env.NODE_ENV'],
      // defaults to undefined
      default: 'development',

      // And preconditions. If you do not set this array, it will default to
      // [isDefined].
      //
      // A precondition can be a function or an object containing a function and
      // name like so:
      // {
      //  fn: isDefined,
      //  name: 'is defined'
      // }
      //
      // Let's add our own here
      // defaults to []
      preconditions: [{
        fn: isDefined,
        name: 'is defined'
      }, {
        fn: function (value) {
          return ['development', 'production', 'test'].indexOf(value) !== -1;
        },
        name: 'is an enviroment'
      }]
    }
  }
});

var defaults = {
  // call resolved.environment function
  environment: resolved.environment()
};

console.log(JSON.stringify(defaults, null, 4));

$ unset NODE_ENV
$ node examples/elaborate --NODE_ENV asdasd
Resolving: environment
    ✖ (1 of 3) - argv.NODE_ENV: asdasd
      ✔ (1 of 2) is defined
      ✖ (2 of 2) is an enviroment
    ✖ (2 of 3) - env.NODE_ENV: undefined
      ✖ (1 of 2) is defined
      ✖ (2 of 2) is an enviroment
  ✊✔ (3 of 3) - default value: development
      ✔ (1 of 2) is defined
      ✔ (2 of 2) is an enviroment

Resolved environment from default value to development

{
    "environment": "development"
}

Running tests

$ gulp test

Test reports are written to ./reports.

Contributing

  • Do pull requests.
  • Make sure there's tests and meaningful coverage.
  • Respect ./eslintrc.
  • Issues should go in issues.

Current Tags

  • 0.4.10                                ...           latest (5 years ago)

18 Versions

  • 0.4.10                                ...           5 years ago
  • 0.4.8                                ...           5 years ago
  • 0.4.7                                ...           5 years ago
  • 0.4.9                                ...           5 years ago
  • 0.4.6                                ...           5 years ago
  • 0.4.5                                ...           5 years ago
  • 0.4.4                                ...           5 years ago
  • 0.4.3                                ...           5 years ago
  • 0.4.2                                ...           5 years ago
  • 0.3.3                                ...           5 years ago
  • 0.4.0                                ...           5 years ago
  • 0.4.1                                ...           5 years ago
  • 0.3.1                                ...           5 years ago
  • 0.3.0                                ...           5 years ago
  • 0.2.1                                ...           5 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.1                                ...           5 years ago
  • 0.1.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (10)
Dev Dependencies (18)

Copyright 2014 - 2017 © taobao.org |