Create CLI scaffolding

This commit is contained in:
2025-04-01 14:35:24 +02:00
parent e1e85f4d5b
commit 9fa3b7d775
5 changed files with 61 additions and 2 deletions

8
src/cli/config.ts Normal file
View File

@@ -0,0 +1,8 @@
import fs from "fs";
import yaml from "yaml";
import { Config } from "@/types/config";
export function loadConfig(file: string): Config {
const text = fs.readFileSync(file, 'utf-8');
return yaml.parse(text) as Config;
}

45
src/cli/index.ts Normal file
View File

@@ -0,0 +1,45 @@
import { program } from "commander";
import { CLIOptions } from "@/types/cli";
import { loadConfig } from "./config";
import { loadTransactions } from "@/runner";
export function run(...args: string[]) {
program
.name("actual-importer")
.version("0.0.1")
.requiredOption("-c, --config <file>", "sets the path to the YAML file with configuration")
.option("-p, --profile <name>", "sets the desired profile to invoke")
.option("-x, --set <arg>", "overrides the config option for this specific run (arg: <key>=<name>, i.e. profiles.myprofile.parser=pl.ing", (v: string, prev: string[]) => prev.concat([v]), [])
.argument('<csv-file>', 'CSV file to be imported')
.action(handle)
.parse(args);
}
function handle(file: string, options: CLIOptions) {
const config = loadConfig(options.config);
for (const override of options.set) {
const [path, value] = override.split("=");
const segments = path.trim().split(".")
let current: any = config;
segments.map(s => s.trim()).forEach((segment: string, idx) => {
if(!current[segment]) {
current[segment] = {};
}
if(idx === segments.length - 1) {
current[segment] = JSON.parse(value.trim());
}
current = current[segment];
});
}
if (Object.keys(config?.profiles ?? {}).length === 0) {
throw new Error(`No profiles defined in the ${options.config}`);
}
loadTransactions(file, options.profile ?? config.defaultProfile ?? Object.keys(config.profiles)[0], config);
}

View File

@@ -1,3 +1,3 @@
#!/usr/bin/env node import { run } from "./cli";
console.log("Hello, world!"); run(...process.argv);

5
src/types/cli.ts Normal file
View File

@@ -0,0 +1,5 @@
export type CLIOptions = {
config: string;
profile?: string;
set: string[];
};

View File

@@ -8,5 +8,6 @@ export type ParserConfig = {
}; };
export type Config = { export type Config = {
defaultProfile?: string;
profiles: Record<string, ProfileConfig>; profiles: Record<string, ProfileConfig>;
}; };