-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathindex.ts
97 lines (90 loc) · 2.63 KB
/
index.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
import { programmingLanguages } from '../../constants';
import type { RangeStrategy } from '../../types';
import managers from './api';
import type {
ExtractConfig,
GlobalManagerConfig,
ManagerApi,
PackageFile,
RangeConfig,
Result,
} from './types';
export { hashMap } from './fingerprint.generated';
const managerList = Array.from(managers.keys());
const languageList = programmingLanguages.concat();
export function get<T extends keyof ManagerApi>(
manager: string,
name: T
): ManagerApi[T] | undefined {
return managers.get(manager)?.[name];
}
export const getLanguageList = (): string[] => languageList;
export const getManagerList = (): string[] => managerList;
export const getManagers = (): Map<string, ManagerApi> => managers;
export async function detectAllGlobalConfig(): Promise<GlobalManagerConfig> {
let config: GlobalManagerConfig = {};
for (const managerName of managerList) {
const manager = managers.get(managerName)!;
if (manager.detectGlobalConfig) {
// This should use mergeChildConfig once more than one manager is supported, but introduces a cyclic dependency
config = { ...config, ...(await manager.detectGlobalConfig()) };
}
}
return config;
}
export async function extractAllPackageFiles(
manager: string,
config: ExtractConfig,
files: string[]
): Promise<PackageFile[] | null> {
if (!managers.has(manager)) {
return null;
}
const m = managers.get(manager)!;
if (m.extractAllPackageFiles) {
const res = await m.extractAllPackageFiles(config, files);
// istanbul ignore if
if (!res) {
return null;
}
return res;
}
return null;
}
export function extractPackageFile(
manager: string,
content: string,
fileName: string,
config: ExtractConfig
): Result<PackageFile | null> {
if (!managers.has(manager)) {
return null;
}
const m = managers.get(manager)!;
return m.extractPackageFile
? m.extractPackageFile(content, fileName, config)
: null;
}
export function getRangeStrategy(config: RangeConfig): RangeStrategy | null {
const { manager, rangeStrategy } = config;
if (!manager || !managers.has(manager)) {
return null;
}
const m = managers.get(manager)!;
if (m.getRangeStrategy) {
// Use manager's own function if it exists
const managerRangeStrategy = m.getRangeStrategy(config);
if (managerRangeStrategy === 'in-range-only') {
return 'update-lockfile';
}
return managerRangeStrategy;
}
if (rangeStrategy === 'auto') {
// default to 'replace' for auto
return 'replace';
}
if (rangeStrategy === 'in-range-only') {
return 'update-lockfile';
}
return config.rangeStrategy;
}