<commit_before>'use strict';

const exec = require('child_process').execFile;
const path = require('path');

var actionsQueue = Promise.resolve();

function takeAction(fn) {
  const result = actionsQueue.then(fn);
  actionsQueue = result.catch(function () {});

  return result;
}

const DOORMAN_EXEC = path.resolve(__dirname, '../../bin/doorman');

function doorman(args, options) {
  return new Promise(function (resolve, reject) {
    exec(DOORMAN_EXEC, args || [], options || {}, function (error, stdout, stderr) {
      if (error) {
        console.error(String(stderr));
        console.error(error);
        return reject(error);
      }

      console.log(String(stdout));
      console.error(String(stderr));
      resolve();
    });
  });
}

exports.play = function playSong(channel, song) {
  return takeAction(function () {
    console.log(`Playing song ${song} on channel ${channel}`);
    return doorman([ 'write', channel, song ]);
  });
}<commit_msg>use spawn instead of execFile<commit_after>'use strict';

const spawn = require('child_process').spawn;
const path = require('path');

var actionsQueue = Promise.resolve();

function takeAction(fn) {
  const result = actionsQueue.then(fn);
  actionsQueue = result.catch(function () {});

  return result;
}

const DOORMAN_EXEC = path.resolve(__dirname, '../../bin/doorman');

function doorman(args) {
  if (arguments.length > 1 && !Array.isArray(args)) {
    args = Array.prototype.slice.call(args);
  }

  return new Promise(function (resolve, reject) {
    const child = spawn(DOORMAN_EXEC, args || [], {
      // detached: true,
      stdio: [ 'ignore', 1, 2 ]
    });

    child.on('close', function (code) {
      console.log(JSON.stringify(code));
      resolve();
    });
  });
}

exports.play = function playSong(channel, song) {
  return takeAction(function () {
    console.log(`Playing song ${song} on channel ${channel}`);
    return doorman('write', channel, song);
  });
}
{'fraction_non_alphanumeric': 0.16598360655737704, 'fraction_numerical': 0.0015368852459016393, 'mean_word_length': 3.7288135593220337, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '5068301', 'n_tokens_mistral': 607, 'n_tokens_neox': 588, 'n_words': 137}