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 | function loadYaml(filepath, content) {
|
---|
71 | return yaml.parse(content);
|
---|
72 | }
|
---|
73 |
|
---|
74 | const options = {
|
---|
75 | loaders: {
|
---|
76 | '.yaml': loadYaml,
|
---|
77 | '.yml': loadYaml,
|
---|
78 | // loader for files with no extension
|
---|
79 | noExt: loadYaml
|
---|
80 | }
|
---|
81 | };
|
---|
82 |
|
---|
83 | lilconfig('myapp', options)
|
---|
84 | .search()
|
---|
85 | .then(result => {
|
---|
86 | result // {config, filepath}
|
---|
87 | });
|
---|
88 | ```
|
---|
89 |
|
---|
90 | ## Version correlation
|
---|
91 |
|
---|
92 | - lilconig v1 → cosmiconfig v6
|
---|
93 | - lilconig v2 → cosmiconfig v7
|
---|