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 |
|
8 | JoyCon is zero-dependency but feature-complete.
|
9 |
|
10 | ## Install
|
11 |
|
12 | ```bash
|
13 | yarn add joycon
|
14 | ```
|
15 |
|
16 | ## Usage
|
17 |
|
18 | ```js
|
19 | const JoyCon = require('joycon')
|
20 |
|
21 | const joycon = new JoyCon()
|
22 |
|
23 | joycon.load(['package-lock.json', 'yarn.lock'])
|
24 | .then(result => {
|
25 | // result is {} when files do not exist
|
26 | // otherwise { path, data }
|
27 | })
|
28 | ```
|
29 |
|
30 | By 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
|
33 | const joycon = new JoyCon()
|
34 |
|
35 | joycon.addLoader({
|
36 | test: /\.toml$/,
|
37 | load(filepath) {
|
38 | return require('toml').parse(filepath)
|
39 | }
|
40 | })
|
41 |
|
42 | joycon.load(['cargo.toml'])
|
43 | ```
|
44 |
|
45 | ## API
|
46 |
|
47 | ### constructor([options])
|
48 |
|
49 | #### options
|
50 |
|
51 | ##### files
|
52 |
|
53 | - Type: `string[]`
|
54 |
|
55 | The files to search.
|
56 |
|
57 | ##### cwd
|
58 |
|
59 | The directory to search files.
|
60 |
|
61 | ##### stopDir
|
62 |
|
63 | The directory to stop searching.
|
64 |
|
65 | ##### packageKey
|
66 |
|
67 | You 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 |
|
74 | The 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 |
|
85 | If using a single object `options`, it will be the same as constructor options.
|
86 |
|
87 | Search files and resolve the path of the file we found.
|
88 |
|
89 | There's also `.resolveSync` method.
|
90 |
|
91 | ### load(...args)
|
92 |
|
93 | The signature is the same as [resolve](#resolvefiles-cwd-stopdir).
|
94 |
|
95 | Search files and resolve `{ path, data }` of the file we found.
|
96 |
|
97 | There's also `.loadSync` method.
|
98 |
|
99 | ### addLoader(Loader)
|
100 |
|
101 | ```typescript
|
102 | interface Loader {
|
103 | name?: string
|
104 | test: RegExp
|
105 | load(filepath: string)?: Promise<any>
|
106 | loadSync(filepath: string)?: any
|
107 | }
|
108 | ```
|
109 |
|
110 | At least one of `load` and `loadSync` is required, depending on whether you're calling the synchonous methods or not.
|
111 |
|
112 | ### removeLoader(name)
|
113 |
|
114 | Remove loaders by loader name.
|
115 |
|
116 | ### clearCache()
|
117 |
|
118 | Each JoyCon instance uses its own cache.
|
119 |
|
120 | ## Contributing
|
121 |
|
122 | 1. Fork it!
|
123 | 2. Create your feature branch: `git checkout -b my-new-feature`
|
124 | 3. Commit your changes: `git commit -am 'Add some feature'`
|
125 | 4. Push to the branch: `git push origin my-new-feature`
|
126 | 5. Submit a pull request :D
|
127 |
|
128 | ## Author
|
129 |
|
130 | **joycon** © [egoist](https://github.com/egoist), Released under the [MIT](./LICENSE) License.<br>
|
131 | Authored 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)
|