All Projects → azu → rc-config-loader

azu / rc-config-loader

Licence: MIT license
Load config from .{product}rc.{json,yml,js} file

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to rc-config-loader

ssr-decode
一个解析Shadowsocks/ShadowsocksR/V2Ray订阅链接(http/ss/ssr/vmess)并生成json配置文件的百行shell小脚本
Stars: ✭ 53 (+15.22%)
Mutual labels:  config
dotfiles
🔧 My dotfiles on  macOS for Neovim, Zsh, kitty, lf, etc
Stars: ✭ 90 (+95.65%)
Mutual labels:  config
SwiftLoadHook
Use a hack way to achieve similar functions as Load() or initialize() in OC
Stars: ✭ 21 (-54.35%)
Mutual labels:  load
Recon
HA LDAP based key/value solution for projects configuration storing with multi master replication support
Stars: ✭ 12 (-73.91%)
Mutual labels:  config
csgo-server-configs
CS:GO Server Configs for Competitive 5v5, Knife Round, Aim Map and FFA Deathmatch
Stars: ✭ 24 (-47.83%)
Mutual labels:  config
config
holy cow, wholly config! Vim, Zshell, Ack, & the rest of my dot-files.
Stars: ✭ 21 (-54.35%)
Mutual labels:  config
archery
Abstract over the atomicity of reference-counting pointers in rust
Stars: ✭ 107 (+132.61%)
Mutual labels:  rc
intuit-spring-cloud-config-inspector
Inspection of Spring Cloud Config properties made easy using React
Stars: ✭ 18 (-60.87%)
Mutual labels:  config
tomli
A lil' TOML parser
Stars: ✭ 313 (+580.43%)
Mutual labels:  config
laravel-micro.js
A Laravel inspired front-end framework for JavaScript artisans.
Stars: ✭ 107 (+132.61%)
Mutual labels:  config
configster
Rust library for parsing configuration files
Stars: ✭ 19 (-58.7%)
Mutual labels:  config
View-Load-ReTry
这个加载框架有点不一样,针对View进行加载,加载页面还保持了原View的属性,侧重点在灵活,哪里需要加载哪里,加载状态页面完全自定义,无任何限制,针对加载结果可以按需配置对应页面,LeakCanary检测无内存泄漏
Stars: ✭ 116 (+152.17%)
Mutual labels:  load
libconfini
Yet another INI parser
Stars: ✭ 106 (+130.43%)
Mutual labels:  config
bspwm rice
bspwm confs and rice
Stars: ✭ 31 (-32.61%)
Mutual labels:  config
example-app
Example app showcasing fulls1z3's Angular libraries
Stars: ✭ 27 (-41.3%)
Mutual labels:  config
beboptwo4g
4G/LTE softmod for the Parrot Bebop 2
Stars: ✭ 50 (+8.7%)
Mutual labels:  rc
javaproperties
Python library for reading & writing Java .properties files
Stars: ✭ 20 (-56.52%)
Mutual labels:  config
read-env
🔧 Transform environment variables into JSON object with sanitized values.
Stars: ✭ 60 (+30.43%)
Mutual labels:  config
awesome-config
My awesome wm configuration.
Stars: ✭ 27 (-41.3%)
Mutual labels:  config
betterconf
Minimalistic Python library for your configs.
Stars: ✭ 35 (-23.91%)
Mutual labels:  config

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

with MoOx/rc-loader

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

with cosmiconfig

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].