Fix JSON output corruption from API log noise
- Redirect console.log to stderr before importing @actual-app/api so breadcrumb/sync messages don't mix with JSON output - Use groupId (not cloudFileId) for downloadBudget sync ID - Fix setup.sh to display and select groupId
This commit is contained in:
@@ -1,9 +1,18 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
// Redirect console.log to stderr so API noise doesn't corrupt JSON output
|
||||
const _origLog = console.log;
|
||||
console.log = (...args) => console.error(...args);
|
||||
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as api from '@actual-app/api';
|
||||
|
||||
// Restore console.log only for our own output function
|
||||
function output(data) {
|
||||
_origLog(JSON.stringify(data, null, 2));
|
||||
}
|
||||
|
||||
const CONFIG_DIR = path.join(process.env.HOME, '.config', 'actual-budget');
|
||||
const CONFIG_FILE = path.join(CONFIG_DIR, 'config');
|
||||
const PASSWORD_FILE = path.join(CONFIG_DIR, 'password');
|
||||
@@ -53,10 +62,6 @@ async function initialize(config) {
|
||||
await api.downloadBudget(config.ACTUAL_SYNC_ID);
|
||||
}
|
||||
|
||||
function output(data) {
|
||||
console.log(JSON.stringify(data, null, 2));
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const [command, ...args] = process.argv.slice(2);
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ npm install @actual-app/api --silent 2>&1 | tail -1
|
||||
# List available budgets and let user pick
|
||||
echo ""
|
||||
echo "Connecting to server and listing budgets..."
|
||||
budgets_json=$(node "${SCRIPT_DIR}/actual-query.mjs" budgets 2>&1) || {
|
||||
budgets_json=$(node "${SCRIPT_DIR}/actual-query.mjs" budgets 2>/dev/null) || {
|
||||
echo "ERROR: Failed to connect to server"
|
||||
echo "${budgets_json}"
|
||||
exit 1
|
||||
@@ -62,7 +62,7 @@ echo "Available budgets:"
|
||||
echo "${budgets_json}" | node -e "
|
||||
const data = JSON.parse(require('fs').readFileSync('/dev/stdin', 'utf-8'));
|
||||
if (Array.isArray(data)) {
|
||||
data.forEach((b, i) => console.log(' ' + (i+1) + ') ' + (b.name || b.cloudFileId || b.id || 'unnamed') + ' [' + (b.cloudFileId || b.groupId || b.id || 'unknown') + ']'));
|
||||
data.forEach((b, i) => console.log(' ' + (i+1) + ') ' + (b.name || b.groupId || b.id || 'unnamed') + ' [groupId: ' + (b.groupId || b.id || 'unknown') + ']'));
|
||||
} else {
|
||||
console.log(' (unexpected format)');
|
||||
console.log(JSON.stringify(data, null, 2));
|
||||
@@ -77,7 +77,7 @@ if [[ "${sync_id}" =~ ^[0-9]+$ ]]; then
|
||||
sync_id=$(echo "${budgets_json}" | node -e "
|
||||
const data = JSON.parse(require('fs').readFileSync('/dev/stdin', 'utf-8'));
|
||||
const idx = ${sync_id} - 1;
|
||||
if (data[idx]) console.log(data[idx].cloudFileId || data[idx].groupId || data[idx].id || '');
|
||||
if (data[idx]) console.log(data[idx].groupId || data[idx].id || '');
|
||||
else console.log('');
|
||||
")
|
||||
fi
|
||||
@@ -92,7 +92,7 @@ echo "ACTUAL_SYNC_ID=\"${sync_id}\"" >> "${CONFIG_FILE}"
|
||||
|
||||
echo ""
|
||||
echo "Testing connection..."
|
||||
test_result=$(node "${SCRIPT_DIR}/actual-query.mjs" accounts 2>&1) || {
|
||||
test_result=$(node "${SCRIPT_DIR}/actual-query.mjs" accounts 2>/dev/null) || {
|
||||
echo "ERROR: Failed to load budget"
|
||||
echo "${test_result}"
|
||||
exit 1
|
||||
|
||||
Reference in New Issue
Block a user