2023-02-19 13:43:48 +01:00
|
|
|
import type { PackageDependency, PackageFileContent } from '../types';
|
2022-08-07 08:12:32 +03:00
|
|
|
import { parse } from './parser';
|
2023-02-11 15:28:52 +03:00
|
|
|
import { extractDepsFromFragment } from './rules';
|
2023-09-20 08:03:58 +01:00
|
|
|
import { dockerRules } from './rules/docker';
|
|
|
|
import { gitRules } from './rules/git';
|
|
|
|
import { goRules } from './rules/go';
|
|
|
|
import { ociRules } from './rules/oci';
|
2022-10-12 07:55:13 +03:00
|
|
|
import type { RecordFragment } from './types';
|
2019-04-05 19:12:38 +03:00
|
|
|
|
2020-08-11 17:25:18 +02:00
|
|
|
export function extractPackageFile(
|
|
|
|
content: string,
|
2023-11-07 12:50:29 -03:00
|
|
|
packageFile: string,
|
2023-02-19 13:43:48 +01:00
|
|
|
): PackageFileContent | null {
|
2022-08-07 08:12:32 +03:00
|
|
|
const deps: PackageDependency[] = [];
|
|
|
|
|
2022-10-12 07:55:13 +03:00
|
|
|
const fragments: RecordFragment[] | null = parse(content, packageFile);
|
|
|
|
if (!fragments) {
|
2018-05-03 18:09:18 +02:00
|
|
|
return null;
|
2017-12-07 09:22:10 +01:00
|
|
|
}
|
2022-06-21 23:16:43 -07:00
|
|
|
|
2022-10-12 07:55:13 +03:00
|
|
|
for (let idx = 0; idx < fragments.length; idx += 1) {
|
|
|
|
const fragment = fragments[idx];
|
2023-02-11 15:28:52 +03:00
|
|
|
for (const dep of extractDepsFromFragment(fragment)) {
|
|
|
|
dep.managerData = { idx };
|
2023-02-12 20:00:28 +03:00
|
|
|
|
2023-09-20 08:03:58 +01:00
|
|
|
// Selectively provide `replaceString` in order to make auto-replace
|
|
|
|
// functionality work correctly.
|
|
|
|
const rules = [...dockerRules, ...ociRules, ...gitRules, ...goRules];
|
2023-02-12 20:00:28 +03:00
|
|
|
const replaceString = fragment.value;
|
2023-09-20 08:03:58 +01:00
|
|
|
if (rules.some((rule) => replaceString.startsWith(rule))) {
|
2023-02-12 20:00:28 +03:00
|
|
|
if (dep.currentValue && dep.currentDigest) {
|
|
|
|
dep.replaceString = replaceString;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 15:28:52 +03:00
|
|
|
deps.push(dep);
|
2017-12-14 20:05:45 +01:00
|
|
|
}
|
2018-05-03 18:09:18 +02:00
|
|
|
}
|
2022-08-07 08:12:32 +03:00
|
|
|
|
|
|
|
return deps.length ? { deps } : null;
|
2017-12-07 09:22:10 +01:00
|
|
|
}
|