import fs from 'fs';
import readline from 'readline';
import { splitStream } from '@simple-libs/stream-utils';
export async function* readRawCommitsFromFiles(files, separator) {
    for (const file of files) {
        try {
            yield* splitStream(fs.createReadStream(file), separator);
        }
        catch (err) {
            console.warn(`Failed to read file ${file}:\n  ${err}`);
        }
    }
}
export async function* readRawCommitsFromLine(separator) {
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
        terminal: true
    });
    let line = '';
    let commit = '';
    for await (line of rl) {
        commit += `${line}\n`;
        if (!commit.includes(separator)) {
            return;
        }
        yield commit;
        commit = '';
    }
}
export function readRawCommitsFromStdin(separator) {
    return splitStream(process.stdin, separator);
}
const JSON_STREAM_OPEN = '[\n';
const JSON_STREAM_SEPARATOR = '\n,\n';
const JSON_STREAM_CLOSE = '\n]\n';
export async function* stringify(commits) {
    let jsonStreamOpened = false;
    yield JSON_STREAM_OPEN;
    for await (const commit of commits) {
        if (jsonStreamOpened) {
            yield JSON_STREAM_SEPARATOR;
        }
        yield JSON.stringify(commit);
        jsonStreamOpened = true;
    }
    yield JSON_STREAM_CLOSE;
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvY2xpL3V0aWxzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxNQUFNLElBQUksQ0FBQTtBQUNuQixPQUFPLFFBQVEsTUFBTSxVQUFVLENBQUE7QUFDL0IsT0FBTyxFQUFFLFdBQVcsRUFBRSxNQUFNLDJCQUEyQixDQUFBO0FBRXZELE1BQU0sQ0FBQyxLQUFLLFNBQVMsQ0FBQyxDQUFDLHVCQUF1QixDQUFDLEtBQWUsRUFBRSxTQUFpQjtJQUMvRSxLQUFLLE1BQU0sSUFBSSxJQUFJLEtBQUssRUFBRSxDQUFDO1FBQ3pCLElBQUksQ0FBQztZQUNILEtBQUssQ0F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