2022-11-05 08:49:57 +00:00
|
|
|
import { programmingLanguages } from '../../constants';
|
2022-03-03 09:35:26 +00:00
|
|
|
import type { RangeStrategy } from '../../types';
|
2021-03-02 15:57:02 +00:00
|
|
|
import managers from './api';
|
2021-03-02 20:44:55 +00:00
|
|
|
import type {
|
2020-05-01 16:03:48 +00:00
|
|
|
ExtractConfig,
|
2021-09-29 19:58:42 +00:00
|
|
|
GlobalManagerConfig,
|
2020-05-01 16:03:48 +00:00
|
|
|
ManagerApi,
|
2023-02-20 14:58:49 +00:00
|
|
|
PackageFile,
|
2023-02-19 12:43:48 +00:00
|
|
|
PackageFileContent,
|
2020-05-01 16:03:48 +00:00
|
|
|
RangeConfig,
|
|
|
|
Result,
|
2021-03-02 20:44:55 +00:00
|
|
|
} from './types';
|
2022-04-06 14:28:48 +00:00
|
|
|
export { hashMap } from './fingerprint.generated';
|
2020-04-06 17:38:20 +00:00
|
|
|
const managerList = Array.from(managers.keys());
|
2019-07-25 06:17:19 +00:00
|
|
|
|
2022-11-05 08:49:57 +00:00
|
|
|
const languageList = programmingLanguages.concat();
|
2019-07-25 06:17:19 +00:00
|
|
|
|
2020-04-06 17:38:20 +00:00
|
|
|
export function get<T extends keyof ManagerApi>(
|
2019-11-23 20:44:55 +00:00
|
|
|
manager: string,
|
|
|
|
name: T
|
2022-04-20 06:40:20 +00:00
|
|
|
): ManagerApi[T] | undefined {
|
2020-04-06 17:38:20 +00:00
|
|
|
return managers.get(manager)?.[name];
|
|
|
|
}
|
2019-08-22 15:42:35 +00:00
|
|
|
export const getLanguageList = (): string[] => languageList;
|
|
|
|
export const getManagerList = (): string[] => managerList;
|
2020-04-06 17:38:20 +00:00
|
|
|
export const getManagers = (): Map<string, ManagerApi> => managers;
|
2019-07-25 06:17:19 +00:00
|
|
|
|
2021-09-29 19:58:42 +00:00
|
|
|
export async function detectAllGlobalConfig(): Promise<GlobalManagerConfig> {
|
|
|
|
let config: GlobalManagerConfig = {};
|
|
|
|
for (const managerName of managerList) {
|
2022-04-20 06:40:20 +00:00
|
|
|
const manager = managers.get(managerName)!;
|
2021-09-29 19:58:42 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-19 06:02:30 +00:00
|
|
|
export async function extractAllPackageFiles(
|
2019-07-25 06:17:19 +00:00
|
|
|
manager: string,
|
|
|
|
config: ExtractConfig,
|
|
|
|
files: string[]
|
2023-02-20 14:58:49 +00:00
|
|
|
): Promise<PackageFile[] | null> {
|
2020-04-06 17:38:20 +00:00
|
|
|
if (!managers.has(manager)) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-20 06:40:20 +00:00
|
|
|
const m = managers.get(manager)!;
|
2020-04-19 06:02:30 +00:00
|
|
|
if (m.extractAllPackageFiles) {
|
|
|
|
const res = await m.extractAllPackageFiles(config, files);
|
|
|
|
// istanbul ignore if
|
|
|
|
if (!res) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-06-30 03:28:14 +00:00
|
|
|
return res;
|
2020-04-19 06:02:30 +00:00
|
|
|
}
|
|
|
|
return null;
|
2019-07-25 06:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function extractPackageFile(
|
|
|
|
manager: string,
|
|
|
|
content: string,
|
2022-05-24 07:01:43 +00:00
|
|
|
fileName: string,
|
|
|
|
config: ExtractConfig
|
2023-02-19 12:43:48 +00:00
|
|
|
): Result<PackageFileContent | null> {
|
2020-04-06 17:38:20 +00:00
|
|
|
if (!managers.has(manager)) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-20 06:40:20 +00:00
|
|
|
const m = managers.get(manager)!;
|
2020-04-06 17:38:20 +00:00
|
|
|
return m.extractPackageFile
|
|
|
|
? m.extractPackageFile(content, fileName, config)
|
2019-07-25 06:17:19 +00:00
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
2022-04-20 06:40:20 +00:00
|
|
|
export function getRangeStrategy(config: RangeConfig): RangeStrategy | null {
|
2019-07-25 06:17:19 +00:00
|
|
|
const { manager, rangeStrategy } = config;
|
2022-04-20 06:40:20 +00:00
|
|
|
if (!manager || !managers.has(manager)) {
|
2020-04-06 17:38:20 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-04-20 06:40:20 +00:00
|
|
|
const m = managers.get(manager)!;
|
2020-04-06 17:38:20 +00:00
|
|
|
if (m.getRangeStrategy) {
|
2019-07-25 06:17:19 +00:00
|
|
|
// Use manager's own function if it exists
|
2022-01-30 03:46:22 +00:00
|
|
|
const managerRangeStrategy = m.getRangeStrategy(config);
|
|
|
|
if (managerRangeStrategy === 'in-range-only') {
|
|
|
|
return 'update-lockfile';
|
|
|
|
}
|
|
|
|
return managerRangeStrategy;
|
2019-07-25 06:17:19 +00:00
|
|
|
}
|
|
|
|
if (rangeStrategy === 'auto') {
|
|
|
|
// default to 'replace' for auto
|
|
|
|
return 'replace';
|
|
|
|
}
|
2022-01-30 03:46:22 +00:00
|
|
|
if (rangeStrategy === 'in-range-only') {
|
|
|
|
return 'update-lockfile';
|
|
|
|
}
|
|
|
|
|
2019-07-25 06:17:19 +00:00
|
|
|
return config.rangeStrategy;
|
|
|
|
}
|