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) [![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 | ## Install
|
7 |
|
8 | ```bash
|
9 | yarn add joycon
|
10 | ```
|
11 |
|
12 | ## Usage
|
13 |
|
14 | ```js
|
15 | const JoyCon = require('joycon')
|
16 |
|
17 | const joycon = new JoyCon()
|
18 |
|
19 | joycon.load(['package-lock.json', 'yarn.lock'])
|
20 | .then(result => {
|
21 | // result is {} when files do not exist
|
22 | // otherwise { path, data }
|
23 | })
|
24 | ```
|
25 |
|
26 | By default only `.js` and `.json` file are parsed, otherwise raw data will be returned, so you can add custom loader to parse them:
|
27 |
|
28 | ```js
|
29 | const joycon = new JoyCon()
|
30 |
|
31 | joycon.addLoader({
|
32 | test: /\.toml$/,
|
33 | load(filepath) {
|
34 | return require('toml').parse(filepath)
|
35 | }
|
36 | })
|
37 |
|
38 | joycon.load(['cargo.toml'])
|
39 | ```
|
40 |
|
41 | ## API
|
42 |
|
43 | ### constructor([options])
|
44 |
|
45 | #### options
|
46 |
|
47 | ##### files
|
48 |
|
49 | Type: `string[]`
|
50 |
|
51 | The files to search.
|
52 |
|
53 | ##### cwd
|
54 |
|
55 | The directory to search files.
|
56 |
|
57 | ### resolve([files], [cwd], [stopDir])
|
58 |
|
59 | `files` defaults to `options.files`.
|
60 |
|
61 | `cwd` defaults to `options.cwd`.
|
62 |
|
63 | `stopDir` defaults to `path.parse(cwd).root`.
|
64 |
|
65 | Search files and resolve the path of the file we found.
|
66 |
|
67 | ### load(...args)
|
68 |
|
69 | The signature is the same as [resolve](#resolvefiles-cwd-stopdir).
|
70 |
|
71 | Search files and resolve `{ path, data }` of the file we found.
|
72 |
|
73 | ### addLoader(Loader)
|
74 |
|
75 | ```typescript
|
76 | interface Loader {
|
77 | test: RegExp
|
78 | load(filepath: string): any
|
79 | }
|
80 | ```
|
81 |
|
82 | ### clearCache()
|
83 |
|
84 | Each JoyCon instance uses its own cache.
|
85 |
|
86 | ## Contributing
|
87 |
|
88 | 1. Fork it!
|
89 | 2. Create your feature branch: `git checkout -b my-new-feature`
|
90 | 3. Commit your changes: `git commit -am 'Add some feature'`
|
91 | 4. Push to the branch: `git push origin my-new-feature`
|
92 | 5. Submit a pull request :D
|
93 |
|
94 | ## Author
|
95 |
|
96 | **joycon** © [egoist](https://github.com/egoist), Released under the [MIT](./LICENSE) License.<br>
|
97 | Authored and maintained by egoist with help from contributors ([list](https://github.com/egoist/joycon/contributors)).
|
98 |
|
99 | > [github.com/egoist](https://github.com/egoist) · GitHub [@egoist](https://github.com/egoist) · Twitter [@_egoistlily](https://twitter.com/_egoistlily)
|