/
userdirfile.js
174 lines (157 loc) · 5.2 KB
/
userdirfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
// MODULES //
var debug = require( 'debug' )( 'find-user-app-config:userdirfile' ),
extname = require( 'utils-extname' ),
upsearch = require( 'utils-upsearch' ).sync,
load = require( 'app-etc-load' ),
check = require( './exists.js' ),
pkgrc = require( './pkgrc.js' );
// STRATEGY //
/**
* CASE 1: file name with extension but no format.
*
* => Search for `<name>` and then `.<name>` in `<udir>`.
* => If found, parse according to `<ext>`.
*
* => Search for `.<name>` starting from the current working directory.
* => If found, parse according to `<ext>`.
*
*
* CASE 2: file name with no extension and no format
* => Search for `.<name>` and `.<name>rc` in `<udir>`.
* => If found, parse as `ini`.
*
* => Search for `<name>` in `<udir>`.
* => If found, parse according to `ini`.
*
* => Search for `<name>.<ext>` and `.<name>.<ext>` in `<udir>`.
* => If found, parse according to `<ext>`.
*
* => Search for `.<name>` and `.<name>rc` starting from the current working directory.
* => If found, parse as `ini`.
*
*
* CASE 3: file name with extension and a format.
*
* => Search for `<name>` and then `.<name>` in `<udir>`.
* => If found, parse according to `<ext>`.
*
* => Search for `.<name>` starting from the current working directory.
* => If found, parse according to `<ext>`.
*
*
* CASE 4: file name with no extension but provided a format.
* => Search for `.<name>` and `.<name>rc` in `<udir>`.
* => If found, parse according to `<format>`.
*
* => Search for `<name>` in `<udir>`.
* => If found, parse according to `<format>`.
*
* => Search for `<name>.<ext>` and `.<name>.<ext>` in `<udir>`.
* => If found, parse according to `<ext>`.
*
* => Search for `.<name>` and `.<name>rc` starting from the current working directory.
* => If found, parse according to `<format>`.
*/
// RESOLVE //
/**
* FUNCTION: resolve( udir, name, fmt )
* Searches for a user configuration file having a specified basename.
*
* @param {String} udir - user configuration directory
* @param {String} name - basename
* @param {Void|String} fmt - configuration file format
* @returns {Object|Null} configuration object or null
*/
function resolve( udir, name, fmt ) {
var fpath,
base,
ext;
ext = extname( name );
// Basename has an extension...
if ( ext ) {
base = name;
fpath = check( udir, base, false );
if ( fpath ) {
debug( 'Found a user configuration file: %s. Attempting to load...', fpath );
return load( fpath );
} else {
debug( 'Unable to resolve a user configuration file with basename `%s` in directory `%s`.', base, udir );
}
if ( name[ 0 ] !== '.' ) {
base = '.' + name;
fpath = check( udir, base, false );
if ( fpath ) {
debug( 'Found a user configuration file: %s. Attempting to load...', fpath );
return load( fpath );
} else {
debug( 'Unable to resolve a user configuration file with basename `%s` in directory `%s`.', base, udir );
}
}
// File basename should already begin with a '.'.
fpath = upsearch( base );
if ( fpath ) {
debug( 'Found a user configuration file: %s. Attempting to load...', fpath );
return load( fpath );
} else {
debug( 'Unable to resolve a user configuration file with basename `%s`.', base );
}
debug( 'Unable to resolve a user configuration file.' );
return null;
}
// No extension...
fmt = fmt || 'ini';
// Search for a dot or rc file...
if ( name[ 0 ] === '.' ) {
base = name;
} else {
base = '.' + name;
}
fpath = check( udir, base, false );
if ( fpath ) {
debug( 'Found a user configuration file: %s. Attempting to load and parse as `%s`...', fpath, fmt );
return load( fpath, fmt );
} else {
debug( 'Unable to resolve a user configuration file with basename `%s` in directory `%s`.', base, udir );
}
base += 'rc';
fpath = check( udir, base, false );
if ( fpath ) {
debug( 'Found a user configuration file: %s. Attempting to load and parse as `%s`...', fpath, fmt );
return load( fpath, fmt );
} else {
debug( 'Unable to resolve a user configuration file with basename `%s` in directory `%s`.', base, udir );
}
// Check for a file without an extension...
base = name;
fpath = check( udir, base, false );
if ( fpath ) {
debug( 'Found a user configuration file: %s. Attempting to load...', fpath );
return load( fpath, fmt );
} else {
debug( 'Unable to resolve a user configuration file with basename `%s` in directory `%s`.', base, udir );
}
// Check for a file having a supported extension...
base = name;
fpath = check( udir, base, true );
if ( fpath ) {
debug( 'Found a user configuration file: %s. Attempting to load...', fpath );
return load( fpath );
} else {
debug( 'Unable to resolve a user configuration file with basename `%s` and a supported extension name in directory `%s`.', base, udir );
}
if ( name[ 0 ] !== '.' ) {
base = '.' + name;
fpath = check( udir, base, true );
if ( fpath ) {
debug( 'Found a user configuration file: %s. Attempting to load...', fpath );
return load( fpath );
} else {
debug( 'Unable to resolve a user configuration file with basename `%s` and a supported extension name in directory `%s`.', base, udir );
}
}
// Walk up from the cwd in search of a dot or rc file...
return pkgrc( name, fmt );
} // end FUNCTION resolve()
// EXPORTS //
module.exports = resolve;