<commit_before>'use strict';

const recast = require(`recast`);
const bluebird = require(`bluebird`);
const fs = bluebird.promisifyAll(require(`fs`));
const path = require(`path`);
const builders = recast.types.builders;

function processScript(script) {
    const includeNodes = [];
    const ast = recast.parse(script);

    recast.visit(ast, {
        visitExpressionStatement: function(p) {
            if (
                p.node.expression &&
                p.node.expression.callee &&
                p.node.expression.callee.name === 'include'
            ) {
                includeNodes.push(p);
            }

            this.traverse(p);
        }
    });

    const includePromises = includeNodes.map((p) => {
        const filePath = path.join(path.dirname(process.argv[2]), p.node.expression.arguments[0].value);

        return fs.readFileAsync(filePath, `utf8`).catch((err) => {
            return bluebird.reject(new Error(`Error when reading ${filePath}. Error given: ${err}`));
        }).then((file) => {
            return processScript(file);
        }).then(({ast: subAst, includes: subIncludes}) => {
            p.replace(
                builders.expressionStatement(
                    builders.callExpression(
                        builders.functionExpression(
                            null,
                            [],
                            builders.blockStatement(subAst.program.body),
                            false,
                            false
                        ),
                        []
                    )
                )
            );

            return [filePath, ...subIncludes];
        });
    });

    return bluebird.all(includePromises).reduce((includes = [], subIncludes) => {
        return includes.push(...subIncludes);
    }).then((includes = []) => {
        return {ast, includes};
    });
}



module.exports = {
    run: (script, callback) => {
        const newScript = processScript(script).then(({ast, includes}) => {
            return {
                code: recast.print(ast).code,
                includes
            };
        }).catch((err) => {
            if (typeof callback !== `undefined`) {
                return callback(err);
            }
            return bluebird.reject(err);
        });

        if (typeof callback === `undefined`) {
            return newScript;
        }

        newScript.then(({code, includes}) => {
            callback(null, includes, code)
        })
    },

    runFile: (path, outputPath, callback) => {
        if (typeof outputPath === `function`) {
            callback = options;
            outputPath = undefined;
        }

        const runFilePromise = fs.readFileAsync(path, `utf8`).catch((err) => {
            return bluebird.reject(new Error(`Error when reading ${path}. Error given: ${err}`));
        }).then((script) => {
            return processScript(script);
        }).then(({ast, includes}) => {
            const code = recast.print(ast).code
            if (!outputPath) {
                return bluebird.resolve([includes, code]);
            }

            return bluebird.all([<commit_msg>Remove unneeded catch and move it to the callback-only code<commit_after>'use strict';

const recast = require(`recast`);
const bluebird = require(`bluebird`);
const fs = bluebird.promisifyAll(require(`fs`));
const path = require(`path`);
const builders = recast.types.builders;

function processScript(script) {
    const includeNodes = [];
    const ast = recast.parse(script);

    recast.visit(ast, {
        visitExpressionStatement: function(p) {
            if (
                p.node.expression &&
                p.node.expression.callee &&
                p.node.expression.callee.name === 'include'
            ) {
                includeNodes.push(p);
            }

            this.traverse(p);
        }
    });

    const includePromises = includeNodes.map((p) => {
        const filePath = path.join(path.dirname(process.argv[2]), p.node.expression.arguments[0].value);

        return fs.readFileAsync(filePath, `utf8`).catch((err) => {
            return bluebird.reject(new Error(`Error when reading ${filePath}. Error given: ${err}`));
        }).then((file) => {
            return processScript(file);
        }).then(({ast: subAst, includes: subIncludes}) => {
            p.replace(
                builders.expressionStatement(
                    builders.callExpression(
                        builders.functionExpression(
                            null,
                            [],
                            builders.blockStatement(subAst.program.body),
                            false,
                            false
                        ),
                        []
                    )
                )
            );

            return [filePath, ...subIncludes];
        });
    });

    return bluebird.all(includePromises).reduce((includes = [], subIncludes) => {
        return includes.push(...subIncludes);
    }).then((includes = []) => {
        return {ast, includes};
    });
}



module.exports = {
    run: (script, callback) => {
        const newScript = processScript(script).then(({ast, includes}) => {
            return {
                code: recast.print(ast).code,
                includes
            };
        });

        if (typeof callback === `undefined`) {
            return newScript;
        }

        newScript.then(({code, includes}) => {
            callback(null, includes, code)
        }).catch((err) => {
            callback(err);
        });
    },

    runFile: (path, outputPath, callback) => {
        if (typeof outputPath === `function`) {
            callback = options;
            outputPath = undefined;
        }

        const runFilePromise = fs.readFileAsync(path, `utf8`).catch((err) => {
            return bluebird.reject(new Error(`Error when reading ${path}. Error given: ${err}`));
        }).then((script) => {
            return processScript(script);
        }).then(({ast, includes}) => {
            const code = recast.print(ast).code
            if (!outputPath) {
                return bluebird.resolve([includes, code]);
            }

            return bluebird.all([
{'fraction_non_alphanumeric': 0.13753993610223642, 'fraction_numerical': 0.0012779552715654952, 'mean_word_length': 1.578665568369028, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '2153006', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1591, 'n_words': 305}