1 | # Lilconfig ⚙️
|
---|
2 | [![npm version](https://badge.fury.io/js/lilconfig.svg)](https://badge.fury.io/js/lilconfig)
|
---|
3 | [![install size](https://packagephobia.now.sh/badge?p=lilconfig)](https://packagephobia.now.sh/result?p=lilconfig)
|
---|
4 | [![Coverage Status](https://coveralls.io/repos/github/antonk52/lilconfig/badge.svg)](https://coveralls.io/github/antonk52/lilconfig)
|
---|
5 |
|
---|
6 | A zero-dependency alternative to [cosmiconfig](https://www.npmjs.com/package/cosmiconfig) with the same API.
|
---|
7 |
|
---|
8 | ## Installation
|
---|
9 |
|
---|
10 | ```sh
|
---|
11 | npm install lilconfig
|
---|
12 | ```
|
---|
13 |
|
---|
14 | ## Usage
|
---|
15 |
|
---|
16 | ```js
|
---|
17 | import {lilconfig, lilconfigSync} from 'lilconfig';
|
---|
18 |
|
---|
19 | // all keys are optional
|
---|
20 | const options = {
|
---|
21 | stopDir: '/Users/you/some/dir',
|
---|
22 | searchPlaces: ['package.json', 'myapp.conf.js'],
|
---|
23 | ignoreEmptySearchPlaces: false
|
---|
24 | }
|
---|
25 |
|
---|
26 | lilconfig(
|
---|
27 | 'myapp',
|
---|
28 | options // optional
|
---|
29 | ).search() // Promise<LilconfigResult>
|
---|
30 |
|
---|
31 | lilconfigSync(
|
---|
32 | 'myapp',
|
---|
33 | options // optional
|
---|
34 | ).load(pathToConfig) // LilconfigResult
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * LilconfigResult
|
---|
38 | * {
|
---|
39 | * config: any; // your config
|
---|
40 | * filepath: string;
|
---|
41 | * }
|
---|
42 | */
|
---|
43 | ```
|
---|
44 |
|
---|
45 | ## Difference to `cosmiconfig`
|
---|
46 | Lilconfig does not intend to be 100% compatible with `cosmiconfig` but tries to mimic it where possible. The key differences are:
|
---|
47 | - **no** support for yaml files out of the box(`lilconfig` attempts to parse files with no extension as JSON instead of YAML). You can still add the support for YAML files by providing a loader, see an [example](#loaders-example) below.
|
---|
48 | - **no** cache
|
---|
49 |
|
---|
50 | ### Options difference between the two.
|
---|
51 |
|
---|
52 | |cosmiconfig option | lilconfig |
|
---|
53 | |------------------------|-----------|
|
---|
54 | |cache | ❌ |
|
---|
55 | |loaders | ✅ |
|
---|
56 | |ignoreEmptySearchPlaces | ✅ |
|
---|
57 | |packageProp | ✅ |
|
---|
58 | |searchPlaces | ✅ |
|
---|
59 | |stopDir | ✅ |
|
---|
60 | |transform | ✅ |
|
---|
61 |
|
---|
62 | ## Loaders example
|
---|
63 |
|
---|
64 | If you need the YAML support you can provide your own loader
|
---|
65 |
|
---|
66 | ```js
|
---|
67 | import {lilconig} from 'lilconfig';
|
---|
68 | import yaml from 'yaml';
|
---|
69 |
|
---|
70 | const options = {
|
---|
71 | loaders: {
|
---|
72 | '.yaml': (filepath, content) => {
|
---|
73 | return yaml.parse(content);
|
---|
74 | },
|
---|
75 | // loader for files with no extension
|
---|
76 | noExt: (filepath, content) => {
|
---|
77 | return yaml.parse(content);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | };
|
---|
81 |
|
---|
82 | lilconfig('myapp', options)
|
---|
83 | .search()
|
---|
84 | .then(result => {
|
---|
85 | result // {config, filepath}
|
---|
86 | });
|
---|
87 | ```
|
---|
88 |
|
---|
89 | ## Version correlation
|
---|
90 |
|
---|
91 | - lilconig v1 → cosmiconfig v6
|
---|
92 | - lilconig v2 → cosmiconfig v7
|
---|