/
utils.js
98 lines (82 loc) · 2.19 KB
/
utils.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
'use strict';
const fs = require('fs');
const os = require('os');
const path = require('path');
const picomatch = require('picomatch');
const union = (...args) => [...new Set([].concat.apply([], args).filter(Boolean))];
/**
* Expand tilde
*/
exports.expand = str => str.replace(/^~/, os.homedir());
/**
* Sift glob patterns into inclusive and exclusive patterns.
*
* @param {String|Array} `patterns`
* @param {Object} opts
* @return {Object}
*/
exports.sift = (patterns, options = {}) => {
let results = { includes: [], excludes: [], globs: 0 };
let index = 0;
for (let pattern of [].concat(patterns || [])) {
if (typeof pattern !== 'string') return null;
let res = picomatch.scan(pattern);
res.pattern = path.posix.join(res.base, res.glob);
res.index = index++;
if (res.isGlob) results.globs++;
if (options.relative) {
res.pattern = exports.toRelative(res.pattern, options);
delete options.cwd;
}
if (res.negated) {
results.excludes.push(res);
} else {
results.includes.push(res);
}
}
return results;
};
/**
* Set the index of ignore patterns based on their position
* in an array of globs.
*
* @param {Object} `options`
* @param {Array} `excludes`
* @param {Number} `inclusiveIndex`
*/
exports.setIgnores = (options, excludes, inclusiveIndex) => {
let opts = Object.assign({}, options);
let negations = [];
for (let exclusive of excludes) {
if (exclusive.index > inclusiveIndex) {
negations.push(exclusive.pattern);
}
}
opts.ignore = union([], opts.ignore, negations);
return opts;
};
/**
* Make a glob pattern relative.
*
* @param {String} `pattern`
* @param {Object} `opts`
* @return {String}
*/
exports.toRelative = (pattern, opts) => {
return path.relative(process.cwd(), path.resolve(exports.expand(opts.cwd), pattern));
};
/**
* Get paths from non-glob patterns
*
* @param {Array} `paths`
* @param {Object} `opts`
* @return {Array}
*/
exports.getPaths = (paths, options = {}) => {
let resolve = fp => path.resolve(exports.expand(options.cwd), fp);
paths = paths.filter(fp => fs.existsSync(resolve(fp)));
if (options.realpath) {
return paths.map(resolve);
}
return paths;
};