<commit_before>import { SolidarityRunContext, SolidaritySettings } from '../../types'
import * as JSON5 from 'json5'
import * as path from 'path'

export const isURI = (path) => !!path.match(/\w+:(\/?\/?)[^\s]+/)

export const loadFile = (context, filePath) => {
  const { filesystem } = context
  if (filesystem.exists(filePath)) {
    return JSON5.parse(filesystem.read('.solidarity'))
  } else {
    throw 'ERROR: There is no solidarity file at the given path'
  }
}

export const loadModule = (context, moduleName) => {
  const { filesystem } = context
  // We will search that module
  const filePath = path.join('node_modules', moduleName, '.solidarity')

  if (filesystem.exists(filePath)) {
    return JSON5.parse(filesystem.read(filePath))
  } else if (filesystem.exists(filePath + '.json')) {
    return JSON5.parse(filesystem.read(filePath + '.json'))
  } else {
    throw 'ERROR: There is no solidarity file found with the given module'
  }
}

export const loadWebCheck = async (context, checkOption) => {
  const { print, http } = context
  const checkSpinner = print.spin(`Running check on ${checkOption}`)
  const api = http.create({
    baseURL: 'https://api.github.com',
    headers: { Accept: 'application/vnd.github.v3+json' },
  })

  // Load check from web
  const checkURL = isURI(checkOption) ? checkOption : `https:\/\/raw.githubusercontent.com/infinitered/solidarity/master/checks/${checkOption}.json`
  const result = await api.get(checkURL)
  // console.log(result)
  if (result.ok) {
    checkSpinner.succeed(`Found ${checkOption}`)
    // Convert strings to JSON5 objects
    const solidarityData = (typeof result.data === 'string')
      ? JSON5.parse(result.data)
      : result.data
    return solidarityData
  } else {
    checkSpinner.fail(`Unable to find a known check stack for ${checkOption}`)
    print.info(
      `Check https://github.com/infinitered/solidarity/checks for options.`
    )
    throw(`ERROR: Request failed (${result.status} - ${result.problem})`)
  }
}

module.exports = async (context: SolidarityRunContext): Promise<SolidaritySettings> => {
  const { filesystem, parameters } = context
  const options = parameters.options || {} // fix possibly undefined from gluegun
  const demandedFile = options.solidarityFile || options.f
  const demandedModule = options.module || options.m
  const demandedCheck = options.stack || options.s<commit_msg>now uses Solidarity Stacks repo<commit_after>import { SolidarityRunContext, SolidaritySettings } from '../../types'
import * as JSON5 from 'json5'
import * as path from 'path'

export const isURI = (path) => !!path.match(/\w+:(\/?\/?)[^\s]+/)

export const loadFile = (context, filePath) => {
  const { filesystem } = context
  if (filesystem.exists(filePath)) {
    return JSON5.parse(filesystem.read('.solidarity'))
  } else {
    throw 'ERROR: There is no solidarity file at the given path'
  }
}

export const loadModule = (context, moduleName) => {
  const { filesystem } = context
  // We will search that module
  const filePath = path.join('node_modules', moduleName, '.solidarity')

  if (filesystem.exists(filePath)) {
    return JSON5.parse(filesystem.read(filePath))
  } else if (filesystem.exists(filePath + '.json')) {
    return JSON5.parse(filesystem.read(filePath + '.json'))
  } else {
    throw 'ERROR: There is no solidarity file found with the given module'
  }
}

export const loadWebCheck = async (context, checkOption) => {
  const { print, http } = context
  const checkSpinner = print.spin(`Running check on ${checkOption}`)
  const api = http.create({
    baseURL: 'https://api.github.com',
    headers: { Accept: 'application/vnd.github.v3+json' },
  })

  // Load check from web
  const checkURL = isURI(checkOption) ? checkOption : `https:\/\/raw.githubusercontent.com/infinitered/solidarity-stacks/master/stacks/${checkOption}.solidarity`
  const result = await api.get(checkURL)
  // console.log(result)
  if (result.ok) {
    checkSpinner.succeed(`Found Stack: ${checkOption}`)
    // Convert strings to JSON5 objects
    const solidarityData = (typeof result.data === 'string')
      ? JSON5.parse(result.data)
      : result.data
    return solidarityData
  } else {
    checkSpinner.fail(`Unable to find a known check stack for ${checkOption}`)
    print.info(
      `Check https://github.com/infinitered/solidarity-stacks for options.`
    )
    throw(`ERROR: Request failed (${result.status} - ${result.problem})`)
  }
}

module.exports = async (context: SolidarityRunContext): Promise<SolidaritySettings> => {
  const { filesystem, parameters } = context
  const options = parameters.options || {} // fix possibly undefined from gluegun
  const demandedFile = options.solidarityFile || options.f
  const demandedModule = options.module || options.m
  const demandedCheck = options.stack || options.s
{'fraction_non_alphanumeric': 0.13766019016122363, 'fraction_numerical': 0.0033071517155849523, 'mean_word_length': 3.843843843843844, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 4, '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': '6607463', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1430, 'n_words': 386}