<commit_before>var string2stream = require('string-to-stream');
var stream2string = require('stream-to-string');
var fs = require('fs-extra-promise');
var jsDocParse = require('jsdoc-parse');

module.exports = function(input, output) {

    input = input || process.stdin;     // default stdin
    output = output || process.stdout;  // default stdout

    // get input as a stream
    getInputAsStream(input).then(function(inputStream) {

        var parser = jsDocParse();

        // send input into jsdoc-parser
        inputStream.pipe(parser);

        // read jsdoc-parse output as json string
        stream2string(parser).then(function(comments) {

            // convert json string into JSON array of comments
            var jsonComments = JSON.parse(comments);

            // build json schema from comments
            var jsonSchema = buildJsonSchema(jsonComments);

            // if the output is a stream
            if (output.write) {

                // send result to output stream
                string2stream(JSON.stringify(jsonSchema)).pipe(output);
            }
            else {

                // otherwise, write output to disk
                fs.writeJsonAsync(output, jsonSchema, { spaces: 4 }).then(function(err) {
                    if (err) {
                        console.error(err);
                    }
                });
            }

        });
    });
};

/**
 * Returns a file as a stream
 * @param {string} input - path to json/js file containing JSDoc comments
 * @returns {Promise} promise to return a stream if the file exists
 */
function getInputAsStream(input) {

    return new Promise(function(resolve, reject) {

        if (input.write) {

            // already a stream
            resolve(input);
        }
        else {
<commit_msg>Modified constructor to return a promise<commit_after>var string2stream = require('string-to-stream');
var stream2string = require('stream-to-string');
var fs = require('fs-extra-promise');
var jsDocParse = require('jsdoc-parse');

module.exports = function(input, output) {

    input = input || process.stdin;     // default stdin
    output = output || process.stdout;  // default stdout

    // get input as a stream
    return getInputAsStream(input).then(function(inputStream) {

        var parser = jsDocParse();

        // send input into jsdoc-parser
        inputStream.pipe(parser);

        // read jsdoc-parse output as json string
        return stream2string(parser).then(function(comments) {

            // convert json string into JSON array of comments
            var jsonComments = JSON.parse(comments);

            // build json schema from comments
            var jsonSchema = buildJsonSchema(jsonComments);

            // if the output is a stream
            if (output.write) {

                // send result to output stream
                string2stream(JSON.stringify(jsonSchema)).pipe(output);
            }
            else {

                // otherwise, write output to disk
                fs.writeJsonAsync(output, jsonSchema, { spaces: 4 }).then(function(err) {
                    if (err) {
                        console.error(err);
                    }
                });
            }

            // if no output object, return promise with JSON object
            return Promise.resolve(jsonSchema);

        });
    });
};

/**
 * Returns a file as a stream
 * @param {string} input - path to json/js file containing JSDoc comments
 * @returns {Promise} promise to return a stream if the file exists
 */
function getInputAsStream(input) {

    return new Promise(function(resolve, reject) {

        if (input.write) {

            // already a stream
            resolve(input);
        }
        else {

{'fraction_non_alphanumeric': 0.10132978723404255, 'fraction_numerical': 0.0026595744680851063, 'mean_word_length': 1.88420245398773, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5295470', 'n_tokens_mistral': 974, 'n_tokens_neox': 922, 'n_words': 284}