UNPKG

9.28 kBMarkdownView Raw
1# cosmiconfig
2
3[![Build Status](https://img.shields.io/travis/davidtheclark/cosmiconfig/master.svg?label=unix%20build)](https://travis-ci.org/davidtheclark/cosmiconfig) [![Build status](https://img.shields.io/appveyor/ci/davidtheclark/cosmiconfig/master.svg?label=windows%20build)](https://ci.appveyor.com/project/davidtheclark/cosmiconfig/branch/master)
4
5Find and load a configuration object from
6- a `package.json` property (anywhere down the file tree)
7- a JSON or YAML "rc file" (anywhere down the file tree)
8- a `.config.js` CommonJS module (anywhere down the file tree)
9- a CLI `--config` argument
10
11For example, if your module's name is "soursocks," cosmiconfig will search out configuration in the following places:
12- a `soursocks` property in `package.json` (anywhere down the file tree)
13- a `.soursocksrc` file in JSON or YAML format (anywhere down the file tree)
14- a `soursocks.config.js` file exporting a JS object (anywhere down the file tree)
15- a CLI `--config` argument
16
17cosmiconfig continues to search in these places all the way down the file tree until it finds acceptable configuration (or hits the home directory).
18
19Additionally, all of these search locations are configurable: you can customize filenames or turn off any location.
20
21You can also look for rc files with extensions, e.g. `.soursocksrc.json` or `.soursocksrc.yaml`.
22You may like extensions on your rc files because you'll get syntax highlighting and linting in text editors.
23
24## Installation
25
26```
27npm install cosmiconfig
28```
29
30Tested in Node 4+.
31
32## Usage
33
34```js
35var cosmiconfig = require('cosmiconfig');
36
37var explorer = cosmiconfig(yourModuleName[, options]);
38
39explorer.load(yourSearchPath)
40 .then((result) => {
41 // result.config is the parsed configuration object
42 // result.filepath is the path to the config file that was found
43 })
44 .catch((parsingError) => {
45 // do something constructive
46 });
47```
48
49The function `cosmiconfig()` searches for a configuration object and returns a Promise,
50which resolves with an object containing the information you're looking for.
51
52You can also pass option `sync: true` to load the config synchronously, returning the config itself.
53
54So let's say `var yourModuleName = 'goldengrahams'` — here's how cosmiconfig will work:
55
56- Starting from `process.cwd()` (or some other directory defined by the `searchPath` argument to `load()`), it looks for configuration objects in three places, in this order:
57 1. A `goldengrahams` property in a `package.json` file (or some other property defined by `options.packageProp`);
58 2. A `.goldengrahamsrc` file with JSON or YAML syntax (or some other filename defined by `options.rc`);
59 3. A `goldengrahams.config.js` JS file exporting the object (or some other filename defined by `options.js`).
60- If none of those searches reveal a configuration object, it moves down one directory and tries again. So the search continues in `./`, `../`, `../../`, `../../../`, etc., checking those three locations in each directory.
61- It continues searching until it arrives at your home directory (or some other directory defined by `options.stopDir`).
62- If at any point a parseable configuration is found, the `cosmiconfig()` Promise resolves with its result object.
63- If no configuration object is found, the `cosmiconfig()` Promise resolves with `null`.
64- If a configuration object is found *but is malformed* (causing a parsing error), the `cosmiconfig()` Promise rejects and shares that error (so you should `.catch()` it).
65
66All this searching can be short-circuited by passing `options.configPath` or a `--config` CLI argument to specify a file.
67cosmiconfig will read that file and try parsing it as JSON, YAML, or JS.
68
69## Caching
70
71As of v2, cosmiconfig uses a few caches to reduce the need for repetitious reading of the filesystem. Every new cosmiconfig instance (created with `cosmiconfig()`) has its own caches.
72
73To avoid or work around caching, you can
74- create separate instances of cosmiconfig, or
75- set `cache: false` in your options.
76- use the cache clearing methods documented below.
77
78## API
79
80### `var explorer = cosmiconfig(moduleName[, options])`
81
82Creates a cosmiconfig instance (i.e. explorer) configured according to the arguments, and initializes its caches.
83
84#### moduleName
85
86Type: `string`
87
88You module name. This is used to create the default filenames that cosmiconfig will look for.
89
90#### Options
91
92##### packageProp
93
94Type: `string` or `false`
95Default: `'[moduleName]'`
96
97Name of the property in `package.json` to look for.
98
99If `false`, cosmiconfig will not look in `package.json` files.
100
101##### rc
102
103Type: `string` or `false`
104Default: `'.[moduleName]rc'`
105
106Name of the "rc file" to look for, which can be formatted as JSON or YAML.
107
108If `false`, cosmiconfig will not look for an rc file.
109
110If `rcExtensions: true`, the rc file can also have extensions that specify the syntax, e.g. `.[moduleName]rc.json`.
111You may like extensions on your rc files because you'll get syntax highlighting and linting in text editors.
112Also, with `rcExtensions: true`, you can use JS modules as rc files, e.g. `.[moduleName]rc.js`.
113
114##### js
115
116Type: `string` or `false`
117Default: `'[moduleName].config.js'`
118
119Name of a JS file to look for, which must export the configuration object.
120
121If `false`, cosmiconfig will not look for a JS file.
122
123##### rcStrictJson
124
125Type: `boolean`
126Default: `false`
127
128If `true`, cosmiconfig will expect rc files to be strict JSON. No YAML permitted, and no sloppy JSON.
129
130By default, rc files are parsed with [js-yaml](https://github.com/nodeca/js-yaml), which is
131more permissive with punctuation than standard strict JSON.
132
133##### rcExtensions
134
135Type: `boolean`
136Default: `false`
137
138If `true`, cosmiconfig will look for rc files with extensions, in addition to rc files without.
139
140This adds a few steps to the search process.
141Instead of *just* looking for `.goldengrahamsrc` (no extension), it will also look for the following, in this order:
142
143- `.goldengrahamsrc.json`
144- `.goldengrahamsrc.yaml`
145- `.goldengrahamsrc.yml`
146- `.goldengrahamsrc.js`
147
148##### stopDir
149
150Type: `string`
151Default: Absolute path to your home directory
152
153Directory where the search will stop.
154
155##### cache
156
157Type: `boolean`
158Default: `true`
159
160If `false`, no caches will be used.
161
162##### sync
163
164Type: `boolean`
165Default: `false`
166
167If `true`, config will be loaded synchronously.
168
169##### transform
170
171Type: `Function`
172
173A function that transforms the parsed configuration. Receives the result object with `config` and `filepath` properties.
174
175If the option `sync` is `false` (default), the function must return a Promise that resolves with the transformed result.
176If the option `sync` is `true`, though, `transform` should be a synchronous function which returns the transformed result.
177
178The reason you might use this option instead of simply applying your transform function some other way is that *the transformed result will be cached*. If your transformation involves additional filesystem I/O or other potentially slow processing, you can use this option to avoid repeating those steps every time a given configuration is loaded.
179
180##### configPath
181
182Type: `string`
183
184If provided, cosmiconfig will load and parse a config from this path, and will not perform its usual search.
185
186### Instance methods (on `explorer`)
187
188#### `load([searchPath, configPath])`
189
190Find and load a configuration file. Returns a Promise that resolves with `null`, if nothing is found, or an object with two properties:
191- `config`: The loaded and parsed configuration.
192- `filepath`: The filepath where this configuration was found.
193
194You should provide *either* `searchPath` *or* `configPath`. Use `configPath` if you know the path of the configuration file you want to load. Otherwise, use `searchPath`.
195
196```js
197explorer.load('start/search/here');
198explorer.load('start/search/at/this/file.css');
199
200explorer.load(null, 'load/this/file.json');
201```
202
203If you provide `searchPath`, cosmiconfig will start its search at `searchPath` and continue to search up the file tree, as documented above.
204
205If you provide `configPath` (i.e. you already know where the configuration is that you want to load), cosmiconfig will try to read and parse that file.
206
207#### `clearFileCache()`
208
209Clears the cache used when you provide a `configPath` argument to `load`.
210
211#### `clearDirectoryCache()`
212
213Clears the cache used when you provide a `searchPath` argument to `load`.
214
215#### `clearCaches()`
216
217Performs both `clearFileCache()` and `clearDirectoryCache()`.
218
219## Differences from [rc](https://github.com/dominictarr/rc)
220
221[rc](https://github.com/dominictarr/rc) serves its focused purpose well. cosmiconfig differs in a few key ways — making it more useful for some projects, less useful for others:
222
223- Looks for configuration in some different places: in a `package.json` property, an rc file, a `.config.js` file, and rc files with extensions.
224- Built-in support for JSON, YAML, and CommonJS formats.
225- Stops at the first configuration found, instead of finding all that can be found down the filetree and merging them automatically.
226- Options.
227- Asynchronous by default (though can be run synchronously).
228
229## Contributing & Development
230
231Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
232
233And please do participate!