renovate/lib/manager/cdnurl/extract.ts
Rhys Arkins 6293edc19a
feat: replace datasource constants with IDs (#5595)
Co-Authored-By: Michael Kriese <michael.kriese@visualon.de>
2020-03-01 08:01:12 +01:00

35 lines
1,020 B
TypeScript

import { PackageFile, PackageDependency } from '../common';
import * as datasourceCdnjs from '../../datasource/cdnjs';
export const cloudflareUrlRegex = /\/\/cdnjs\.cloudflare\.com\/ajax\/libs\/(?<depName>[^/]+?)\/(?<currentValue>[^/]+?)\/(?<asset>[-/_.a-zA-Z0-9]+)/;
export function extractPackageFile(content: string): PackageFile {
const deps: PackageDependency[] = [];
let rest = content;
let match = cloudflareUrlRegex.exec(rest);
let offset = 0;
while (match) {
const [wholeSubstr] = match;
const { depName, currentValue, asset } = match.groups;
const fileReplacePosition =
offset + match.index + wholeSubstr.indexOf(currentValue);
offset += match.index + wholeSubstr.length;
rest = content.slice(offset);
match = cloudflareUrlRegex.exec(rest);
deps.push({
datasource: datasourceCdnjs.id,
depName,
lookupName: `${depName}/${asset}`,
currentValue,
managerData: {
fileReplacePosition,
},
});
}
return { deps };
}