mt940-mbank-ts/src/cli.ts

58 lines
1.4 KiB
TypeScript

import { promises as fs } from "fs";
import iconv from "iconv-lite";
import yargs from "yargs";
import { convert } from ".";
const path = process.argv.at(-1);
if (!path) {
console.error("Podaj ścieżkę do pliku jako argument w CLI");
process.exit();
}
yargs
.scriptName("mbank-mt940")
.usage("$0 <cmd> [args]")
.command(
"convert",
"onvert from csv to mt940",
(yargs) => {
yargs.option("i", {
alias: "input",
type: "string",
describe: "path to the mbank csv file",
demandOption: false,
});
yargs.option("o", {
alias: "output",
type: "string",
describe: "where to save the mt940 file",
demandOption: false,
});
},
async function (argv) {
let content: Buffer;
const has_path = argv.input && argv.input !== "--";
if (has_path) {
console.error("Reading from ", argv.input);
content = await fs.readFile(argv.input as string, {
encoding: null,
});
} else {
console.error("reading CSV from stdin...");
content = await new Promise((resolve, reject) => {
let chunks: Buffer[] = [];
process.stdin.on("data", (chunk) => {
chunks.push(chunk);
});
process.stdin.on("end", () => {
const buffer = Buffer.concat(chunks);
resolve(buffer);
});
});
}
const result = convert(iconv.decode(content, "cp1250"));
console.log(result);
}
)
.help().argv;