UNPKG

5.86 kBMarkdownView Raw
1# rc-config-loader [![Actions Status: test](https://github.com/azu/rc-config-loader/workflows/test/badge.svg)](https://github.com/azu/rc-config-loader/actions?query=workflow%3A"test")
2
3Load config from `.{product}rc.{json,yml,js}` file.
4
5It is a Node.js library for loading `.textlintrc`, `.eslintrc`, `.stylelintrc` etc...
6
7## Features
8
9Find and load a configuration object from:
10
11- a `package.json` property if it is needed
12- a JSON or YAML, JS "rc file"
13 - `.<product>rc` or `.<product>rc.json` or `.<product>rc.js` or`.<product>rc.yml`, `.<product>rc.yaml`
14- TypeScript support
15 - Includes `.d.ts`
16
17## Difference
18
19### with [MoOx/rc-loader](https://github.com/MoOx/rc-loader "MoOx/rc-loader")
20
21- Safe API
22 - `rc` contains shabang in `.js` file
23- Enhance Error message
24
25### with [cosmiconfig](https://github.com/davidtheclark/cosmiconfig "cosmiconfig")
26
27- <del>Sync loading</del>
28 - [cosmiconfig@3+](https://github.com/davidtheclark/cosmiconfig/blob/master/CHANGELOG.md#300) support `sync` option
29- <del>Built-in TypeScript support</del>
30 - [comisconfig@6*](https://github.com/davidtheclark/cosmiconfig/blob/master/CHANGELOG.md#600) is written by TypeScript
31
32If you want to async support and customize loader, recommended to use [cosmiconfig](https://github.com/davidtheclark/cosmiconfig).
33
34## Install
35
36Install with [npm](https://www.npmjs.com/):
37
38 npm install rc-config-loader
39
40## Usage
41
42### API
43
44```ts
45export interface rcConfigLoaderOption {
46 // does look for `package.json`
47 packageJSON?:
48 | boolean
49 | {
50 fieldName: string;
51 };
52 // if config file name is not same with packageName, set the name
53 configFileName?: string;
54 // treat default(no ext file) as some extension
55 defaultExtension?: string | string[];
56 // where start to load
57 cwd?: string;
58}
59/**
60 * Find and load rcfile, return { config, filePath }
61 * If not found any rcfile, throw an Error.
62 * @param {string} pkgName
63 * @param {rcConfigLoaderOption} [opts]
64 * @returns {{ config: Object, filePath:string } | undefined}
65 */
66export declare function rcFile<R extends {}>(pkgName: string, opts?: rcConfigLoaderOption): {
67 config: R;
68 filePath: string;
69} | undefined;
70
71```
72
73`rcFile` return `{ config, filePath }` object.
74
75- `config`: it is config object
76- `filePath`: absolute path to config file
77
78**Note:**
79
80- `rcFile` function return `undefined` if the config file is not found
81- `rcFile` throw an Error if the config file content is malformed (causing a parsing error)
82
83Recommenced usage:
84
85```js
86import { rcFile } from "rc-config-loader"
87
88function loadRcFile(rcFileName){
89 try {
90 const results = rcFile(rcFileName);
91 // Not Found
92 if (!results) {
93 return {};
94 }
95 return results.config;
96 } catch (error) {
97 // Found it, but it is parsing error
98 return {} ; // default value
99 }
100}
101// load config
102const config = loadRcFile("your-application");
103console.log(config); // => rcfile content
104```
105
106It will check these files and return config file if found it.
107
108- `.your-applicationrc.json`
109- `.your-applicationrc.yml`
110- `.your-applicationrc.yaml`
111- `.your-applicationrc.js`
112- [optional] `package.json`
113 - if `packageJSON` option is enabled
114
115### Example
116
117```js
118import { rcFile } from "rc-config-loader"
119// load .eslintrc from current dir
120console.log(rcFile("eslint"));
121
122// load .eslintrc from specific path
123console.log(rcFile("eslint", {
124 configFileName: `${__dirname}/test/fixtures/.eslintrc`
125}));
126/*
127config: { extends: 'standard',
128 rules:
129 { 'comma-dangle': [ 2, 'always-multiline' ],
130 'arrow-parens': [ 2, 'as-needed' ] } }
131filePath: ${__dirname}/test/fixtures/.eslintrc
132 */
133
134// load property from package.json
135console.log(rcFile("rc-config-loader", {
136 packageJSON: {
137 fieldName: "directories"
138 }
139}));
140/*
141config: { test: 'test' }
142filePath: /path/to/package.json
143 */
144
145// load .eslintrc from specific dir
146console.log(rcFile("eslint", {
147 cwd: `${__dirname}/test/fixtures`
148}));
149
150// load specific filename from current dir
151console.log(rcFile("travis", {configFileName: ".travis"}));
152/*
153config: { sudo: false, language: 'node_js', node_js: 'stable' }
154filePath: /path/to/.travis
155 */
156
157// try to load as .json, .yml, js
158console.log(rcFile("bar", {
159 configFileName: `${__dirname}/test/fixtures/.barrc`,
160 defaultExtension: [".json", ".yml", ".js"]
161}));
162
163// try to load as foobar, but .foobarrc is not found
164console.log(rcFile("foorbar")); // => undefined
165
166// try to load as .json, but it is not json
167// throw SyntaxError
168try {
169 rcFile("unknown", {
170 // This is not json
171 configFileName: `${__dirname}/test/fixtures/.unknownrc`,
172 defaultExtension: ".json"
173 })
174} catch (error) {
175 console.log(error);
176 /*
177 SyntaxError: Cannot read config file: /test/fixtures/.unknownrc
178 */
179}
180```
181
182## Users
183
184- [textlint](https://github.com/textlint/textlint "textlint")
185
186## Changelog
187
188See [Releases page](https://github.com/azu/rc-config-loader/releases).
189
190## Running tests
191
192Install devDependencies and Run `npm test`:
193
194 npm i -d && npm test
195
196## Contributing
197
198Pull requests and stars are always welcome.
199
200For bugs and feature requests, [please create an issue](https://github.com/azu/rc-config-loader/issues).
201
2021. Fork it!
2032. Create your feature branch: `git checkout -b my-new-feature`
2043. Commit your changes: `git commit -am 'Add some feature'`
2054. Push to the branch: `git push origin my-new-feature`
2065. Submit a pull request :D
207
208## Author
209
210- [github/azu](https://github.com/azu)
211- [twitter/azu_re](https://twitter.com/azu_re)
212
213## License
214
215MIT © azu
216
217## Acknowledgement
218
219- [zkochan/rcfile: Loads library configuration in all possible ways](https://github.com/zkochan/rcfile "zkochan/rcfile: Loads library configuration in all possible ways")
220
221**Difference**
222
223- support multiple `defaultExtension`