UNPKG

9.17 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). And it does all this asynchronously, so it shouldn't get in your way.
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 0.12+.
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
52So let's say `var yourModuleName = 'goldengrahams'` — here's how cosmiconfig will work:
53
54- 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:
55 1. A `goldengrahams` property in a `package.json` file (or some other property defined by `options.packageProp`);
56 2. A `.goldengrahamsrc` file with JSON or YAML syntax (or some other filename defined by `options.rc`);
57 3. A `goldengrahams.config.js` JS file exporting the object (or some other filename defined by `options.js`).
58- 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.
59- It continues searching until it arrives at your home directory (or some other directory defined by `options.stopDir`).
60- If at any point a parseable configuration is found, the `cosmiconfig()` Promise resolves with its result object.
61- If no configuration object is found, the `cosmiconfig()` Promise resolves with `null`.
62- 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).
63
64All this searching can be short-circuited by passing `options.configPath` or a `--config` CLI argument to specify a file.
65cosmiconfig will read that file and try parsing it as JSON, YAML, or JS.
66
67## Caching
68
69As 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.
70
71To avoid or work around caching, you can
72- create separate instances of cosmiconfig, or
73- set `cache: false` in your options.
74- use the cache clearing methods documented below.
75
76## API
77
78### `var explorer = cosmiconfig(moduleName[, options])`
79
80Creates a cosmiconfig instance (i.e. explorer) configured according to the arguments, and initializes its caches.
81
82#### moduleName
83
84Type: `string`
85
86You module name. This is used to create the default filenames that cosmiconfig will look for.
87
88#### Options
89
90##### packageProp
91
92Type: `string` or `false`
93Default: `'[moduleName]'`
94
95Name of the property in `package.json` to look for.
96
97If `false`, cosmiconfig will not look in `package.json` files.
98
99##### rc
100
101Type: `string` or `false`
102Default: `'.[moduleName]rc'`
103
104Name of the "rc file" to look for, which can be formatted as JSON or YAML.
105
106If `false`, cosmiconfig will not look for an rc file.
107
108If `rcExtensions: true`, the rc file can also have extensions that specify the syntax, e.g. `.[moduleName]rc.json`.
109You may like extensions on your rc files because you'll get syntax highlighting and linting in text editors.
110Also, with `rcExtensions: true`, you can use JS modules as rc files, e.g. `.[moduleName]rc.js`.
111
112##### js
113
114Type: `string` or `false`
115Default: `'[moduleName].config.js'`
116
117Name of a JS file to look for, which must export the configuration object.
118
119If `false`, cosmiconfig will not look for a JS file.
120
121##### argv
122
123Type: `string` or `false`
124Default: `'config'`
125
126Name of a `process.argv` argument to look for, whose value should be the path to a configuration file.
127cosmiconfig will read the file and try to parse it as JSON, YAML, or JS.
128By default, cosmiconfig looks for `--config`.
129
130If `false`, cosmiconfig will not look for any `process.argv` arguments.
131
132##### rcStrictJson
133
134Type: `boolean`
135Default: `false`
136
137If `true`, cosmiconfig will expect rc files to be strict JSON. No YAML permitted, and no sloppy JSON.
138
139By default, rc files are parsed with [js-yaml](https://github.com/nodeca/js-yaml), which is
140more permissive with punctuation than standard strict JSON.
141
142##### rcExtensions
143
144Type: `boolean`
145Default: `false`
146
147If `true`, cosmiconfig will look for rc files with extensions, in addition to rc files without.
148
149This adds a few steps to the search process.
150Instead of *just* looking for `.goldengrahamsrc` (no extension), it will also look for the following, in this order:
151
152- `.goldengrahamsrc.json`
153- `.goldengrahamsrc.yaml`
154- `.goldengrahamsrc.yml`
155- `.goldengrahamsrc.js`
156
157##### stopDir
158
159Type: `string`
160Default: Absolute path to your home directory
161
162Directory where the search will stop.
163
164##### cache
165
166Type: `boolean`
167Default: `true`
168
169If `false`, no caches will be used.
170
171##### transform
172
173Type: `Function` returning a Promise
174
175A function that transforms the parsed configuration. Receives the result object with `config` and `filepath` properties, and must return a Promise that resolves with the transformed result.
176
177The 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.
178
179### Instance methods (on `explorer`)
180
181#### `load([searchPath, configPath])`
182
183Find and load a configuration file. Returns a Promise that resolves with `null`, if nothing is found, or an object with two properties:
184- `config`: The loaded and parsed configuration.
185- `filepath`: The filepath where this configuration was found.
186
187You should provide *either* `searchPath` *or* `configPath`. Use `configPath` if you know the path of the configuration file you want to load. Otherwise, use `searchPath`.
188
189```js
190explorer.load('start/search/here');
191explorer.load('start/search/at/this/file.css');
192
193explorer.load(null, 'load/this/file.json');
194```
195
196If you provide `searchPath`, cosmiconfig will start its search at `searchPath` and continue to search up the file tree, as documented above.
197
198If 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.
199
200#### `clearFileCache()`
201
202Clears the cache used when you provide a `configPath` argument to `load`.
203
204#### `clearDirectoryCache()`
205
206Clears the cache used when you provide a `searchPath` argument to `load`.
207
208#### `clearCaches()`
209
210Performs both `clearFileCache()` and `clearDirectoryCache()`.
211
212## Differences from [rc](https://github.com/dominictarr/rc)
213
214[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:
215
216- Looks for configuration in some different places: in a `package.json` property, an rc file, a `.config.js` file, and rc files with extensions.
217- Built-in support for JSON, YAML, and CommonJS formats.
218- Stops at the first configuration found, instead of finding all that can be found down the filetree and merging them automatically.
219- Options.
220- Asynchronicity.
221
222## Contributing & Development
223
224Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
225
226And please do participate!