/
util.ts
69 lines (62 loc) · 1.94 KB
/
util.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
import is from '@sindresorhus/is';
import { regEx } from '../../../util/regex';
import { TerraformProviderDatasource } from '../../datasource/terraform-provider';
import type { PackageDependency } from '../types';
import type { ProviderLock } from './lockfile/types';
import { extractLocks, findLockFile, readLockFile } from './lockfile/util';
export function checkFileContainsDependency(
content: string,
checkList: string[]
): boolean {
return checkList.some((check) => content.includes(check));
}
const pathStringRegex = regEx(/(.|..)?(\/[^/])+/);
export function checkIfStringIsPath(path: string): boolean {
const match = pathStringRegex.exec(path);
return !!match;
}
export function massageProviderLookupName(dep: PackageDependency): void {
if (!dep.packageName) {
dep.packageName = dep.depName;
}
// TODO #7154
if (!dep.packageName!.includes('/')) {
dep.packageName = `hashicorp/${dep.packageName!}`;
}
// handle cases like `Telmate/proxmox`
// TODO #7154
dep.packageName = dep.packageName!.toLowerCase();
}
export function getLockedVersion(
dep: PackageDependency,
locks: ProviderLock[]
): string | undefined {
const depRegistryUrl = dep.registryUrls
? dep.registryUrls[0]
: TerraformProviderDatasource.defaultRegistryUrls[0];
const foundLock = locks.find(
(lock) =>
lock.packageName === dep.packageName &&
lock.registryUrl === depRegistryUrl
);
if (foundLock) {
return foundLock.version;
}
return undefined;
}
export async function extractLocksForPackageFile(
fileName: string
): Promise<ProviderLock[]> {
const locks: ProviderLock[] = [];
const lockFilePath = await findLockFile(fileName);
if (lockFilePath) {
const lockFileContent = await readLockFile(lockFilePath);
if (lockFileContent) {
const extractedLocks = extractLocks(lockFileContent);
if (is.nonEmptyArray(extractedLocks)) {
locks.push(...extractedLocks);
}
}
}
return locks;
}