|
1 | 1 | export function parseWingetOutput(rawOutput: string): Record<string, string>[] { |
2 | | - const lines = rawOutput.split('\n').map(l => l.trimEnd()) |
3 | | - const separatorIndex = lines.findIndex(line => line.startsWith('---')) |
4 | | - if (separatorIndex === -1 || separatorIndex === 0) { |
| 2 | + const lines = rawOutput.split('\n').map(l => l.replace(/\r/g, '').trim()) |
| 3 | + |
| 4 | + const separatorIndex = lines.findIndex(line => /^-{10,}$/.test(line)) |
| 5 | + if (separatorIndex === -1) { |
5 | 6 | return [] |
6 | 7 | } |
7 | | - const headerLine = lines[separatorIndex - 1] |
| 8 | + |
8 | 9 | const dataLines = lines.slice(separatorIndex + 1) |
9 | | - const columns: { name: string; start: number; end?: number }[] = [] |
10 | | - const wordRegex = /\S+/g |
11 | | - let match: RegExpExecArray | null = wordRegex.exec(headerLine) |
12 | | - while (match !== null) { |
13 | | - columns.push({ |
14 | | - name: match[0].toLowerCase(), |
15 | | - start: match.index, |
16 | | - }) |
17 | | - match = wordRegex.exec(headerLine) |
18 | | - } |
19 | | - for (let i = 0; i < columns.length - 1; i++) { |
20 | | - columns[i].end = columns[i + 1].start |
21 | | - } |
22 | 10 | const results: Record<string, string>[] = [] |
| 11 | + |
23 | 12 | for (const line of dataLines) { |
24 | | - if (!line.trim()) continue |
25 | | - const entry: Record<string, string> = {} |
26 | | - for (const col of columns) { |
27 | | - const text = col.end |
28 | | - ? line.substring(col.start, col.end) |
29 | | - : line.substring(col.start) |
30 | | - entry[col.name] = text.trim() |
| 13 | + const cleanLine = line.replace(/^[^a-zA-Z0-9(]+/, '').trim() |
| 14 | + if (!cleanLine) continue |
| 15 | + |
| 16 | + if ( |
| 17 | + /^\d+\s+upgrades/i.test(cleanLine) || |
| 18 | + cleanLine.includes('upgrades available') |
| 19 | + ) |
| 20 | + continue |
| 21 | + if ( |
| 22 | + cleanLine.startsWith('1 package') || |
| 23 | + cleanLine.includes('cannot be determined') |
| 24 | + ) |
| 25 | + continue |
| 26 | + |
| 27 | + const parts = cleanLine.split(/\s{2,}/) |
| 28 | + |
| 29 | + if (parts.length >= 4) { |
| 30 | + results.push({ |
| 31 | + name: parts[0], |
| 32 | + id: parts[1], |
| 33 | + version: parts[2], |
| 34 | + available: parts[3], |
| 35 | + source: parts[4] || 'winget', |
| 36 | + }) |
31 | 37 | } |
32 | | - results.push(entry) |
33 | 38 | } |
| 39 | + |
34 | 40 | return results |
35 | 41 | } |
0 commit comments