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
|
'use strict';
var fs = require('fs');
var path = require('path');
var isPlainObject = require('is-plain-object');
var pick = require('object.pick');
var defaults = require('object.defaults/immutable');
var expandTilde = require('expand-tilde');
var parsePath = require('parse-filepath');
function fined(pathObj, defaultObj) {
var expandedPath = expandPath(pathObj, defaultObj);
return expandedPath ? findWithExpandedPath(expandedPath) : null;
}
function expandPath(pathObj, defaultObj) {
if (!isPlainObject(defaultObj)) {
defaultObj = {};
}
if (isString(pathObj)) {
pathObj = { path: pathObj };
}
if (!isPlainObject(pathObj)) {
pathObj = {};
}
pathObj = defaults(pathObj, defaultObj);
var filePath;
if (!isString(pathObj.path)) {
return null;
}
// Execution of toString is for a String object.
if (isString(pathObj.name) && pathObj.name) {
if (pathObj.path) {
filePath = expandTilde(pathObj.path.toString());
filePath = path.join(filePath, pathObj.name.toString());
} else {
filePath = pathObj.name.toString();
}
} else {
filePath = expandTilde(pathObj.path.toString());
}
var extArr = createExtensionArray(pathObj.extensions);
var extMap = createExtensionMap(pathObj.extensions);
var basedir = isString(pathObj.cwd) ? pathObj.cwd.toString() : '.';
basedir = path.resolve(expandTilde(basedir));
var findUp = !!pathObj.findUp;
var parsed = parsePath(filePath);
if (parsed.isAbsolute) {
filePath = filePath.slice(parsed.root.length);
findUp = false;
basedir = parsed.root;
/* istanbul ignore if */
} else if (parsed.root) { // Expanded path has a drive letter on Windows.
filePath = filePath.slice(parsed.root.length);
basedir = path.resolve(parsed.root);
}
return {
path: filePath,
basedir: basedir,
findUp: findUp,
extArr: extArr,
extMap: extMap,
};
}
function findWithExpandedPath(expanded) {
var found = expanded.findUp ?
findUpFile(expanded.basedir, expanded.path, expanded.extArr) :
findFile(expanded.basedir, expanded.path, expanded.extArr);
if (!found) {
return null;
}
if (expanded.extMap) {
found.extension = pick(expanded.extMap, found.extension);
}
return found;
}
function findFile(basedir, relpath, extArr) {
var noExtPath = path.resolve(basedir, relpath);
for (var i = 0, n = extArr.length; i < n; i++) {
var filepath = noExtPath + extArr[i];
try {
fs.statSync(filepath);
return { path: filepath, extension: extArr[i] };
} catch (e) {}
}
return null;
}
function findUpFile(basedir, filepath, extArr) {
var lastdir;
do {
var found = findFile(basedir, filepath, extArr);
if (found) {
return found;
}
lastdir = basedir;
basedir = path.dirname(basedir);
} while (lastdir !== basedir);
return null;
}
function createExtensionArray(exts) {
if (isString(exts)) {
return [exts];
}
if (Array.isArray(exts)) {
exts = exts.filter(isString);
return (exts.length > 0) ? exts : [''];
}
if (isPlainObject(exts)) {
exts = Object.keys(exts);
return (exts.length > 0) ? exts : [''];
}
return [''];
}
function createExtensionMap(exts) {
if (!isPlainObject(exts)) {
return null;
}
if (isEmpty(exts)) {
return { '': null };
}
return exts;
}
function isEmpty(object) {
return !Object.keys(object).length;
}
function isString(value) {
if (typeof value === 'string') {
return true;
}
if (Object.prototype.toString.call(value) === '[object String]') {
return true;
}
return false;
}
module.exports = fined;
|