UNPKG

1.41 kBPlain TextView Raw
1import { relative } from "path";
2import minimatch = require("minimatch");
3import glob from "glob";
4import { invariant } from "@apollographql/apollo-tools";
5import URI from "vscode-uri";
6import { normalizeURI } from "./utilities";
7
8export class FileSet {
9 private rootURI: URI;
10 private includes: string[];
11 private excludes: string[];
12
13 constructor({
14 rootURI,
15 includes,
16 excludes,
17 configURI,
18 }: {
19 rootURI: URI;
20 includes: string[];
21 excludes: string[];
22 configURI?: URI;
23 }) {
24 invariant(rootURI, `Must provide "rootURI".`);
25 invariant(includes, `Must provide "includes".`);
26 invariant(excludes, `Must provide "excludes".`);
27
28 this.rootURI = rootURI;
29 this.includes = includes;
30 this.excludes = excludes;
31 }
32
33 includesFile(filePath: string): boolean {
34 return this.allFiles().includes(normalizeURI(filePath));
35 }
36
37 allFiles(): string[] {
38 // since glob.sync takes a single pattern, but we allow an array of `includes`, we can join all the
39 // `includes` globs into a single pattern and pass to glob.sync. The `ignore` option does, however, allow
40 // an array of globs to ignore, so we can pass it in directly
41 const joinedIncludes = `{${this.includes.join(",")}}`;
42 return glob
43 .sync(joinedIncludes, {
44 cwd: this.rootURI.fsPath,
45 absolute: true,
46 ignore: this.excludes,
47 })
48 .map(normalizeURI);
49 }
50}