<commit_before>var fs = require('fs'),
    Stream = require('stream'),
    path = require('path'),
    LogWriter = require(__dirname + '/../bin/cli_messages.js').LogWriter;

var LmdWriter = function (buildResult) {
    this.source = buildResult;
    this.cwd = process.cwd();
    this.cli = new LogWriter(process.stdout);
};

module.exports = LmdWriter;

/**
 * Changes current work dir
 *
 * @param {String} cwd
 *
 * @return {LmdWriter}
 */
LmdWriter.prototype.relativeTo = function (cwd) {
    this.cwd = cwd;

    return this;
};

/**
 * Changes current log interface
 *
 * @param {LogWriter} cli
 *
 * @return {LmdWriter}
 */
LmdWriter.prototype.logTo = function (cli) {
    if (!(cli instanceof LogWriter)) {
        throw new TypeError('cli should be instance of LogWriter');
    }

    this.cli = cli;

    return this;
};

/**
 * Resolves paths relative to cwd
 *
 * @param {Object} buildConfig
 * @private
 */
LmdWriter.prototype._resolvePaths = function (buildConfig) {
    var cwd = this.cwd,
        configs = path.join(cwd, '.lmd'),
        root = path.join(configs, buildConfig.root || ""),
        sourceMap = buildConfig.sourcemap ? path.join(root, buildConfig.sourcemap) : null,
        source = buildConfig.output ? path.join(root, buildConfig.output) : null;

    return {
        configs: configs,
        root: root,
        sourceMap: sourceMap,
        source: source
    };
};

LmdWriter.prototype._isCanWriteStream = function (stream, fileName) {
    return fileName && stream && stream.readable;
};

LmdWriter.prototype._pipeStreamToFile = function (stream, fileName) {
    if (this._isCanWriteStream(stream, fileName)) {
        stream.pipe(fs.createWriteStream(fileName, {
            flags: "w",
            encoding: "utf8",
            mode: 0666
        }));
    }
};

LmdWriter.prototype._logResult = function (stream, fileName, resourceName) {
    var cli = this.cli;

    if (this._isCanWriteStream(stream, fileName)) {
        stream.on('end', function () {
            cli.ok('Writing ' + resourceName + ' to ' + fileName.green);
        });
    }
};

LmdWriter.prototype._writeStreamsTo = function (stream, paths, resourceName) {
    this._logResult(stream, paths.source, 'LMD ' + resourceName);
    this._pipeStreamToFile(stream, paths.source);

    this._logResult(stream.sourceMap, paths.sourceMap, 'Source Map of ' + resourceName);
    this._pipeStreamToFile(stream.sourceMap, paths.sourceMap);
};

LmdWriter.prototype._onAllEnd = function (callback) {
    var counter = 0,
        bundles = this.source.bundles;

    var complete = function () {
        counter--;
        if (counter <= 0) {
            callback();
        }
    };

    Object.keys(bundles)
        .map(function (bundleName) {
            return bundles[bundleName];
        })
        .concat([this.source, this.source.log])
        .forEach(function (stream) {
            if (!stream.readable) {
                return;
            }

            counter++;
            stream.on('end', complete);
        });
};

/**
 * Writes all build data
 */
LmdWriter.prototype.writeAll = function (callback) {
    var cli = this.cli,<commit_msg>#116 _onAllEnd should callback onNext tick<commit_after>var fs = require('fs'),
    Stream = require('stream'),
    path = require('path'),
    LogWriter = require(__dirname + '/../bin/cli_messages.js').LogWriter;

var LmdWriter = function (buildResult) {
    this.source = buildResult;
    this.cwd = process.cwd();
    this.cli = new LogWriter(process.stdout);
};

module.exports = LmdWriter;

/**
 * Changes current work dir
 *
 * @param {String} cwd
 *
 * @return {LmdWriter}
 */
LmdWriter.prototype.relativeTo = function (cwd) {
    this.cwd = cwd;

    return this;
};

/**
 * Changes current log interface
 *
 * @param {LogWriter} cli
 *
 * @return {LmdWriter}
 */
LmdWriter.prototype.logTo = function (cli) {
    if (!(cli instanceof LogWriter)) {
        throw new TypeError('cli should be instance of LogWriter');
    }

    this.cli = cli;

    return this;
};

/**
 * Resolves paths relative to cwd
 *
 * @param {Object} buildConfig
 * @private
 */
LmdWriter.prototype._resolvePaths = function (buildConfig) {
    var cwd = this.cwd,
        configs = path.join(cwd, '.lmd'),
        root = path.join(configs, buildConfig.root || ""),
        sourceMap = buildConfig.sourcemap ? path.join(root, buildConfig.sourcemap) : null,
        source = buildConfig.output ? path.join(root, buildConfig.output) : null;

    return {
        configs: configs,
        root: root,
        sourceMap: sourceMap,
        source: source
    };
};

LmdWriter.prototype._isCanWriteStream = function (stream, fileName) {
    return fileName && stream && stream.readable;
};

LmdWriter.prototype._pipeStreamToFile = function (stream, fileName) {
    if (this._isCanWriteStream(stream, fileName)) {
        stream.pipe(fs.createWriteStream(fileName, {
            flags: "w",
            encoding: "utf8",
            mode: 0666
        }));
    }
};

LmdWriter.prototype._logResult = function (stream, fileName, resourceName) {
    var cli = this.cli;

    if (this._isCanWriteStream(stream, fileName)) {
        stream.on('end', function () {
            cli.ok('Writing ' + resourceName + ' to ' + fileName.green);
        });
    }
};

LmdWriter.prototype._writeStreamsTo = function (stream, paths, resourceName) {
    this._logResult(stream, paths.source, 'LMD ' + resourceName);
    this._pipeStreamToFile(stream, paths.source);

    this._logResult(stream.sourceMap, paths.sourceMap, 'Source Map of ' + resourceName);
    this._pipeStreamToFile(stream.sourceMap, paths.sourceMap);
};

LmdWriter.prototype._onAllEnd = function (callback) {
    var counter = 0,
        bundles = this.source.bundles;

    var complete = function () {
        counter--;
        if (counter <= 0) {
            process.nextTick(callback);
        }
    };

    Object.keys(bundles)
        .map(function (bundleName) {
            return bundles[bundleName];
        })
        .concat([this.source, this.source.log])
        .forEach(function (stream) {
            if (!stream.readable) {
                return;
            }

            counter++;
            stream.on('end', complete);
        });
};

/**
 * Writes all build data
 */
LmdWriter.prototype.writeAll = function (callback) {
    var cli = this.cli,
{'fraction_non_alphanumeric': 0.13702945345723735, 'fraction_numerical': 0.0026775870215782014, 'mean_word_length': 2.6059057353776263, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3662524', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1927, 'n_words': 416}