mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-15 09:06:25 +00:00
da6ba6435f
* refactor: strict null checks for util * chore: fix type * Update tsconfig.strict.json * Update lib/util/package-rules.ts * Update lib/util/package-rules.ts * chore: fix test and coverage * chore: fix package rules * refactor(manager): strict null checks * chore: revert config changes
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import { regEx } from '../../../util/regex';
|
|
import { CdnJsDatasource } from '../../datasource/cdnjs';
|
|
import { cloudflareUrlRegex } from '../cdnurl/extract';
|
|
import type { PackageDependency, PackageFile } from '../types';
|
|
|
|
const regex = regEx(/<\s*(script|link)\s+[^>]*?\/?>/i);
|
|
|
|
const integrityRegex = regEx(
|
|
/\s+integrity\s*=\s*("|')(?<currentDigest>[^"']+)/
|
|
);
|
|
|
|
export function extractDep(tag: string): PackageDependency | null {
|
|
const match = cloudflareUrlRegex.exec(tag);
|
|
if (!match?.groups) {
|
|
return null;
|
|
}
|
|
const { depName, currentValue, asset } = match.groups;
|
|
const dep: PackageDependency = {
|
|
datasource: CdnJsDatasource.id,
|
|
depName,
|
|
packageName: `${depName}/${asset}`,
|
|
currentValue,
|
|
replaceString: tag,
|
|
};
|
|
const integrityMatch = integrityRegex.exec(tag);
|
|
if (integrityMatch?.groups) {
|
|
dep.currentDigest = integrityMatch.groups.currentDigest;
|
|
}
|
|
return dep;
|
|
}
|
|
|
|
export function extractPackageFile(content: string): PackageFile | null {
|
|
const deps: PackageDependency[] = [];
|
|
let rest = content;
|
|
let match = regex.exec(rest);
|
|
let offset = 0;
|
|
while (match) {
|
|
const [replaceString] = match;
|
|
offset += match.index + replaceString.length;
|
|
rest = content.slice(offset);
|
|
match = regex.exec(rest);
|
|
const dep = extractDep(replaceString);
|
|
if (dep) {
|
|
deps.push(dep);
|
|
}
|
|
}
|
|
if (!deps.length) {
|
|
return null;
|
|
}
|
|
return { deps };
|
|
}
|