rc-parser
Find and parse rc, rc.js, rc.yaml or etc if any one of them exists. rc-parser
searches the giving path(s), and parses out that if the runtime configuration file with the certain extension listed in options.extensions
exists. If found, it parses and returns the config object.
rc-parser
featured in:
- Supports to define custom parser for a certain file type.
- Better error messages and syntax hint.
- Fully customizable.
Install
$ npm i rc-parser
Usage
const parse = const sync = // The synchronous version const options = path: __dirname // current directory name: '.travis' const rc = await console // 'yml'console // 'node_js' console // the same as rc
Define your own parsers to support more file types
const ini =
APIs
parse(options): Promise<RCResult>
- options
- path
string | Array<string>
the search path(s) for the rc file. - name
string
the prefix name of the rc file to search. - extensions
Extensions | undefined
- parsers
Object{[Extension]: ParserFunction}
- not_found_error
NotFoundErrorFunction
will be executed if no rc files are found. - code_frame
CodeFrameFunction | false
- path
Returns Promise<RCResult>
options.extensions
specifies the extension priority for searching rc files. Defaults to ['yaml', 'yml', 'js', parse.NO_EXT]
parse.NO_EXT
is a special extension which indicates there is no extension after name
# Suppose: options.name === '.eslintrc' # filepath | extension # ------------------------- | ----------------- /path/to/project |-- .eslintrc # rc-parser.NO_EXT |-- .eslintrc.js # 'js' |-- .eslintrc.yaml # 'yaml'
If options.extensions
as ['yaml', 'js', NO_EXT]
, then we will get .eslintrc.yaml
.
Similarly, ['js', 'yaml', NO_EXT]
=> .eslintrc.js
- extension the extension string of the found rc file, excluding
.
- value the parsed value
- abspath the absolute path of the rc file.
- content the content of the rc file
- filepath the filepath relative to the current search path
Parses the content of rc files, returns the parsed object, or throws error if there is a parse error.
If the error (ParserError
) thrown by ParserFunction
contains both the line
property and the column
property, the error.message
will be argumented by CodeFrameFunction
sync(options): RCResult
- options the same as
options
ofparse(options)
Built-in parsers
parsePARSERS<type>
yaml, yml
Based on js-yaml
json
Based on json5
License
MIT