-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
107 lines (87 loc) · 2.09 KB
/
index.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
/*!
* resolve-glob </~https://github.com/jonschlinkert/resolve-glob>
*
* Copyright (c) 2015-2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
var path = require('path');
var extend = require('extend-shallow');
var isValidGlob = require('is-valid-glob');
var resolveDir = require('resolve-dir');
var relative = require('relative');
var glob = require('matched');
/**
* async
*/
module.exports = function(patterns, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
if (typeof cb !== 'function') {
throw new TypeError('expected callback to be a function');
}
if (!isValidGlob(patterns)) {
cb(new TypeError('expected glob to be a string or array'));
return;
}
var opts = createOptions(options);
var nonull = opts.nonull === true;
delete opts.nonull;
glob(patterns, opts, function(err, files) {
if (err) {
cb(err);
return;
}
if (!files.length && nonull) {
cb(null, arrayify(patterns));
return;
}
cb(null, resolveFiles(files, opts));
});
};
/**
* sync
*/
module.exports.sync = function(patterns, options) {
if (!isValidGlob(patterns)) {
throw new TypeError('expected glob to be a string or array');
}
var opts = createOptions(options);
var nonull = opts.nonull === true;
delete opts.nonull;
var files = glob.sync(patterns, opts);
if (!files.length && nonull) {
return arrayify(patterns);
}
return resolveFiles(files, opts);
};
/**
* Utils
*/
function arrayify(val) {
return val ? (Array.isArray(val) ? val : [val]) : [];
}
function createOptions(options) {
var opts = extend({cwd: process.cwd()}, options);
opts.cwd = path.resolve(resolve(opts.cwd));
return opts;
}
function resolveFiles(files, opts) {
var len = files.length, i = -1;
while (++i < len) {
files[i] = resolveFile(files[i], opts);
}
return files;
}
function resolveFile(fp, opts) {
fp = path.resolve(opts.cwd, fp);
if (opts.relative) {
fp = relative(process.cwd(), fp);
}
return fp;
}
function resolve(cwd) {
return path.resolve(resolveDir(cwd));
}