-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
glob-pattern.js
63 lines (53 loc) · 1.42 KB
/
glob-pattern.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
import path from 'node:path';
import fs from 'node:fs';
import {globbySync, isDynamicPattern} from 'globby';
import {isNotJunk} from 'junk';
export default class GlobPattern {
/**
@param {string} pattern
@param {string} destination
@param {import('.').Options} options
*/
constructor(pattern, destination, options) {
this.path = pattern;
this.originalPath = pattern;
this.destination = destination;
this.options = options;
this.isDirectory = false;
if (
!isDynamicPattern(pattern)
&& fs.existsSync(pattern)
&& fs.lstatSync(pattern).isDirectory()
) {
this.path = [pattern, '**'].join('/');
this.isDirectory = true;
}
}
get name() {
return path.basename(this.originalPath);
}
get normalizedPath() {
const segments = this.originalPath.split('/');
const magicIndex = segments.findIndex(item => item ? isDynamicPattern(item) : false);
const normalized = segments.slice(0, magicIndex).join('/');
if (normalized) {
return path.isAbsolute(normalized) ? normalized : path.join(this.options.cwd, normalized);
}
return this.destination;
}
hasMagic() {
return isDynamicPattern(this.options.flat ? this.path : this.originalPath);
}
getMatches() {
let matches = globbySync(this.path, {
...this.options,
dot: true,
absolute: true,
onlyFiles: true,
});
if (this.options.ignoreJunk) {
matches = matches.filter(file => isNotJunk(path.basename(file)));
}
return matches;
}
}