UNPKG

6.36 kBTypeScriptView Raw
1import minimist = require("minimist");
2
3interface RcResultType extends Record<number | string | symbol, unknown> {
4 /**
5 * Contains file path of all parsed configurations from different sources
6 * like '/etc/${appname}rc', '/etc/${appname}/config', and other.
7 */
8 configs?: string[];
9 /**
10 * same as RcResultType.configs[RcResultType.configs.length - 1]
11 */
12 config?: string;
13}
14
15declare function rc(
16 /**
17 * The name of the app to configure, rc will search for this files :
18 * `/etc/${name}/config`,
19 * `/etc/${name}rc`,
20 * `~/.config/${name}/config`,
21 * `~/.config/${name}`,
22 * `~/.${name}/config`,
23 * `~/.${name}rc` and
24 * `.${name}rc` in CWD and its ancestors
25 */
26 name: string,
27 /**
28 * Default config values.
29 * Can be an Object that contains the default values for the config,
30 * the path of a JSON or INI file to use as default config or
31 * can be `null` or `undefined` for no default values
32 */
33 defaults?: string | null,
34 /**
35 * Parsed argv object.
36 * For example, if args is `--foo bar`, then this value should be `{foo: 'bar'}`
37 * If `argv` is `null` or `undefined`, then `rc`'s default parser will parse `process.argv`.
38 */
39 argv?: null,
40 /**
41 * Custom config file parser.
42 * This function will be passed the string contents of each
43 * discovered configuration file, should return a parsed object dictionary.
44 */
45 parse?: (content: string) => object,
46): RcResultType & minimist.ParsedArgs;
47declare function rc<T extends object>(
48 /**
49 * The name of the app to configure, rc will search for this files :
50 * `/etc/${name}/config`,
51 * `/etc/${name}rc`,
52 * `~/.config/${name}/config`,
53 * `~/.config/${name}`,
54 * `~/.${name}/config`,
55 * `~/.${name}rc` and
56 * `.${name}rc` in CWD and its ancestors
57 */
58 name: string,
59 /**
60 * Default config values.
61 * Can be an Object that contains the default values for the config,
62 * the path of a JSON or INI file to use as default config or
63 * can be `null` or `undefined` for no default values
64 */
65 defaults: T,
66 /**
67 * Parsed argv object.
68 * For example, if args is `--foo bar`, then this value should be `{foo: 'bar'}`
69 * If `argv` is `null` or `undefined`, then `rc`'s default parser will parse `process.argv`.
70 */
71 argv?: null,
72 /**
73 * Custom config file parser.
74 * This function will be passed the string contents of each
75 * discovered configuration file, should return a parsed object dictionary.
76 */
77 parse?: (content: string) => object,
78): T & RcResultType & minimist.ParsedArgs;
79declare function rc<U extends object>(
80 /**
81 * The name of the app to configure, rc will search for this files :
82 * `/etc/${name}/config`,
83 * `/etc/${name}rc`,
84 * `~/.config/${name}/config`,
85 * `~/.config/${name}`,
86 * `~/.${name}/config`,
87 * `~/.${name}rc` and
88 * `.${name}rc` in CWD and its ancestors
89 */
90 name: string,
91 /**
92 * Default config values.
93 * Can be an Object that contains the default values for the config,
94 * the path of a JSON or INI file to use as default config or
95 * can be `null` or `undefined` for no default values
96 */
97 defaults: string | null | undefined,
98 /**
99 * Parsed argv object.
100 * For example, if args is `--foo bar`, then this value should be `{foo: 'bar'}`
101 * If `argv` is `null` or `undefined`, then `rc`'s default parser will parse `process.argv`.
102 */
103 argv: U,
104 /**
105 * Custom config file parser.
106 * This function will be passed the string contents of each
107 * discovered configuration file, should return a parsed object dictionary.
108 */
109 parse?: (content: string) => object,
110): U & RcResultType;
111declare function rc<
112 T extends object,
113 U extends object,
114>(
115 /**
116 * The name of the app to configure, rc will search for this files :
117 * `/etc/${name}/config`,
118 * `/etc/${name}rc`,
119 * `~/.config/${name}/config`,
120 * `~/.config/${name}`,
121 * `~/.${name}/config`,
122 * `~/.${name}rc` and
123 * `.${name}rc` in CWD and its ancestors
124 */
125 name: string,
126 /**
127 * Default config values.
128 * Can be an Object that contains the default values for the config,
129 * the path of a JSON or INI file to use as default config or
130 * can be `null` or `undefined` for no default values
131 */
132 defaults: T,
133 /**
134 * Parsed argv object.
135 * For example, if args is `--foo bar`, then this value should be `{foo: 'bar'}`
136 * If `argv` is `null` or `undefined`, then `rc`'s default parser will parse `process.argv`.
137 */
138 argv: U,
139 /**
140 * Custom config file parser.
141 * This function will be passed the string contents of each
142 * discovered configuration file, should return a parsed object dictionary.
143 */
144 parse?: (content: string) => object,
145): T & U & RcResultType;
146declare function rc<
147 T extends object | string | null | undefined,
148 U extends object | null | undefined,
149>(
150 /**
151 * The name of the app to configure, rc will search for this files :
152 * `/etc/${name}/config`,
153 * `/etc/${name}rc`,
154 * `~/.config/${name}/config`,
155 * `~/.config/${name}`,
156 * `~/.${name}/config`,
157 * `~/.${name}rc` and
158 * `.${name}rc` in CWD and its ancestors
159 */
160 name: string,
161 /**
162 * Default config values.
163 * Can be an Object that contains the default values for the config,
164 * the path of a JSON or INI file to use as default config or
165 * can be `null` or `undefined` for no default values
166 */
167 defaults: T,
168 /**
169 * Parsed argv object.
170 * For example, if args is `--foo bar`, then this value should be `{foo: 'bar'}`
171 * If `argv` is `null` or `undefined`, then `rc`'s default parser will parse `process.argv`.
172 */
173 argv: U,
174 /**
175 * Custom config file parser.
176 * This function will be passed the string contents of each
177 * discovered configuration file, should return a parsed object dictionary.
178 */
179 parse?: (content: string) => object,
180): T extends string | null | undefined ? (U extends null | undefined ? minimist.ParsedArgs : U) & RcResultType
181 : T & (U extends null | undefined ? minimist.ParsedArgs : U) & RcResultType;
182
183export = rc;