actual-moneymoney
Version:
An importer for syncing MoneyMoney accounts and transactions to Actual.
67 lines (66 loc) • 3.11 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import toml from 'toml';
import { configSchema, getConfigFile } from '../utils/config.js';
import fs from 'fs/promises';
import Logger, { LogLevel } from '../utils/Logger.js';
import { z } from 'zod';
import { EXAMPLE_CONFIG } from '../utils/shared.js';
const handleValidate = (argv) => __awaiter(void 0, void 0, void 0, function* () {
const configPath = yield getConfigFile(argv);
const logLevel = (argv.logLevel || LogLevel.INFO);
const logger = new Logger(logLevel);
logger.info(`Current configuration file: ${configPath}`);
const configFileExists = yield fs
.access(configPath)
.then(() => true)
.catch(() => false);
if (!configFileExists) {
// Create path to file and file itself if it doesn't exist
yield fs.writeFile(configPath, EXAMPLE_CONFIG);
logger.warn('Configuration file not found.');
logger.info(`Created default configuration file at: ${configPath}. Please edit it with your preferred settings.`);
process.exit(0);
}
else {
logger.info('Validating configuration...');
try {
logger.debug(`Reading configuration file...`);
const configContent = yield fs.readFile(configPath, 'utf-8');
logger.debug(`Parsing configuration file...`);
const configData = toml.parse(configContent);
logger.debug(`Parsing configuration schema...`);
configSchema.parse(configData);
}
catch (e) {
if (e instanceof z.ZodError) {
logger.error('Configuration file is invalid:');
for (const error of e.errors) {
logger.error(`Path [${error.path.join('.')}]: ${error.message}`);
}
}
else if (e instanceof Error && e.name === 'SyntaxError') {
const line = 'line' in e ? e.line : -1;
const column = 'column' in e ? e.column : -1;
logger.error(`Failed to parse configuration file: ${e.message} (line ${line}, column ${column})`);
}
else {
logger.error(`An unexpected error occured: ${e}`);
}
process.exit(1);
}
logger.info('Configuration file is valid.');
}
});
export default {
command: 'validate',
describe: 'View information about and validate the current configuration',
handler: handleValidate,
};