<commit_before>// Requires
var Q = require('q');
var _ = require('lodash');

var path = require('path');

var utils = require('../utils');


function ProjectRunner(events, workspace, shells, run_ports, project, urlPattern) {
    // Id for port allocation and stuff
    this.id = 'project';

    this.events = events;
    this.workspace = workspace;

    this.shells = shells;

    this.run_ports = run_ports;
    this.project = project;

    this.urlPattern = urlPattern;

    // Map
    // runnerId => shellId
    this.projects = {
        //'php:run': 'project-php-2000-xyz'
    };

    // Self incrimenting unique id
    this.projectCount = 0;

    _.bindAll(this);
}

ProjectRunner.prototype.shellId = function(runnerId, port) {
    return [this.id, runnerId, port, this.projectCount++].join('-');
};

ProjectRunner.prototype.portId = function(runnerId) {
    return [this.id, runnerId].join('-');
};

ProjectRunner.prototype.runScript = function(runner, port) {
    var self = this;

    // Id of our shell
    var shellId = this.shellId(runner.id, port);

    // Spawn the new shell
    return this.shells.createShellCommand(
        shellId, [
            // Script itself
            runner.script,

            // Path to project folder
            this.workspace.root,

            // Port allocated/claimed
            port
        ], {
        cwd: this.workspace.root,
        env: _.defaults({
            PORT: port,
            HTTP_PORT: port
        }, process.env)
    }).then(function(shell) {
        // Id of our harbor port (to release)
        var portId = self.portId(runner.id);

        // Register project
        self.projects[runner.id] = shellId;
<commit_msg>Improve robustness of runner scripts<commit_after>// Requires
var Q = require('q');
var _ = require('lodash');

var path = require('path');

var utils = require('../utils');


function ProjectRunner(events, workspace, shells, run_ports, project, urlPattern) {
    // Id for port allocation and stuff
    this.id = 'project';

    this.events = events;
    this.workspace = workspace;

    this.shells = shells;

    this.run_ports = run_ports;
    this.project = project;

    this.urlPattern = urlPattern;

    // Map
    // runnerId => shellId
    this.projects = {
        //'php:run': 'project-php-2000-xyz'
    };

    // Self incrimenting unique id
    this.projectCount = 0;

    _.bindAll(this);
}

ProjectRunner.prototype.shellId = function(runnerId, port) {
    return [this.id, runnerId, port, this.projectCount++].join('-');
};

ProjectRunner.prototype.portId = function(runnerId) {
    return [this.id, runnerId].join('-');
};

ProjectRunner.prototype.runScript = function(runner, port) {
    var self = this;

    // Id of our shell
    var shellId = this.shellId(runner.id, port);

    // Spawn the new shell
    return this.shells.createShellCommand(
        shellId, [
            '/bin/bash',

            // Script itself
            runner.script,

            // Path to project folder
            this.workspace.root,

            // Port allocated/claimed
            port
        ], {
        cwd: this.workspace.root,
        env: _.defaults({
            PORT: port,
            HTTP_PORT: port
        }, process.env)
    }).then(function(shell) {
        // Id of our harbor port (to release)
        var portId = self.portId(runner.id);

        // Register project
        self.projects[runner.id] = shellId;

{'fraction_non_alphanumeric': 0.13035870516185477, 'fraction_numerical': 0.0029163021289005542, 'mean_word_length': 2.1847725162488394, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '642170', 'n_tokens_mistral': 1057, 'n_tokens_neox': 1010, 'n_words': 249}