<commit_before>import path = require('path')
import findup = require('find-up')

import { findPlugins } from './findPlugins'
import { Command } from './Command'

export interface CliRegistrar {
  addCommand(command: Command<any>): void
}

class CliRegistrarImpl {
  command: Command

  addCommand(command: Command) {
    this.command = command
  }
}

export function loadPlugins(keyword, { cwd } = { cwd: '.' }) {
  const pluginNames = findPlugins(keyword, cwd)
  const globalPluginNames = findPlugins(keyword, getGlobalPackageFolder())

  let r = activatePlugins(pluginNames, cwd);

  globalPluginNames.forEach(p => {
    if (pluginNames.indexOf(p) !== -1)
      return
    const m = loadModule(p, process.env.PWD!)
    if (isValidPlugin(m))
      r.push(activatePlugin(m))
  })

  return r
}

function getGlobalPackageFolder(): string {
  return path.resolve(findup.sync('node_modules', { cwd: __dirname }), '..')
}

function activatePlugins(pluginNames: string[], cwd: string) {
  return pluginNames<commit_msg>fix: global resolution (#61)<commit_after>import path = require('path')
import findup = require('find-up')

import { findPlugins } from './findPlugins'
import { Command } from './Command'

export interface CliRegistrar {
  addCommand(command: Command<any>): void
}

class CliRegistrarImpl {
  command: Command

  addCommand(command: Command) {
    this.command = command
  }
}

export function loadPlugins(keyword, { cwd } = { cwd: '.' }) {
  const pluginNames = findPlugins(keyword, cwd)
  const globalPluginNames = findPlugins(keyword, getGlobalPackageFolder(__dirname))

  let r = activatePlugins(pluginNames, cwd);

  globalPluginNames.forEach(p => {
    if (pluginNames.indexOf(p) !== -1)
      return
    const m = loadModule(p, process.env.PWD!)
    if (isValidPlugin(m))
      r.push(activatePlugin(m))
  })

  return r
}

function getGlobalPackageFolder(folder): string {
  const indexToFirstNodeModulesFolder = folder.indexOf('node_modules')
  const basePath = indexToFirstNodeModulesFolder === -1 ? folder : folder.slice(0, indexToFirstNodeModulesFolder)
  return path.resolve(findup.sync('node_modules', { cwd: basePath }), '..')
}

function activatePlugins(pluginNames: string[], cwd: string) {
  return pluginNames
{'fraction_non_alphanumeric': 0.12979482604817127, 'fraction_numerical': 0.0026761819803746653, 'mean_word_length': 4.635678391959799, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3469169', 'n_tokens_mistral': 706, 'n_tokens_neox': 699, 'n_words': 158}