Replace broken windows-1250 package with iconv-lite

This commit is contained in:
2025-04-01 20:31:16 +02:00
parent 8359e159f4
commit 58b2d2c629
7 changed files with 56 additions and 59 deletions

View File

@@ -1,9 +1,11 @@
import Papa from "papaparse";
import fs from "fs";
import { openCsv } from "@/csv";
import iconv from "iconv-lite";
import { createParser } from "@/parser";
import { Actual } from "@/server";
import { Config } from "@/types/config";
export function loadTransactions(file: string, profile: string, server: string, config: Config) {
const profileConfig = config.profiles[profile];
if (!profileConfig) {
@@ -19,33 +21,38 @@ export function loadTransactions(file: string, profile: string, server: string,
const actualServer = new Actual(serverConfig);
const skipped: string[] = [];
openCsv(file)
.on('data', async data => {
const transaction = await parser.parseTransaction(profileConfig, data);
if (transaction === undefined) {
skipped.push(`Skipped ==> ${data.join(" ::: ")}`);
return;
}
actualServer.pushTransaction(transaction);
const handleRow = async (data: string[]) => {
const transaction = await parser.parseTransaction(profileConfig, data);
if (transaction === undefined) {
skipped.push(`Skipped ==> ${data.join(" ::: ")}`);
return;
}
actualServer.pushTransaction(transaction);
};
const handleClose = () => actualServer.submit()
.then(x => {
console.log(`Inserted: ${x.added.length}`);
console.log(`Updated: ${x.updated.length}`);
console.log(`Errors: ${x.errors?.length}`);
console.log(`Skipped: ${skipped.length}`);
const now = new Date();
const date = `${now.getFullYear()}-${(now.getMonth()+1).toString().padStart(2, '0')}-${now.getDate().toString().padStart(2, '0')}`;
const time = `${now.getHours().toString().padStart(2, '0')}-${now.getMinutes().toString().padStart(2, '0')}-${now.getSeconds().toString().padStart(2, '0')}`
const filename = `${serverConfig.data}/import.${profile}.${server}.${date}T${time}.json`.replaceAll(/\/+/g, "/");
const logContent = `${JSON.stringify(x, undefined, 2)}\n\n\n\n${skipped.join("\n")}`
fs.writeFileSync(filename, logContent);
console.log(`Detailed output written to ${filename} file.`);
})
.on('close', () => actualServer.submit()
.then(x => {
console.log(`Inserted: ${x.added.length}`);
console.log(`Updated: ${x.updated.length}`);
console.log(`Errors: ${x.errors?.length}`);
console.log(`Skipped: ${skipped.length}`);
const now = new Date();
const date = `${now.getFullYear()}-${(now.getMonth()+1).toString().padStart(2, '0')}-${now.getDate().toString().padStart(2, '0')}`;
const time = `${now.getHours().toString().padStart(2, '0')}-${now.getMinutes().toString().padStart(2, '0')}-${now.getSeconds().toString().padStart(2, '0')}`
const filename = `${serverConfig.data}/import.${profile}.${server}.${date}T${time}.json`.replaceAll(/\/+/g, "/");
const logContent = `${JSON.stringify(x, undefined, 2)}\n\n\n\n${skipped.join("\n")}`
fs.writeFileSync(filename, logContent);
console.log(`Detailed output written to ${filename} file.`);
})
.catch(x => console.error(x))
);
.catch(x => console.error(x))
fs.createReadStream(file)
.pipe(iconv.decodeStream("win1250"))
.pipe(Papa.parse(Papa.NODE_STREAM_INPUT))
.on('data', handleRow)
.on('close', handleClose);
}