/
PluginFileHandler.ts
118 lines (99 loc) · 3.29 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
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
import { FileHandler } from './FileHandler';
import { FileSystem } from './FileSystem';
import { LicenseIdentifiedModule } from './LicenseIdentifiedModule';
import { PackageJson } from './PackageJson';
class PluginFileHandler implements FileHandler {
private cache: { [key: string]: any } = {};
constructor(
private fileSystem: FileSystem,
private buildRoot: string,
private modulesDirectories: string[] | null,
private excludedPackageTest: (packageName: string) => boolean
) {}
static PACKAGE_JSON: string = 'package.json';
getModule(filename: string): LicenseIdentifiedModule | null {
return this.cache[filename] === undefined
? (this.cache[filename] = this.getModuleInternal(filename))
: this.cache[filename];
}
isInModuleDirectory(filename: string) {
if (this.modulesDirectories === null) return true;
if (filename === null || filename === undefined) return false;
let foundInModuleDirectory = false;
const resolvedPath = this.fileSystem.resolvePath(filename);
for (const modulesDirectory of this.modulesDirectories) {
if (
resolvedPath.startsWith(this.fileSystem.resolvePath(modulesDirectory))
) {
foundInModuleDirectory = true;
}
}
return foundInModuleDirectory;
}
isBuildRoot(filename: string) {
return this.buildRoot === filename;
}
private getModuleInternal(
filename: string
): Partial<LicenseIdentifiedModule> | null {
if (filename === null || filename === undefined) return null;
if (!this.isInModuleDirectory(filename)) return null;
const module = this.findModuleDir(filename);
if (module !== null && this.excludedPackageTest(module.name)) {
return null;
}
return module;
}
private findModuleDir(
filename: string
): Pick<
LicenseIdentifiedModule,
'name' | 'packageJson' | 'directory'
> | null {
const pathSeparator = this.fileSystem.pathSeparator;
let dirOfModule = filename.substring(
0,
filename.lastIndexOf(pathSeparator)
);
let oldDirOfModule: string | null = null;
while (!this.dirContainsValidPackageJson(dirOfModule)) {
// check parent directory
oldDirOfModule = dirOfModule;
dirOfModule = this.fileSystem.resolvePath(
`${dirOfModule}${pathSeparator}..${pathSeparator}`
);
// reached filesystem root
if (oldDirOfModule === dirOfModule) {
return null;
}
}
if (this.isBuildRoot(dirOfModule)) {
return null;
}
const packageJson: PackageJson = this.parsePackageJson(dirOfModule);
return {
packageJson,
name: packageJson.name,
directory: dirOfModule
};
}
private parsePackageJson(dirOfModule: string): PackageJson {
const packageJsonText = this.fileSystem.readFileAsUtf8(
this.fileSystem.join(dirOfModule, PluginFileHandler.PACKAGE_JSON)
);
const packageJson: PackageJson = JSON.parse(packageJsonText);
return packageJson;
}
private dirContainsValidPackageJson(dirOfModule: string): boolean {
if (
!this.fileSystem.pathExists(
this.fileSystem.join(dirOfModule, PluginFileHandler.PACKAGE_JSON)
)
) {
return false;
}
const packageJson: PackageJson = this.parsePackageJson(dirOfModule);
return !!packageJson.name;
}
}
export { PluginFileHandler };