-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathpackageJsonCache.ts
79 lines (74 loc) · 2.98 KB
/
packageJsonCache.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
import {
combinePaths,
createPackageJsonInfo,
Debug,
forEachAncestorDirectoryStoppingAtGlobalCache,
getDirectoryPath,
Path,
ProjectPackageJsonInfo,
Ternary,
tryFileExists,
} from "./_namespaces/ts.js";
import {
Project,
ProjectService,
} from "./_namespaces/ts.server.js";
/** @internal */
export interface PackageJsonCache {
addOrUpdate(fileName: string, path: Path): void;
invalidate(path: Path): void;
delete(fileName: Path): void;
getInDirectory(directory: string): ProjectPackageJsonInfo | undefined;
directoryHasPackageJson(directory: string): Ternary;
searchDirectoryAndAncestors(directory: string, project: Project): void;
}
/** @internal */
export function createPackageJsonCache(host: ProjectService): PackageJsonCache {
const packageJsons = new Map<Path, ProjectPackageJsonInfo>();
const directoriesWithoutPackageJson = new Map<Path, true>();
return {
addOrUpdate,
invalidate,
delete: fileName => {
packageJsons.delete(fileName);
directoriesWithoutPackageJson.set(getDirectoryPath(fileName), true);
},
getInDirectory: directory => {
return packageJsons.get(host.toPath(combinePaths(directory, "package.json"))) || undefined;
},
directoryHasPackageJson: directory => directoryHasPackageJson(host.toPath(directory)),
searchDirectoryAndAncestors: (directory, project) => {
forEachAncestorDirectoryStoppingAtGlobalCache(
project,
directory,
ancestor => {
const ancestorPath = host.toPath(ancestor);
if (directoryHasPackageJson(ancestorPath) !== Ternary.Maybe) {
return true;
}
const packageJsonFileName = combinePaths(ancestor, "package.json");
if (tryFileExists(host, packageJsonFileName)) {
addOrUpdate(packageJsonFileName, combinePaths(ancestorPath, "package.json") as Path);
}
else {
directoriesWithoutPackageJson.set(ancestorPath, true);
}
},
);
},
};
function addOrUpdate(fileName: string, path: Path) {
const packageJsonInfo = Debug.checkDefined(createPackageJsonInfo(fileName, host.host));
packageJsons.set(path, packageJsonInfo);
directoriesWithoutPackageJson.delete(getDirectoryPath(path));
}
function invalidate(path: Path) {
packageJsons.delete(path);
directoriesWithoutPackageJson.delete(getDirectoryPath(path));
}
function directoryHasPackageJson(directory: Path) {
return packageJsons.has(combinePaths(directory, "package.json") as Path) ? Ternary.True :
directoriesWithoutPackageJson.has(directory) ? Ternary.False :
Ternary.Maybe;
}
}