-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
179 lines (146 loc) · 4.68 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
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
175
176
177
178
179
import path from 'path';
import ig from 'ignored';
import fs from 'fs-extra';
import _ from 'underscore';
import op from 'object-path';
import micromatch from 'micromatch';
const defaultOptions = {
expandDirectories: true,
gitignore: false,
ignoreFiles: [],
deep: 10,
};
const isMatch = (str, patterns) => {
let matched = false;
const matches = Array.from(_.flatten([patterns]));
while (matches.length > 0 && matched === false) {
matched = micromatch.isMatch(str, matches.shift());
}
return matched;
};
const defaultIgnore = [
'**/.DS_Store',
'**/node_modules',
'**/flow-typed',
'**/coverage',
'**/.git',
'**/.vscode',
'**.log',
'**/LICENSE',
];
const gitIgnore = _.flatten([ig(), defaultIgnore]).sort();
const posix = (p) =>
String(p)
.split(/[\\\/]/g)
.join(path.posix.sep);
class Glob {
constructor(options = {}) {
this.__options = { ...defaultOptions, ...options };
}
get opt() {
return this.__options;
}
get walk() {
return (dir, pattern) =>
new Promise(async (resolve) => {
const output = [];
const results = await fs.readdir(dir);
if (
String(posix(path.resolve(dir))).split(path.posix.sep)
.length > this.opt.deep
) {
resolve([]);
}
const ignorePatterns = !op.get(this.opt, 'gitignore')
? op.get(this.opt, 'ignoreFiles', [])
: gitIgnore;
while (results.length > 0) {
const item = results.shift();
if (isMatch(item, ignorePatterns)) continue;
const r = posix(path.resolve(dir, item));
const stat = await fs.stat(r);
if (stat) {
if (
stat.isDirectory() &&
op.get(this.opt, 'expandDirectories')
) {
const sub = await this.walk(r, pattern);
sub.forEach((i) => output.push(i));
} else {
if (isMatch(item, pattern)) {
output.push(r);
}
}
}
}
resolve(output);
});
}
get glob() {
return async (pattern, options) => {
options = { ...this.opt, options } || { ...this.opt };
if (!op.get(options, 'cwd')) op.set(options, 'cwd', process.cwd());
const files = await this.walk(posix(options.cwd), pattern);
return files;
};
}
}
class GlobSync {
constructor(options = {}) {
this.__options = { ...defaultOptions, ...options };
}
get opt() {
return this.__options;
}
get walk() {
return (dir, pattern) => {
const output = [];
const results = fs.readdirSync(dir);
const ignorePatterns = !op.get(this.opt, 'gitignore')
? op.get(this.opt, 'ignoreFiles', [])
: gitIgnore;
while (results.length > 0) {
const item = results.shift();
if (isMatch(item, ignorePatterns)) continue;
const r = posix(path.resolve(dir, item));
const stat = fs.statSync(r);
if (stat) {
if (
stat.isDirectory() === true &&
op.get(this.opt, 'expandDirectories')
) {
const sub = this.walk(r, pattern);
sub.forEach((i) => output.push(i));
} else {
if (isMatch(item, pattern)) output.push(r);
}
} else {
console.log({ error: 'no stat', r });
}
}
return output;
};
}
get glob() {
return (pattern, options) => {
options = { ...this.opt, options } || { ...this.opt };
if (!op.get(options, 'cwd')) op.set(options, 'cwd', process.cwd());
const files = this.walk(posix(options.cwd), pattern);
return files;
};
}
}
const glob = (patterns, options) => {
if (patterns) {
const G = new Glob(options);
return G.glob(patterns);
}
};
export const globSync = (patterns, options) => {
if (patterns) {
const G = new GlobSync(options);
return G.glob(patterns);
}
};
glob.sync = globSync;
export default glob;