-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtoml.ts
More file actions
70 lines (54 loc) · 1.77 KB
/
toml.ts
File metadata and controls
70 lines (54 loc) · 1.77 KB
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 { normalize } from "../../core/normalize";
import { Dependency } from "../../types";
import { splitFirst } from "../strings";
const parseInfo = (str: string) => {
// TODO: this is a naive way of parsing this string, but for now it works, probably
const pieces = str.replace(/^\{/, "").replace(/\}$/, "").split(", ");
const info = { version: "unknown" };
for (const piece of pieces) {
const [key, value] = splitFirst(piece).map((x) => x.trim());
if (key === "version") {
info.version = JSON.parse(value);
}
}
return info;
};
const parseDependency = (line: string, lineNumber: number) => {
const [name, info] = splitFirst(line).map((x) => x.trim());
const version = info.includes("{")
? parseInfo(info).version
: JSON.parse(info);
return {
name: normalize(name),
version: {
toml: version,
},
line: lineNumber,
rawText: line,
};
};
export const findDependencies = (text: string) => {
const lines = text.split("\n");
let lastSection: string = "";
const dependencies: Dependency[] = [];
lines.forEach((line, index) => {
if (line.trim() === "") {
return;
}
if (line.startsWith("[")) {
lastSection = line.replace(/^\[/, "").replace(/\]$/, "");
} else {
const poetryDependenciesRe = new RegExp("^tool\.poetry\..*dependencies$")
if (
poetryDependenciesRe.test(lastSection)
) {
const dep = parseDependency(line, index);
if (dep.name === "python") {
return;
}
dependencies.push(dep);
}
}
});
return dependencies;
};