/
HasteFS.ts
100 lines (85 loc) · 2.72 KB
/
HasteFS.ts
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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import micromatch from 'micromatch';
import {replacePathSepForGlob} from 'jest-util';
import {Config} from '@jest/types';
import {FileData} from './types';
import * as fastPath from './lib/fast_path';
import H from './constants';
export default class HasteFS {
private readonly _rootDir: Config.Path;
private readonly _files: FileData;
constructor({rootDir, files}: {rootDir: Config.Path; files: FileData}) {
this._rootDir = rootDir;
this._files = files;
}
getModuleName(file: Config.Path): string | null {
const fileMetadata = this._getFileData(file);
return (fileMetadata && fileMetadata[H.ID]) || null;
}
getSize(file: Config.Path): number | null {
const fileMetadata = this._getFileData(file);
return (fileMetadata && fileMetadata[H.SIZE]) || null;
}
getDependencies(file: Config.Path): Array<string> | null {
const fileMetadata = this._getFileData(file);
if (fileMetadata) {
return fileMetadata[H.DEPENDENCIES]
? fileMetadata[H.DEPENDENCIES].split(H.DEPENDENCY_DELIM)
: [];
} else {
return null;
}
}
getSha1(file: Config.Path): string | null {
const fileMetadata = this._getFileData(file);
return (fileMetadata && fileMetadata[H.SHA1]) || null;
}
exists(file: Config.Path): boolean {
return this._getFileData(file) != null;
}
getAllFiles(): Array<Config.Path> {
return Array.from(this.getAbsoluteFileIterator());
}
getFileIterator(): Iterable<Config.Path> {
return this._files.keys();
}
*getAbsoluteFileIterator(): Iterable<Config.Path> {
for (const file of this.getFileIterator()) {
yield fastPath.resolve(this._rootDir, file);
}
}
matchFiles(pattern: RegExp | string): Array<Config.Path> {
if (!(pattern instanceof RegExp)) {
pattern = new RegExp(pattern);
}
const files = [];
for (const file of this.getAbsoluteFileIterator()) {
if (pattern.test(file)) {
files.push(file);
}
}
return files;
}
matchFilesWithGlob(
globs: Array<Config.Glob>,
root: Config.Path | null,
): Set<Config.Path> {
const files = new Set<string>();
for (const file of this.getAbsoluteFileIterator()) {
const filePath = root ? fastPath.relative(root, file) : file;
if (micromatch([replacePathSepForGlob(filePath)], globs).length) {
files.add(file);
}
}
return files;
}
private _getFileData(file: Config.Path) {
const relativePath = fastPath.relative(this._rootDir, file);
return this._files.get(relativePath);
}
}