From eef7719970b1a8743c4a41ae073479a09b143a4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Tue, 1 Apr 2025 14:13:08 +0200 Subject: [PATCH] Implement scaffolding of runner --- src/parser/index.ts | 10 ++++------ src/runner/index.ts | 18 ++++++++++++++++++ src/types/config.ts | 7 ++++++- 3 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 src/runner/index.ts diff --git a/src/parser/index.ts b/src/parser/index.ts index a44e61c..e0f8d16 100644 --- a/src/parser/index.ts +++ b/src/parser/index.ts @@ -10,15 +10,13 @@ const PARSERS: Record>> "pl.ing": PlIng }; -export async function parseTransaction(parserName: string, config: ProfileConfig, data: string[]): Promise { - const Parser = PARSERS[parserName]; +export function createParser(config: ProfileConfig): BaseTransactionParser { + const Parser = PARSERS[config.parser]; if (!Parser) { - throw new Error(`Unknown parser: ${parserName}`); + throw new Error(`Unknown parser: ${config.parser}`); } - const parser = new Parser(parserName); - - return parser.parseTransaction(config, data); + return new Parser(config.parser); } diff --git a/src/runner/index.ts b/src/runner/index.ts new file mode 100644 index 0000000..832f4ca --- /dev/null +++ b/src/runner/index.ts @@ -0,0 +1,18 @@ +import { openCsv } from "@/csv"; +import { createParser } from "@/parser"; +import { Config } from "@/types/config"; + +export function loadTransactions(file: string, profile: string, config: Config) { + const profileConfig = config.profiles[profile]; + + if (!profileConfig) { + throw new Error(`Unknown profile: ${profile}`); + } + + const parser = createParser(profileConfig); + + openCsv(file).on('data', async data => { + const transaction = await parser.parseTransaction(profileConfig, data); + console.log(transaction); + }); +} \ No newline at end of file diff --git a/src/types/config.ts b/src/types/config.ts index d26de2e..0e59a71 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -1,7 +1,12 @@ export type ProfileConfig = { - config?: object; + parser: string; + config?: ParserConfig; }; export type ParserConfig = { +}; + +export type Config = { + profiles: Record; }; \ No newline at end of file