Giter Site home page Giter Site logo

snowyu / load-config-file.js Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 146 KB

Load the config file as a plain object. The config file format can be registered. The registered file format order is the search order.

License: MIT License

CoffeeScript 98.38% JavaScript 1.62%

load-config-file.js's Introduction

load-config-file npm

Build Status Code Climate Test Coverage downloads license

Load the config file as a plain object.

  • The config file format can be registered.
  • The registered file format order is the search order.
  • the virtual file system can be specfied: loadConfig.setFileSystem(fs)
    • fs.path = require('path.js/lib/path').path for the virtual POSIX path.
      • you must set the path first before call setFileSystem.

Usage

var loadConfig = require('load-config-file');
var yaml  = require('js-yaml');
var cson  = require('cson');

loadConfig.register(['.yaml', '.yml'], yaml.safeLoad); //first search.
loadConfig.register('.cson', cson.parseCSONString.bind(cson)); //second search
loadConfig.register('.json', JSON.parse); //third search.

//Synchronously load config from file.
//it will search config.yaml, config.yml, config.cson, config.json in the current folder.
//the first exist file will be loaded.
//the default encoding is "utf8" if no encoding.
//loadConfig('config', {encoding: 'ascii'})
//the non-enumerable "$cfgPath" property added.
console.log(loadConfig('config'));

//Asynchronously load config from file
loadConfig('config', function(err, result){
  if (err) {
    console.log('error:', err);
  } else {
    console.log(result);
  }
})

API

var config = require('load-config-file');
  • config.register(extensionNames, parserFunc): register the configuration file format to load. return the configurators if successful.
    • extensionNames (Sting|ArrayOf String): the configuration file extension name(s) with dot.
    • parserFunc Function(context): the configuration context parser function:
      • parse the configuration context and return the plain object.
  • config.setFileSystem(fs): set your favour file system. defaults to 'fs'.
    • the "file system" must implement readFile(path[, options], done) and readFileSync(path[, options])
  • load(path, options, done): Asynchronously load config from file
    • options
      • raiseError (Boolean): raise error if nothing loaded defaults to false.
      • exclude (String|ArrayOf String): excludes some files.
      • encoding (String): the file encoding name. defaults to 'utf8'.
    • return the plain object and the $cfgPath property added if successful.
  • loadSync(path, options): Synchronously load config from file
    • return the plain object and the $cfgPath property added if successful.

Changes

v0.2

  • raiseError option to load function asynchronously.

  • add the $cfgPath(String) non-enumerable property to the result.

  • add object usage supports:

    var cfgObj = new Config(aPath, aOptions) //create a configuration object.
    result = cfgObj.loadSync()

TODO

  • ! how to use the specified promise library via any-promise?
    • currently use the bluebird by default.

License

MIT

load-config-file.js's People

Contributors

snowyu avatar

Watchers

 avatar  avatar

load-config-file.js's Issues

con.jon file in windows

Hi all,

please rename the test\fixture\con.jon file to something different as con.
As con is a reserved keyword in Windows, the file is not removable or editable using normal windows tools.

[Question] How to use together with rc

I love this project; I was thinking about write a tiny module for try to load JSON/CSON/YAML file but not necessary because this project exists! kudos ❤️ .

About this aspect, how I can use it together with rc?

Basically expose an API where you specify the parse for your files. For example, here, I use CSON.parse:

var rc = require('rc')
var conf = rc('myProject', DEFAULT.scaffold(), CSON.parse);

In the CSON.parse I want to use load-config-file. Can you give any guideline about how to use it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.