Coder Social home page Coder Social logo

rc-config-loader's Introduction

rc-config-loader Actions Status: test

Load config from .{product}rc.{json,yml,js} file.

It is a Node.js library for loading .textlintrc, .eslintrc, .stylelintrc etc...

Features

Find and load a configuration object from:

  • a package.json property if it is needed
  • a JSON or YAML, JS "rc file"
    • .<product>rc or .<product>rc.json or .<product>rc.js or.<product>rc.yml, .<product>rc.yaml
  • TypeScript support
    • Includes .d.ts

Difference

  • Safe API
    • rc contains shabang in .js file
  • Enhance Error message

If you want to async support and customize loader, recommended to use cosmiconfig.

Install

Install with npm:

npm install rc-config-loader

Usage

API

export interface rcConfigLoaderOption {
    // does look for `package.json`
    packageJSON?:
        | boolean
        | {
              fieldName: string;
          };
    // if config file name is not same with packageName, set the name
    configFileName?: string;
    // treat default(no ext file) as some extension
    defaultExtension?: string | string[];
    // where start to load
    cwd?: string;
}
/**
 * Find and load rcfile, return { config, filePath }
 * If not found any rcfile, throw an Error.
 * @param {string} pkgName
 * @param {rcConfigLoaderOption} [opts]
 * @returns {{ config: Object, filePath:string } | undefined}
 */
export declare function rcFile<R extends {}>(pkgName: string, opts?: rcConfigLoaderOption): {
    config: R;
    filePath: string;
} | undefined;

rcFile return { config, filePath } object.

  • config: it is config object
  • filePath: absolute path to config file

Note:

  • rcFile function return undefined if the config file is not found
  • rcFile throw an Error if the config file content is malformed (causing a parsing error)

Recommenced usage:

import { rcFile } from "rc-config-loader"

function loadRcFile(rcFileName){
    try {
        const results = rcFile(rcFileName);
        // Not Found
        if (!results) {
            return {};
        }
        return results.config;
    } catch (error) {
        // Found it, but it is parsing error
        return {} ; // default value
    }
}
// load config
const config = loadRcFile("your-application");
console.log(config); // => rcfile content

It will check these files and return config file if found it.

  • .your-applicationrc.json
  • .your-applicationrc.yml
  • .your-applicationrc.yaml
  • .your-applicationrc.js
  • [optional] package.json
    • if packageJSON option is enabled

Example

import { rcFile } from "rc-config-loader"
// load .eslintrc from current dir
console.log(rcFile("eslint"));

// load .eslintrc from specific path
console.log(rcFile("eslint", {
    configFileName: `${__dirname}/test/fixtures/.eslintrc`
}));
/*
config: { extends: 'standard',
  rules:
   { 'comma-dangle': [ 2, 'always-multiline' ],
     'arrow-parens': [ 2, 'as-needed' ] } }
filePath: ${__dirname}/test/fixtures/.eslintrc
 */

// load property from package.json
console.log(rcFile("rc-config-loader", {
    packageJSON: {
        fieldName: "directories"
    }
}));
/*
config: { test: 'test' }
filePath: /path/to/package.json
 */

// load .eslintrc from specific dir
console.log(rcFile("eslint", {
    cwd: `${__dirname}/test/fixtures`
}));

// load specific filename from current dir
console.log(rcFile("travis", {configFileName: ".travis"}));
/*
config: { sudo: false, language: 'node_js', node_js: 'stable' }
filePath: /path/to/.travis
 */

// try to load as .json, .yml, js
console.log(rcFile("bar", {
    configFileName: `${__dirname}/test/fixtures/.barrc`,
    defaultExtension: [".json", ".yml", ".js"]
}));

// try to load as foobar, but .foobarrc is not found
console.log(rcFile("foorbar")); // => undefined

// try to load as .json, but it is not json
// throw SyntaxError
try {
    rcFile("unknown", {
        // This is not json
        configFileName: `${__dirname}/test/fixtures/.unknownrc`,
        defaultExtension: ".json"
    })
} catch (error) {
    console.log(error);
    /*
    SyntaxError: Cannot read config file: /test/fixtures/.unknownrc
    */
}

Users

Changelog

See Releases page.

Running tests

Install devDependencies and Run npm test:

npm i -d && npm test

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

License

MIT © azu

Acknowledgement

Difference

  • support multiple defaultExtension

rc-config-loader's People

Contributors

azu avatar dependabot[bot] avatar feego avatar iljapostnovs avatar munierujp avatar raineorshine avatar sebring avatar shadymind avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rc-config-loader's Issues

rc-config-loader doesn't view config file in current path

Accorind sources, if I haven't options rc-config-loader use default path for search - current directory

const configFileName = opts.configFileName || `.${pkgName}rc`;

So I use loader without options and get undefined. Code available here

  1. git clone https://github.com/ravecat/mendeleev.git --single-branch --branch rc-config-loader-issue
  2. yarn

aTry to use in src/common/Media/responsive/index.js

/* eslint-disable */
import { css } from "styled-components";

const rcfile = require("rc-config-loader");

const responsive = (property, resolution) => {
  console.log(rcfile('sct'))

  return css`
    @media (max-width: ${resolution}px) {
      ${property};
    }
  `;
}

export default responsive;

Probably it's webpack settings relative with resolve options, but I use relative path, not absolute

Module not found: Can't resolve 'module'

Try to use cosmiconfig without any use

const rcfile = require("rc-config-loader");

and get error

./node_modules/rc-config-loader/node_modules/require-from-string/index.js
Module not found: Can't resolve 'module' in '/home/max/project/mendeleev/node_modules/rc-config-loader/node_modules/require-from-string'

"rc-config-loader": "^2.0.2",
node: 8.12.0

support for ts files

i understand that the file would require something like ts-node or else, but even with deno the loading of ts files would be native so, do you think it should be supported in that lib?

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.