All files / src/modules/implementations/cli implementations.js

96.96% Statements 64/66
91.3% Branches 42/46
100% Functions 16/16
96.96% Lines 64/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 6739x 39x 39x 771x 771x 290x 290x 290x 290x 4x 289x     4x 290x 771x 290x 290x 290x 290x 290x 290x 289x 289x 289x 289x 289x 289x 289x 289x 289x 289x 161x 161x 126x 28x 28x 28x 28x 98x 98x 98x 98x 287x 287x 287x 289x 290x 290x 771x 771x 771x 771x 771x 771x 771x 771x 771x 771x 771x 771x 771x 771x 771x 771x 39x  
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
module.exports = ({ commands: { definitions }, core: { readConfiguration }, project, cliFormat, output, external: { currentWorkingDirectory, packageMetadata }, }) => {
    const commands = new Map(definitions.map((definition) => [definition.name, definition]));
    function withJournal(file, operation) {
        if (file?.source === 'stdin')
            return project.withJournalText(file.content, currentWorkingDirectory(), operation);
        if (file?.source === 'file')
            return operation(project.openJournal(file.path));
        const { journalPath } = readConfiguration();
        if (journalPath === undefined) {
            throw new Error('No journal file was provided and no project journal is configured');
        }
        return operation(project.openJournal(journalPath));
    }
    function run(commandName, cliOptions) {
        const command = commands.get(commandName);
        if (command === undefined)
            throw new Error(`Missing command definition: ${commandName}`);
        const apiInputs = new Set(project.apiDefinitions[command.operation].inputs);
        const options = Object.fromEntries(Object.entries(cliOptions).filter(([name, value]) => name !== 'file' && value !== undefined && apiInputs.has(name)));
        withJournal(cliOptions.file, (journal) => {
            const query = journal[command.operation];
            if (typeof query !== 'function')
                throw new Error(`Missing journal operation: ${command.operation}`);
            const result = query
                .call(journal, options);
            const prepared = command.prepareOutput
                ? command.prepareOutput(result, cliOptions, cliFormat)
                : result;
            let formatted;
            if (cliOptions.format === 'json') {
                formatted = cliFormat.formatJson(prepared);
            }
            else if (cliOptions.format === 'csv') {
                if (command.formatCsv === undefined)
                    throw new Error(`Missing CSV formatter: ${commandName}`);
                formatted = command.formatCsv(prepared, cliOptions, cliFormat);
            }
            else {
                const formatData = command.loadFormatData?.(journal);
                formatted = command.formatText(prepared, cliOptions, cliFormat, formatData);
            }
            output.writeOutput(formatted);
            const warnings = cliFormat.formatWarnings(journal.warnings);
            if (warnings !== '')
                output.writeError(warnings);
        });
    }
    const implementations = {
        accounts: (options) => run('accounts', options),
        aggregate: (options) => run('aggregate', options),
        commodities: (options) => run('commodities', options),
        investmentPerformance: (options) => run('investment-performance', options),
        postings: (options) => run('postings', options),
        prices: (options) => run('prices', options),
        print: (options) => run('print', options),
        tags: (options) => run('tags', options),
        totalHistory: (options) => run('total-history', options),
        transactions: (options) => run('transactions', options),
        unrealizedGains: (options) => run('unrealized-gains', options),
        validate: (options) => run('validate', options),
        version: () => output.writeOutput(`${packageMetadata.version}\n`),
    };
    return implementations;
};