$ npm install rc-config-loader
Load config from .{product}rc.{json,yml,js}
file.
It is a Node.js library for loading .textlintrc
, .eslintrc
, .stylelintrc
etc...
Find and load a configuration object from:
package.json
property if it is needed.<product>rc
or .<product>rc.json
or .<product>rc.js
or.<product>rc.yml
, .<product>rc.yaml
.d.ts
rc
contains shabang in .js
filesync
optionIf you want to async support and customize loader, recommended to use cosmiconfig.
Install with npm:
npm install rc-config-loader
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 objectfilePath
: absolute path to config fileNote:
rcFile
function return undefined
if the config file is not foundrcFile
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
package.json
packageJSON
option is enabledimport { 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
*/
}
See Releases page.
Install devDependencies and Run npm test
:
npm i -d && npm test
Pull requests and stars are always welcome.
For bugs and feature requests, please create an issue.
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
MIT © azu
Difference
defaultExtension
© 2010 - cnpmjs.org x YWFE | Home | YWFE