<commit_before>"use strict";

var node = require("when/node"),
    findPort = require("./findPort"),
    childProcess = require("child_process"),
    phantomjs = require("phantomjs"),
    config = require("./config.js"),
    when = require("when"),
    fs = require("fs"),
    net = require("net"),
    temp = require("temp"),
    path = require("path"),
    Phantom = require("./Phantom.js");

var startScript = path.resolve(__dirname, "./phantom/start.js"),
    writeFile,
    open;

open = node.lift(temp.open);
writeFile = node.lift(fs.writeFile);

/**
 * Spawns a new phantomjs process with the given phantom config. Returns a Promises/A+ compliant promise
 * which resolves when the process is ready to execute commands.
 *
 * @see http://phantomjs.org/api/command-line.html
 * @param {Object} phantomJsConfig
 * @returns {Promise}
 */
function spawn(phantomJsConfig) {
    var configPath,
        stdout,
        stderr,
        child;

    phantomJsConfig = phantomJsConfig || {};
    phantomJsConfig.phridge = phantomJsConfig.phridge || {};
    phantomJsConfig.phridge.secret = Math.random().toString().substr(2);
    stdout = config.stdout;
    stderr = config.stderr;

    /**
     * Step 1: Find an available port where we'll spin up the phantomjs internal http server.
     */
    return findPort()
    /**
     * Step 2: Open a temp file where the phantomjs config will be written to.
     */
        .then(function openTempFile(p) {
            phantomJsConfig.phridge.port = p;
            return open(null);
        })
    /**
     * Step 3: Write the config.
     */
        .then(function writeConfig(info) {
            configPath = info.path;

            return writeFile(info.path, JSON.stringify(phantomJsConfig));
        })
    /**
     * Step 4: Start phantomjs with the config path and pipe stderr and stdout.
     */
        .then(function startPhantom() {
            child = childProcess.spawn(phantomjs.path, [
                "--config=" + configPath,
                startScript,
                configPath<commit_msg>Close file descriptor<commit_after>"use strict";

var node = require("when/node"),
    findPort = require("./findPort"),
    childProcess = require("child_process"),
    phantomjs = require("phantomjs"),
    config = require("./config.js"),
    when = require("when"),
    fs = require("fs"),
    net = require("net"),
    temp = require("temp"),
    path = require("path"),
    Phantom = require("./Phantom.js");

var startScript = path.resolve(__dirname, "./phantom/start.js"),
    writeFile,
    open,
    close;

open = node.lift(temp.open);
close = node.lift(fs.close);
writeFile = node.lift(fs.writeFile);

/**
 * Spawns a new phantomjs process with the given phantom config. Returns a Promises/A+ compliant promise
 * which resolves when the process is ready to execute commands.
 *
 * @see http://phantomjs.org/api/command-line.html
 * @param {Object} phantomJsConfig
 * @returns {Promise}
 */
function spawn(phantomJsConfig) {
    var configPath,
        stdout,
        stderr,
        child;

    phantomJsConfig = phantomJsConfig || {};
    phantomJsConfig.phridge = phantomJsConfig.phridge || {};
    phantomJsConfig.phridge.secret = Math.random().toString().substr(2);
    stdout = config.stdout;
    stderr = config.stderr;

    /**
     * Step 1: Find an available port where we'll spin up the phantomjs internal http server.
     */
    return findPort()
    /**
     * Step 2: Open a temp file where the phantomjs config will be written to.
     */
        .then(function openTempFile(p) {
            phantomJsConfig.phridge.port = p;
            return open(null);
        })
    /**
     * Step 3: Write the config.
     */
        .then(function writeConfig(info) {
            configPath = info.path;

            return writeFile(info.path, JSON.stringify(phantomJsConfig)).then(function(){
                return close(info.fd);
            });
        })
    /**
     * Step 4: Start phantomjs with the config path and pipe stderr and stdout.
     */
        .then(function startPhantom() {
            child = childProcess.spawn(phantomjs.path, [
                "--config=" + configPath,
                startScript,
                configPath
{'fraction_non_alphanumeric': 0.1288183755623964, 'fraction_numerical': 0.0023679848448969927, 'mean_word_length': 2.4566284779050735, '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': '706059', 'n_tokens_mistral': 1282, 'n_tokens_neox': 1173, 'n_words': 314}