-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathfileSet.ts
More file actions
51 lines (45 loc) · 1.25 KB
/
fileSet.ts
File metadata and controls
51 lines (45 loc) · 1.25 KB
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
import { relative } from "path";
import minimatch = require("minimatch");
import * as glob from "glob";
import { invariant } from "@apollographql/apollo-tools";
import URI from "vscode-uri";
export class FileSet {
private rootURI: URI;
private includes: string[];
private excludes: string[];
constructor({
rootURI,
includes,
excludes
}: {
rootURI: URI;
includes: string[];
excludes: string[];
}) {
invariant(rootURI, `Must provide "rootURI".`);
invariant(includes, `Must provide "includes".`);
invariant(excludes, `Must provide "excludes".`);
this.rootURI = rootURI;
this.includes = includes;
this.excludes = excludes;
}
includesFile(filePath: string): boolean {
filePath = relative(this.rootURI.fsPath, filePath);
return (
this.includes.some(include => minimatch(filePath, include)) &&
!this.excludes.some(exclude => minimatch(filePath, exclude))
);
}
allFiles(): string[] {
return this.includes
.flatMap(include =>
glob.sync(include, { cwd: this.rootURI.fsPath, absolute: true })
)
.filter(
filePath =>
!this.excludes.some(exclude =>
minimatch(relative(this.rootURI.fsPath, filePath), exclude)
)
);
}
}