<commit_before>/*!
 * base-cli <https://github.com/jonschlinkert/base-cli>
 *
 * Copyright (c) 2015, <NAME>.
 * Licensed under the MIT License.
 */

'use strict';

var path = require('path');
var config = require('map-config');

module.exports = function(args) {
  return function(app) {
    var cli = config(app)
      .alias('show', 'get')
      .alias('options', 'option')
      .map('store', store(app.store))
      .map('data')
      .map('option')
      .map('enable')
      .map('enabled')
      .map('disable')
      .map('disabled')
      .map('define')
      .map('set')
      .map('del')
      .map('cwd', function(fp) {
        app.set('cwd', fp);
      })
      .map('has', function(prop) {
        arrayify(prop).forEach(function (key) {
          app.get(key);
        });
      })
      .map('get', function(prop) {
        arrayify(prop).forEach(function (key) {
          app.get(key);
        });
      })
      .map('use', function(names) {
        arrayify(names).forEach(function (name) {
          app.use(tryRequire(name, app));
        });
      });

    app.define('cli', proxy(cli));

    app.cli.process = function (val) {
      args = arrayify(args);
      if (val) args = args.concat(val);
      args.forEach(function(arg) {
        cli.process(arg);
      });
    };
  };

  function store(app) {
    if (!app) return {};
    var cli = config(app)
      .alias('show', 'get')
      .map('set')
      .map('del')
      .map('has')
      .map('hasOwn')
      .map('get');

    app.define('cli', proxy(cli));
    return function(argv) {
      cli.process(argv);
    };
  }
};

/**
 * Proxy to support `app.cli` as a function or object
 * with methods, allowing the user to do either of
 * the following:
 *
 * ```js
 * base.cli({foo: 'bar'});
 *
 * // or
 * base.cli.map('foo', 'bar');
 * ```
 */

function proxy(config) {
  function fn(key, val) {
    if (typeof val === 'string') {
      config.alias.apply(config, arguments);
      return config;
    }

    if (typeof key === 'string') {
      config.map.apply(config, arguments);
      return config;
    }

    if (!isObject(key)) {
      throw new TypeError('expected key to be a string or object');
    }

    for (var prop in key) {
      fn(prop, key[prop]);
    }
    return config;
  }
  fn.__proto__ = config;
  return fn;
}

function arrayify(val) {
  if (typeof val === 'string') {
    return val.split(',');
  }
  return Array.isArray(val) ? val : [val];
}

function tryRequire(name, app) {
  try {
    return require(name);
  } catch(err) {}

  try {
    var cwd = app.get('cwd') || process.cwd();
    return require(path.resolve(cwd, name));
  } catch(err) {}

  return function() {
    console.log('cannot find plugin:', name);
  };
}<commit_msg>ensure proper cwd handling<commit_after>/*!
 * base-cli <https://github.com/jonschlinkert/base-cli>
 *
 * Copyright (c) 2015, <NAME>.
 * Licensed under the MIT License.
 */

'use strict';

var path = require('path');
var config = require('map-config');
var isObject = require('isobject');

module.exports = function(args) {
  return function(app) {
    var cli = config(app)
      .alias('show', 'get')
      .alias('options', 'option')
      .map('option')
      .map('data')
      .map('store', store(app.store))
      .map('enable')
      .map('enabled')
      .map('disable')
      .map('disabled')
      .map('define')
      .map('set')
      .map('del')
      .map('cwd', function(fp) {
        app.set('cwd', fp);
      })
      .map('has', function(prop) {
        arrayify(prop).forEach(function (key) {
          app.has(key);
        });
      })
      .map('get', function(prop) {
        arrayify(prop).forEach(function (key) {
          app.get(key);
        });
      })
      .map('use', function(names) {
        arrayify(names).forEach(function (name) {
          var cwd = app.get('cwd') || process.cwd();
          app.use(tryRequire(name, cwd));
          app.emit('use', name);
        });
      });

    app.define('cli', proxy(cli));

    app.cli.process = function (val) {
      args = arrayify(args);
      if (val) args = args.concat(val);
      args.forEach(function(arg) {
        cli.process(arg);
      });
    };
  };

  function store(app) {
    if (!app) return {};
    var cli = config(app)
      .alias('show', 'get')
      .map('set')
      .map('del')
      .map('has')
      .map('hasOwn')
      .map('get');

    app.define('cli', proxy(cli));
    return function(argv) {
      cli.process(argv);
    };
  }
};

/**
 * Proxy to support `app.cli` as a function or object
 * with methods, allowing the user to do either of
 * the following:
 *
 * ```js
 * base.cli({foo: 'bar'});
 *
 * // or
 * base.cli.map('foo', 'bar');
 * ```
 */

function proxy(config) {
  function fn(key, val) {
    if (typeof val === 'string') {
      config.alias.apply(config, arguments);
      return config;
    }

    if (typeof key === 'string') {
      config.map.apply(config, arguments);
      return config;
    }

    if (!isObject(key)) {
      throw new TypeError('expected key to be a string or object');
    }

    for (var prop in key) {
      fn(prop, key[prop]);
    }
    return config;
  }
  fn.__proto__ = config;
  return fn;
}

/**
 * Cast the given value to an array
 */

function arrayify(val) {
  if (typeof val === 'string') {
    return val.split(',');
  }
  return Array.isArray(val) ? val : [val];
}

/**
 * Try to require the given module
 * or file path.
 */

function tryRequire(name, cwd) {
  try {
    return require(name);
  } catch(err) {}

  try {
    return require(path.resolve(cwd, name));
  } catch(err) {}

  throw new Error('cannot find plugin: ' + name);
}
{'fraction_non_alphanumeric': 0.1886158741382358, 'fraction_numerical': 0.0014141771256849921, 'mean_word_length': 2.2914485165794067, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '1228933', 'n_tokens_mistral': 1928, 'n_tokens_neox': 1900, 'n_words': 426}