/
index.ts
70 lines (60 loc) · 1.9 KB
/
index.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
70
import moo from 'moo';
import { LANGUAGE_DOT_NET } from '../../constants/languages';
import { id as datasource } from '../../datasource/nuget';
import { SkipReason } from '../../types';
import { PackageDependency, PackageFile } from '../types';
export const language = LANGUAGE_DOT_NET;
export const defaultConfig = {
fileMatch: ['\\.cake$'],
};
const lexer = moo.states({
main: {
lineComment: { match: /\/\/.*?$/ },
multiLineComment: { match: /\/\*[^]*?\*\//, lineBreaks: true },
dependency: {
match: /^#(?:addin|tool|module)\s+(?:nuget|dotnet):.*$/,
},
dependencyQuoted: {
match: /^#(?:addin|tool|module)\s+"(?:nuget|dotnet):[^"]+"\s*$/,
value: (s: string) => s.trim().slice(1, -1),
},
unknown: { match: /[^]/, lineBreaks: true },
},
});
function parseDependencyLine(line: string): PackageDependency | null {
try {
let url = line.replace(/^[^:]*:/, '');
const isEmptyHost = url.startsWith('?');
url = isEmptyHost ? `http://localhost/${url}` : url;
const { origin: registryUrl, protocol, searchParams } = new URL(url);
const depName = searchParams.get('package');
const currentValue = searchParams.get('version');
const result: PackageDependency = { datasource, depName, currentValue };
if (!isEmptyHost) {
if (protocol.startsWith('http')) {
result.registryUrls = [registryUrl];
} else {
result.skipReason = SkipReason.UnsupportedUrl;
}
}
return result;
} catch (err) {
return null;
}
}
export function extractPackageFile(content: string): PackageFile {
const deps = [];
lexer.reset(content);
let token = lexer.next();
while (token) {
const { type, value } = token;
if (type === 'dependency' || type === 'dependencyQuoted') {
const dep = parseDependencyLine(value);
if (dep) {
deps.push(dep);
}
}
token = lexer.next();
}
return { deps };
}