From f46f91f4deebe6948122da945cfea31f3154761c Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Fri, 15 Dec 2023 09:47:19 -0800 Subject: [PATCH] feat: move files after importing using config value --- README.md | 5 +++++ src/scripts/import.ts | 6 ++++++ src/utils/config.ts | 2 ++ 3 files changed, 13 insertions(+) diff --git a/README.md b/README.md index 7cc4e6b..f3786ab 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,10 @@ This tool can be configured using a `.budget-cli.json` file in the root of this "carryover": 125 } } + }, + "moveFilesAfterImport": { + "AccountTranslatorName1": "/path/to/destination/directory/for/TranslatorName1", + "AccountTranslatorName2": "/path/to/destination/directory/for/TranslatorName2", } } ``` @@ -89,6 +93,7 @@ All keys are optional and will provide defaults. - `subCategories`: This can be set to an object with `income` and `expense` as keys. Each of those keys must be set to an array of strings indicating the sub-categories to use for each. When importing transactions, the tool will prompt you to select one. - `expenseTypeMapping`: The keys in the object are expense categories and the values are an expense type of "need" or "want" to automatically map expense categories to types. - `expenseAllowance`: This can be set to an object with keys indicating a specific year. Each year should be set to an object with expense sub-categories as keys. Each sub-category should be set to an object with the keys `allowance`, indicating how much is allowed per month, and `carryover`, indicating any rollover from the previous year (or `0` if none). +- `moveFilesAfterImport`: This can be set to an object with keys indicating an account name and values indicating an absolute path to a local directory. If a destination path is set for a specific account, the CSV file will be moved there after successful import. ## Usage diff --git a/src/scripts/import.ts b/src/scripts/import.ts index c68824e..9ea035b 100644 --- a/src/scripts/import.ts +++ b/src/scripts/import.ts @@ -1,4 +1,5 @@ import path from "path"; +import { renameSync } from "fs"; import { getTranslator } from "../translators/index.js"; import { DB } from "../utils/storage.js"; @@ -183,5 +184,10 @@ export const run = async ( splitRemaining = roundCurrency(splitRemaining - splitAmount); } } + + const destination = config.moveFilesAfterImport[importAccountName]; + if (destination) { + renameSync(currentFile, path.join(destination, csvFile)); + } } }; diff --git a/src/utils/config.ts b/src/utils/config.ts index 42fabad..9139ba8 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -11,6 +11,7 @@ const configPath = path.join(process.cwd(), ".budget-cli.json"); const defaultConfig = { outputFile: "./output/data.csv", expenseTypeMapping: {}, + moveFilesAfterImport: {}, subCategories: { income: ["salary", "reimbursable", "other"], expense: [ @@ -50,6 +51,7 @@ export interface Configuration { subCategories: SubCategories; getOutputFile: (args?: CommandArgs) => string; expenseTypeMapping: { [key: string]: "need" | "want" }; + moveFilesAfterImport: { [key: string]: string }; expenseAllowance?: { [key: string]: Allowance; };