Skip to content

Commit 0dbd288

Browse files
committed
feat: refactor csv loader
1 parent 10f167e commit 0dbd288

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"lingo.dev": patch
3+
---
4+
5+
refactor csv loader

packages/cli/src/cli/loaders/csv.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,49 @@ export default function createCsvLoader(): ILoader<string, Record<string, string
99
async pull(locale, _input) {
1010
const input = parse(_input, {
1111
columns: true,
12+
skip_empty_lines: true,
1213
});
1314

1415
const result: Record<string, string> = {};
1516

16-
// Convert CSV rows to key-value pairs for the specified locale
1717
_.forEach(input, (row) => {
1818
const key = row.id;
19-
if (key && row[locale]) {
19+
if (key && row[locale] && row[locale].trim() !== "") {
2020
result[key] = row[locale];
2121
}
2222
});
2323

2424
return result;
2525
},
2626
async push(locale, data, originalInput) {
27-
const input = parse(originalInput || "", { columns: true }) as Record<string, any>[];
28-
const columns = Object.keys(input[0] || { id: "" });
27+
const input = parse(originalInput || "", {
28+
columns: true,
29+
skip_empty_lines: true,
30+
}) as Record<string, any>[];
31+
32+
const columns = input.length > 0 ? Object.keys(input[0]) : ["id", locale];
2933

30-
// Update existing rows and collect new keys
3134
const updatedRows = input.map((row) => ({
3235
...row,
3336
[locale]: data[row.id] || row[locale] || "",
3437
}));
3538
const existingKeys = new Set(input.map((row) => row.id));
3639

37-
// Add new keys
3840
Object.entries(data).forEach(([key, value]) => {
3941
if (!existingKeys.has(key)) {
40-
updatedRows.push({
42+
const newRow: Record<string, string> = {
4143
id: key,
42-
...Object.fromEntries(columns.map((column) => [column, column === locale ? value : ""])),
43-
});
44+
...Object.fromEntries(columns.map((column) => [column, ""])),
45+
};
46+
newRow[locale] = value;
47+
updatedRows.push(newRow);
4448
}
4549
});
4650

47-
return stringify(updatedRows, { header: true });
51+
return stringify(updatedRows, {
52+
header: true,
53+
columns,
54+
});
4855
},
4956
});
5057
}

0 commit comments

Comments
 (0)