UNPKG

3.53 kBMarkdownView Raw
1
2# joycon
3
4[![NPM version](https://img.shields.io/npm/v/joycon.svg?style=flat)](https://npmjs.com/package/joycon) [![NPM downloads](https://img.shields.io/npm/dm/joycon.svg?style=flat)](https://npmjs.com/package/joycon) [![install size](https://packagephobia.now.sh/badge?p=joycon@2.0.0)](https://packagephobia.now.sh/result?p=joycon@2.0.0) [![CircleCI](https://circleci.com/gh/egoist/joycon/tree/master.svg?style=shield)](https://circleci.com/gh/egoist/joycon/tree/master) [![donate](https://img.shields.io/badge/$-donate-ff69b4.svg?maxAge=2592000&style=flat)](https://github.com/egoist/donate) [![chat](https://img.shields.io/badge/chat-on%20discord-7289DA.svg?style=flat)](https://chat.egoist.moe)
5
6## Differences with [cosmiconfig](https://github.com/davidtheclark/cosmiconfig)?
7
8JoyCon is zero-dependency but feature-complete.
9
10## Install
11
12```bash
13yarn add joycon
14```
15
16## Usage
17
18```js
19const JoyCon = require('joycon')
20
21const joycon = new JoyCon()
22
23joycon.load(['package-lock.json', 'yarn.lock'])
24.then(result => {
25 // result is {} when files do not exist
26 // otherwise { path, data }
27})
28```
29
30By default only `.js` and `.json` file are parsed, otherwise raw data will be returned, so you can add custom loader to parse them:
31
32```js
33const joycon = new JoyCon()
34
35joycon.addLoader({
36 test: /\.toml$/,
37 load(filepath) {
38 return require('toml').parse(filepath)
39 }
40})
41
42joycon.load(['cargo.toml'])
43```
44
45## API
46
47### constructor([options])
48
49#### options
50
51##### files
52
53- Type: `string[]`
54
55The files to search.
56
57##### cwd
58
59The directory to search files.
60
61##### stopDir
62
63The directory to stop searching.
64
65##### packageKey
66
67You can load config from certain property in a `package.json` file. For example, when you set `packageKey: 'babel'`, it will load the `babel` property in `package.json` instead of the entire data.
68
69##### parseJSON
70
71- Type: `(str: string) => any`
72- Default: `JSON.parse`
73
74The function used to parse JSON string.
75
76### resolve([files], [cwd], [stopDir])
77### resolve([options])
78
79`files` defaults to `options.files`.
80
81`cwd` defaults to `options.cwd`.
82
83`stopDir` defaults to `options.stopDir` then `path.parse(cwd).root`.
84
85If using a single object `options`, it will be the same as constructor options.
86
87Search files and resolve the path of the file we found.
88
89There's also `.resolveSync` method.
90
91### load(...args)
92
93The signature is the same as [resolve](#resolvefiles-cwd-stopdir).
94
95Search files and resolve `{ path, data }` of the file we found.
96
97There's also `.loadSync` method.
98
99### addLoader(Loader)
100
101```typescript
102interface Loader {
103 name?: string
104 test: RegExp
105 load(filepath: string)?: Promise<any>
106 loadSync(filepath: string)?: any
107}
108```
109
110At least one of `load` and `loadSync` is required, depending on whether you're calling the synchonous methods or not.
111
112### removeLoader(name)
113
114Remove loaders by loader name.
115
116### clearCache()
117
118Each JoyCon instance uses its own cache.
119
120## Contributing
121
1221. Fork it!
1232. Create your feature branch: `git checkout -b my-new-feature`
1243. Commit your changes: `git commit -am 'Add some feature'`
1254. Push to the branch: `git push origin my-new-feature`
1265. Submit a pull request :D
127
128## Author
129
130**joycon** © [egoist](https://github.com/egoist), Released under the [MIT](./LICENSE) License.<br>
131Authored and maintained by egoist with help from contributors ([list](https://github.com/egoist/joycon/contributors)).
132
133> [github.com/egoist](https://github.com/egoist) · GitHub [@egoist](https://github.com/egoist) · Twitter [@_egoistlily](https://twitter.com/_egoistlily)