Git Product home page Git Product logo

manis's Introduction

Manis

For build system plugins that need to fetch relative config files (like .fecsrc).

Build Status NPM version Coverage Status DevDependencies

Install

npm install manis

Usage

Using strip-json-comments

var Manis = require('manis');
var stripJSONComments = require('strip-json-comments');

var loader = function (text) {
    return JSON.parse(stripJSONComments(text));
};

var manis = new Manis({
    files: [
        {
            // just for example, it should be loaded as yaml in fact.
            name: '.eslintrc',
            get: function (json) {
                return {eslint: json};
            }
        },
        '.fecsrc',
        {
            name: 'package.json',
            get: function (json) {
                return json.fecs || {};
            }
        }
    ],
    loader: loader
});

var options = manis.from('path/to/file.js');

// do something cool with options

NOTICE: the default loader strip comment after [email protected].

Loading .yml with js-yaml

var yaml = require('js-yaml');

var Manis = require('manis');

var loader = function (text) {
    return yaml.load(text);
};

var manis = new Manis('.travis.yml', {loader: loader});

var options = manis.from('path/to/file.js');

// do something cool with options

NOTICE: the default loader used js-yaml to load YAML and JSON content after [email protected].

With defaults

var Manis = require('manis');

var manis = new Manis({
    files: [
        '.fecsrc',
        {
            name: 'package.json',
            get: 'fecs'
        }
    ]
});

manis.setDefault('default/path/to/config/fecs.json');

var options = manis.from('path/to/file.js');

// do something cool with options

User config

var Manis = require('manis');

var manis = new Manis({
    files: [
        '.fecsrc',
        {
            name: 'package.json',
            get: 'fecs'
        }
    ]
});

manis.setDefault('default/path/to/config/fecs.json');

// will find `~/.fecsrc`
manis.setUserConfig();

var options = manis.from('path/to/file.js');

// do something cool with options

Within a gulp plugin

var Manis = require('manis');
var map = require('map-stream');

module.exports = function MyGulpPlugin(options) {
    var manis = new Manis('.fecsrc', options);

    return map(function (file, cb) {

        // get the options for this file specifically
        var options = manis.from(file.path);

        // do something cool

        // send the file along
        cb(null, file);

    });
};

API

new Manis(string fileName[, Object options]);

new Manis(string[] fileNames[, Object options]);

new Manis(Object[] finderOptioins[, Object options]);

new Manis(Object options);

void Manis#setDefault(Object defaultValue);

void Manis#setDefault(string filePath[, Object finderOptions]);

void Manis#setUserConfig();

void Manis#setUserConfig(Object userConfig);

void Manis#setUserConfig(string userConfigPathOrName[, Object finderOptions]);

Object Manis#from(string path);

Manis.yaml;

Alias for js-yaml module.

Object Manis.loader;

The default loader, parse JSON or YAML content with js-yaml.

Object Manis#from(string path);

options

  • files, Array or string, items could be string or Object.

  • loader, Function,parser for config content.

  • lookup, Boolean, Find all up-level config files. default is true.

  • merge, Boolean, Merge all config objects. default is true.

  • cache, Boolean, Cache config files. default is true.

  • rootName, String, The name of flag when enableRoot set to true. default is 'root'.

  • enableRoot, Boolean, Enable the root flag to stop lookup in up-level directory. default is false.

  • stopper, Function, the predicate for stopping search. default is null.

finderOptions

  • name, string, the file name to be searched.

  • loader, Funtion, the same as options.loader above;

  • stopper, Function, the predicate for stopping search.

  • get, string or Function, the field name to retrieve from config object.

  • cache, Boolean, Cache config files. default is true.

manis's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

manis's Issues

内置 YAML 配置文件的支持

理由如下:

  • YAML 格式更易书写更易阅读,支持的格式更多
  • YAML 是 JSON 的超集,js-yaml 的实现兼容 JSON

考虑支持不合并配置信息的策略

如题,例如文件/a/b/c,配置文件名为.fecsrc,考虑是否支持不合并如下配置信息的策略:

  1. /a/b/.fecsrc
  2. /a/.fecsrc
  3. ~/.fecsrc(用户配置信息,通过 setUserConfig 开启)
  4. 默认配置,通过 setDefault 指定

这种策略下,一个文件唯一确定配置信息,便于开发者确定特定文件/目录的配置情况,排查配置相关的问题会更容易。

增加 root 标识

参照社区约定,配置文件中包含 root 为 true 的标识时,作为唯一配置,不再查找其它文件。

获取`~/`下的配置文件

希望能提供获取HOME路径下的配置文件的能力,它与现有的配置信息优先级关系为:

指定文件路径及其祖先路径下的配置 > HOME路径下的配置 > 默认配置

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.