<commit_before>var crawl = require('./crawl.js');
var Spooky = require('spooky');

module.exports = function (url, targetDir, resolution, imageQuality, imageFormat, showControls, waitTime, maxIndex, callback) {
    var env = process.env;

    // forwarding the path towards the local copy of PhantomJS
    env.PHANTOMJS_EXECUTABLE = require.resolve('casperjs/node_modules/.bin/phantomjs');

    var spookyOpts = {
        child: {
            transport: 'http'
        },
        casper: {
            logLevel: 'debug',
            verbose: true,
            viewportSize: resolution ? resolution : null
        },
        child: {
            command: require.resolve('casperjs/bin/casperjs'),
            spawnOptions: {
                env: env
            }
        }
    };

    var spooky = new Spooky(spookyOpts, function (err) {
        if (err) {
            e = new Error('Failed to initialise SpookyJS');
            e.details = err;
            throw e;
        }

        spooky.start(url);
        testRevealVersion();
        if (!resolution) setResolution()
        configureReveal();
        crawl(spooky, targetDir, imageFormat, imageQuality, waitTime, maxIndex);

        spooky.run(function () {
            this.emit('finished');
            this.exit(0);
        });
    });

    spooky.on('error', function (e, stack) {
        console.error(e);
        if (stack) {
            console.log(stack);
        }
    });

    spooky.on('console', function (line) {
        console.log(line);
    });

    spooky.on('log', function (log) {
        if (log.space === 'remote') {
            console.log(log.message.replace(/ \- .*/, ''));
        }
    });

    spooky.on('finished', function () {
        callback(null);
    });

    var testRevealVersion = function () {
        spooky.then(function () {
            var compatible = this.evaluate(function () {
                return typeof(Reveal.availableFragments) === 'function'
                    && typeof(Reveal.configure) === 'function'
                    && typeof(Reveal.getCurrentSlide) === 'function'
                    && typeof(Reveal.getIndices) === 'function'
                    && typeof(Reveal.isLastSlide) === 'function'
                    && typeof(Reveal.next) === 'function'
                    && typeof(Reveal.nextFragment) === 'function';
            });

            if (!compatible) {
                this.emit('console', 'This URL is invalid or the version of Reveal.js used in the presentation is too old for Revealation.');
                this.die('Fix URL or upgrade Reveal.js', 1);
            }
        });
    }

    var setResolution = function () {
        spooky.then(function () {
            var res = this.evaluate(function () {
                return { width: Reveal.getConfig().width,
                         height: Reveal.getConfig().height };
            });
            this.emit('console', 'Setting viewport resolution to ' + res.width + 'x' + res.height);
            this.viewport(res.width, res.height);
        });
    }

    var configureReveal = function () {
        spooky.thenEvaluate([{
            showControls: showControls
        }, function (showControls) {
            Reveal.configure({ controls: showControls,
                               transition: 'none',
                               rollingLinks: false });
        }]);
    }
}
<commit_msg>npm 3 compatibility<commit_after>var crawl = require('./crawl.js');
var Spooky = require('spooky');
var path = require('path');

module.exports = function (url, targetDir, resolution, imageQuality, imageFormat, showControls, waitTime, maxIndex, callback) {
    var env = process.env;

    // forwarding npm@2 and npm@3 modules paths to help Casper find PhantomJS
    env.PATH = [
      path.join(__dirname, 'node_modules', '.bin'),  // npm@3
      path.join(__dirname, 'node_modules', 'casperjs', '.bin'),  // npm@2
      env.PATH
    ].join(path.delimiter);

    var spookyOpts = {
        child: {
            transport: 'http'
        },
        casper: {
            logLevel: 'debug',
            verbose: true,
            viewportSize: resolution ? resolution : null
        },
        child: {
            command: require.resolve('casperjs/bin/casperjs'),
            spawnOptions: {
                env: env
            }
        }
    };

    var spooky = new Spooky(spookyOpts, function (err) {
        if (err) {
            e = new Error('Failed to initialise SpookyJS');
            e.details = err;
            throw e;
        }

        spooky.start(url);
        testRevealVersion();
        if (!resolution) setResolution()
        configureReveal();
        crawl(spooky, targetDir, imageFormat, imageQuality, waitTime, maxIndex);

        spooky.run(function () {
            this.emit('finished');
            this.exit(0);
        });
    });

    spooky.on('error', function (e, stack) {
        console.error(e);
        if (stack) {
            console.log(stack);
        }
    });

    spooky.on('console', function (line) {
        console.log(line);
    });

    spooky.on('log', function (log) {
        if (log.space === 'remote') {
            console.log(log.message.replace(/ \- .*/, ''));
        }
    });

    spooky.on('finished', function () {
        callback(null);
    });

    var testRevealVersion = function () {
        spooky.then(function () {
            var compatible = this.evaluate(function () {
                return typeof(Reveal.availableFragments) === 'function'
                    && typeof(Reveal.configure) === 'function'
                    && typeof(Reveal.getCurrentSlide) === 'function'
                    && typeof(Reveal.getIndices) === 'function'
                    && typeof(Reveal.isLastSlide) === 'function'
                    && typeof(Reveal.next) === 'function'
                    && typeof(Reveal.nextFragment) === 'function';
            });

            if (!compatible) {
                this.emit('console', 'This URL is invalid or the version of Reveal.js used in the presentation is too old for Revealation.');
                this.die('Fix URL or upgrade Reveal.js', 1);
            }
        });
    }

    var setResolution = function () {
        spooky.then(function () {
            var res = this.evaluate(function () {
                return { width: Reveal.getConfig().width,
                         height: Reveal.getConfig().height };
            });
            this.emit('console', 'Setting viewport resolution to ' + res.width + 'x' + res.height);
            this.viewport(res.width, res.height);
        });
    }

    var configureReveal = function () {
        spooky.thenEvaluate([{
            showControls: showControls
        }, function (showControls) {
            Reveal.configure({ controls: showControls,
                               transition: 'none',
                               rollingLinks: false });
        }]);
    }
}

{'fraction_non_alphanumeric': 0.13499495749891946, 'fraction_numerical': 0.0012966431349949576, 'mean_word_length': 1.696969696969697, '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': '589217', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1910, 'n_words': 401}