<commit_before>'use strict';

// Loads commands and assembles them into a hash.

var RSVP = require('rsvp');
var glob = RSVP.denodeify(require('glob'));
var path = require('path');
var camelize = require('../utilities/string').camelize;

module.exports = loadCommands;
function loadCommands() {
  return glob(__dirname + '/../commands/*.js').then(buildHash);
}

function buildHash(files) {
  return files.reduce(function(commands, file) {
    var command = require(file);

    // Set optional properties and check if required properties are set
    command.name = command.name || path.basename(file, '.js');
    command.key = command.key || camelize(command.name);
    command.aliases = command.aliases || [];
    command.options = command.options || [];
    command.works = command.works || 'insideProject';

    if (!['insideProject', 'outsideProject', 'everywhere'].some(function(works) {
      return command.works === works;
    })) {
      throw new Error('The "' + command.name + '" command\'s works field has to ' +
                      'be either "everywhere", "insideProject" or "outsideProject".');
    }

    command.options.forEach(function(option) {
      if (!option.name || !option.type) {
        throw new Error('The command "' + command.name + '" has an option ' +
                        'without the required type and name fields.');
      }

      if (option.name !== option.name.toLowerCase()) {
        throw new Error('The "' + option.name + '" option\'s name of the "' +
                         command.name + '" command contains a capital letter.');
      }

      option.key = camelize(option.name);
      option.required = option.required || false;
    });

    if (!command.run) {
      throw new Error('Command "' + command.name + '" has no run() defined.');
    }

    if (!command.usageInstructions) {
      throw new Error('Command "' + command.name +
                      '" has no usageInstructions() defined.');
    }

    // Add command to commands hash
    commands[command.key] = command;

    return commands;
  }, {});
}
<commit_msg>Make loadCommands() a bit nicer<commit_after>'use strict';

// Loads commands and assembles them into a hash.

var RSVP = require('rsvp');
var glob = RSVP.denodeify(require('glob'));
var path = require('path');
var camelize = require('../utilities/string').camelize;

module.exports = loadCommands;
function loadCommands() {
  return glob(__dirname + '/../commands/*.js').then(buildHash);
}

function buildHash(files) {
  return files.reduce(function(commands, file) {
    var command = loadCommandFromFile(file);
    commands[command.key] = command;
    return commands;
  }, {});
}

function loadCommandFromFile(file) {
  var command = require(file);
  command.name = command.name || path.basename(file, '.js');
  command.key = command.key || camelize(command.name);
  command.aliases = command.aliases || [];

  // Works Property
  command.works = command.works || 'insideProject';
  if (!['insideProject', 'outsideProject', 'everywhere'].some(function(works) {
    return command.works === works;
  })) {
    throw new Error('The "' + command.name + '" command\'s works field has to ' +
                    'be either "everywhere", "insideProject" or "outsideProject".');
  }

  // Options property
  command.options = command.options || [];
  command.options.forEach(function(option) {
    if (!option.name || !option.type) {
      throw new Error('The command "' + command.name + '" has an option ' +
                      'without the required type and name fields.');
    }

    if (option.name !== option.name.toLowerCase()) {
      throw new Error('The "' + option.name + '" option\'s name of the "' +
                       command.name + '" command contains a capital letter.');
    }

    option.key = camelize(option.name);
    option.required = option.required || false;
  });

  // run() method
  if (!command.run) {
    throw new Error('Command "' + command.name + '" has no run() defined.');
  }

  // usageInstructions() method
  if (!command.usageInstructions) {
    throw new Error('Command "' + command.name +
                    '" has no usageInstructions() defined.');
  }

  return command;
}
{'fraction_non_alphanumeric': 0.15467111534795042, 'fraction_numerical': 0.0, 'mean_word_length': 2.9078212290502794, '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': '3915822', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1173, 'n_words': 310}