-
Notifications
You must be signed in to change notification settings - Fork 1
/
financisto-to-ledger.js
executable file
·133 lines (112 loc) · 4.16 KB
/
financisto-to-ledger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
"use strict";
const financistoToLedger = require('./lib'),
argparse = require('argparse'),
Promise = require('bluebird'),
fs = Promise.promisifyAll(require('fs')),
mkdirp = Promise.promisifyAll(require('mkdirp')),
path = require('path');
function addBooleanGroup(arg, help) {
let group = parser.addMutuallyExclusiveGroup();
group.addArgument(`--no-${arg}`, {
action: 'storeFalse',
dest: arg,
help: false
});
group.addArgument(`--${arg}`, {
action: 'storeTrue',
dest: arg,
help: help
});
}
const parser = new argparse.ArgumentParser({
version: '0.0.1',
addHelp: true,
formatterClass: argparse.ArgumentDefaultsHelpFormatter,
description: 'Financisto backup to ledger file format converter.'
});
parser.setDefaults(financistoToLedger.defaults);
parser.addArgument('--unknown-expense', {
dest: 'unknownExpense',
help: 'Account for unspecified expenses, (default: "%(defaultValue)s")'
});
parser.addArgument('--unknown-payee', {
dest: 'unknownPayee',
help: 'Default payee where unspecified. (default: "%(defaultValue)s")'
});
parser.addArgument('--account-prefix', {
dest: 'accountPrefix',
help: 'Account prefix to use if the account name is not hierarchical. (default: "%(defaultValue)s")'
});
addBooleanGroup('transactions', "Convert transactions.");
addBooleanGroup('accounts', "Add 'account ...' definitions.");
addBooleanGroup('currencies', "Add 'commodity ...' definitions.");
addBooleanGroup('payees', "Add 'payee ...' definitions.");
addBooleanGroup('pricedb', "Add 'P ...' exchange rates.");
addBooleanGroup('projects', "Add Projects to postings as tags.");
addBooleanGroup('locations', "Add Locations to postings as tags.");
addBooleanGroup('lonlats', "Add longitude and latitude to postings as tags.");
addBooleanGroup('simplify', "Simplify the file where possible to ease readability.");
addBooleanGroup('debug', "Add debug information to postings.");
parser.addArgument('filename', {help: 'Financisto backup file'});
parser.addArgument('output-dir', {
nargs: '?',
help: 'Directory to save files to. Will create prices.db, definitions.ledger and financisto.ledger in the directory.'
});
const args = parser.parseArgs();
financistoToLedger.fromAnyFile(args.filename, args)
.then(resultHandler)
.then(successHandler, errorHandler);
function resultHandler(result) {
const outputDir = args['output-dir'];
return outputDir ? writeToDirectory(result, outputDir) : writeToConsole(result);
}
function writeToDirectory(result, outputDir) {
return mkdirp.mkdirpAsync(outputDir)
.then(() => {
const definitionsPath = path.join(outputDir, "definitions.ledger"),
pricedbPath = path.join(outputDir, "prices.db"),
ledgerPath = path.join(outputDir, "financisto.ledger"),
promises = [];
if (result.definitions) {
promises.push(fs.writeFileAsync(definitionsPath, result.definitions));
}
if (result.pricedb) {
promises.push(fs.writeFileAsync(pricedbPath, result.pricedb));
}
if (result.ledger) {
let ledgerText = '';
if (result.definitions) {
ledgerText += "include definitions.ledger\n\n";
}
if (result.pricedb) {
ledgerText += "include prices.db\n\n";
}
if (result.ledger) {
ledgerText += result.ledger + "\n";
}
promises.push(fs.writeFileAsync(ledgerPath, ledgerText));
}
return Promise.all(promises);
});
}
function writeToConsole(result) {
let ledgerText = '';
if (result.definitions) {
ledgerText += `${result.definitions}\n`;
}
if (result.pricedb) {
ledgerText += `${result.pricedb}\n`;
}
if (result.ledger) {
ledgerText += `${result.ledger}\n`;
}
console.log(ledgerText);
}
function successHandler() {
process.exit(0);
}
function errorHandler(err) {
console.error("Failed to parse file:", err);
process.exit(1);
}