-
Notifications
You must be signed in to change notification settings - Fork 53
/
PluginFileHandler.ts
82 lines (69 loc) · 2.14 KB
/
PluginFileHandler.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
import { FileHandler } from './FileHandler';
import { FileSystem } from './FileSystem';
import { Module } from './Module';
import { PackageJson } from './PackageJson';
class PluginFileHandler implements FileHandler {
constructor(
private fileSystem: FileSystem,
private buildRoot: string,
private modulesDirectories: string[] | null,
private excludedPackageTest: ((packageName: string) => boolean)
) {}
getModule(filename: string): Module | null {
if (filename === null || filename === undefined) {
return null;
}
if (this.modulesDirectories !== null) {
let foundInModuleDirectory = false;
for (const modulesDirectory of this.modulesDirectories) {
if (this.fileSystem.isFileInDirectory(filename, modulesDirectory)) {
foundInModuleDirectory = true;
}
}
if (!foundInModuleDirectory) {
return null;
}
}
const module: Module | null = this.findModuleDir(filename);
if (module !== null && this.excludedPackageTest(module.name)) {
return null;
}
return module;
}
private findModuleDir(filename: string): Module | null {
const pathSeparator = this.fileSystem.pathSeparator;
const PACKAGE_JSON = 'package.json';
let dirOfModule = filename.substring(
0,
filename.lastIndexOf(pathSeparator)
);
let oldDirOfModule: string | null = null;
while (
!this.fileSystem.pathExists(
this.fileSystem.join(dirOfModule, PACKAGE_JSON)
)
) {
// check parent directory
oldDirOfModule = dirOfModule;
dirOfModule = this.fileSystem.resolvePath(
`${dirOfModule}${pathSeparator}..${pathSeparator}`
);
// reached filesystem root
if (oldDirOfModule === dirOfModule) {
return null;
}
}
if (this.buildRoot === dirOfModule) {
return null;
}
const packageJsonText = this.fileSystem.readFileAsUtf8(
this.fileSystem.join(dirOfModule, PACKAGE_JSON)
);
const packageJson: PackageJson = JSON.parse(packageJsonText);
return {
name: packageJson.name,
directory: dirOfModule
};
}
}
export { PluginFileHandler };