import type { Dependency } from "./manifests-json";
const REQUIREMENT_LINE = /^([A-Za-z0-9][A-Za-z0-9._-]*)(?:\[[^\]]*\])?\s*(.*)$/;
function pep508(spec: string, scope: Dependency["scope"], direct: boolean): Dependency | null {
const clean = spec.split(";")[0].trim();
const match = REQUIREMENT_LINE.exec(clean);
if (!match) return null;
return { name: match[1].toLowerCase(), requirement: match[2].trim() || null, scope, direct };
}
export function parseRequirementsTxt(text: string, scope: Dependency["scope"]): Dependency[] {
return text.split("\n").flatMap((raw) => {
const line = raw.split(" #")[0].trim();
if (!line || line.startsWith("#") || line.startsWith("-") || line.includes("://")) return [];
const dependency = pep508(line, scope, true);
return dependency ? [dependency] : [];
});
}
type TomlTable = { name: string; lines: string[] };
/** Splits TOML into tables by header; enough for dependency sections without a full TOML parser. */
function tomlTables(text: string): TomlTable[] {
const tables: TomlTable[] = [{ name: "", lines: [] }];
for (const raw of text.split("\n")) {
const line = raw.replace(/\s+#.*$/, "").trim();
const header = /^\[\[?([^\]]+)\]\]?$/.exec(line);
if (header) tables.push({ name: header[1].trim(), lines: [] });
else if (line && !line.startsWith("#")) tables[tables.length - 1].lines.push(line);
}
return tables;
}
function tomlString(value: string): string | null {
const match = /^["']([^"']*)["']/.exec(value.trim());
return match ? match[1] : null;
}
function tomlInlineVersion(value: string): string | null {