<commit_before>/* @flow */

import Path from 'path'
import uniqueArray from 'lodash.uniq'
import { spawn, spawnSync } from 'child_process'

const SPAWN_TIMEOUT = 4000
const DEFAULT_PATHS = ['/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin']
export const KNOWN_SHELLS = ['zsh', 'bash', 'fish']
export const CACHE_KEY = '__STEELBRAIN_CONSISTENT_ENV_V1'
export const assign = Object.assign || function (target, source) {
  for (const key in source) {
    if ({}.hasOwnProperty.call(source, key)) {
      target[key] = source[key]
    }
  }
  return target
}

export function identifyEnvironment() {
  const { command, parameters, options } = getCommand()
  options.timeout = SPAWN_TIMEOUT
  return spawnSync(command, parameters, options).stdout.toString().trim().split('\n')
}

export function identifyEnvironmentAsync() {
  return new Promise(function(resolve, reject) {
    const { command, parameters, options } = getCommand()
    const childProcess = spawn(command, parameters, options)
    const stdout = []
    const timer = setTimeout(function() {
      childProcess.kill()
      reject(new Error('Process execution timed out'))
    }, SPAWN_TIMEOUT)
    childProcess.stdout.on('data', function(chunk) {
      stdout.push(chunk)
    })
    childProcess.on('close', function() {
      clearTimeout(timer)
      resolve(stdout.join('').trim().split('\n'))
    })
    childProcess.on('error', function(error) {
      reject(error)
    })
  })
}

export function parse(rawEnvironment: Array<string>): Object {
  const environment = {}
  for (const chunk of rawEnvironment) {
    const index = chunk.indexOf('=')
    if (index !== -1) {
      const key = chunk.slice(0, index).trim()
      const value = chunk.slice(index + 1).trim()
      environment[key] = value
    }
  }
  return environment
}

export function applySugar(environment: Object) {
  let path = process.env.PATH ? process.env.PATH.split(':') : []
  if (environment.PATH) {
    for (const chunk of environment.PATH.split(':')) {
      if (chunk && path.indexOf(chunk) === -1) {
        path.push(chunk)
      }
    }
  }
  for (const entry of DEFAULT_PATHS) {
    if (path.indexOf(entry) === -1) {
      path = [entry].concat(path)
    }
  }
  if (!environment.USER) {
    if (process.env.USER) {
      environment.USER = process.env.USER
    } else if (environment.HOME) {
      environment.USER = Path.basename(environment.HOME)
    }
  }

  environment.PATH = uniqueArray(path).join(':')
  environment.PWD = environment.OLDPWD = process.cwd()
  return environment
}

export function getCommand(): { command: string, options: Object, parameters: Array<string> } {
  const command = process.env.SHELL || 'sh'<commit_msg>:art: Make ESLint happy on helpers.js<commit_after>/* @flow */

import Path from 'path'
import uniqueArray from 'lodash.uniq'
import { spawn, spawnSync } from 'child_process'

const SPAWN_TIMEOUT = 4000
const DEFAULT_PATHS = ['/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin']
export const KNOWN_SHELLS = ['zsh', 'bash', 'fish']
export const CACHE_KEY = '__STEELBRAIN_CONSISTENT_ENV_V1'
export const assign = Object.assign || function (target, source) {
  for (const key in source) {
    if ({}.hasOwnProperty.call(source, key)) {
      target[key] = source[key]
    }
  }
  return target
}

export function identifyEnvironment() {
  const { command, parameters, options } = getCommand()
  options.timeout = SPAWN_TIMEOUT
  return spawnSync(command, parameters, options).stdout.toString().trim().split('\n')
}

export function identifyEnvironmentAsync() {
  return new Promise(function(resolve, reject) {
    const { command, parameters, options } = getCommand()
    const childProcess = spawn(command, parameters, options)
    const stdout = []
    const timer = setTimeout(function() {
      childProcess.kill()
      reject(new Error('Process execution timed out'))
    }, SPAWN_TIMEOUT)
    childProcess.stdout.on('data', function(chunk) {
      stdout.push(chunk)
    })
    childProcess.on('close', function() {
      clearTimeout(timer)
      resolve(stdout.join('').trim().split('\n'))
    })
    childProcess.on('error', function(error) {
      reject(error)
    })
  })
}

export function parse(rawEnvironment: Array<string>): Object {
  const environment = {}
  for (const chunk of rawEnvironment) {
    const index = chunk.indexOf('=')
    if (index !== -1) {
      const key = chunk.slice(0, index).trim()
      const value = chunk.slice(index + 1).trim()
      environment[key] = value
    }
  }
  return environment
}

export function applySugar(environment: Object) {
  let path = process.env.PATH ? process.env.PATH.split(':') : []
  if (environment.PATH) {
    for (const chunk of environment.PATH.split(':')) {
      if (chunk && path.indexOf(chunk) === -1) {
        path.push(chunk)
      }
    }
  }
  for (const entry of DEFAULT_PATHS) {
    if (path.indexOf(entry) === -1) {
      path = [entry].concat(path)
    }
  }
  if (!environment.USER) {
    if (process.env.USER) {
      environment.USER = process.env.USER
    } else if (environment.HOME) {
      environment.USER = Path.basename(environment.HOME)
    }
  }

  environment.PATH = uniqueArray(path).join(':')
  environment.PWD = process.cwd()
  environment.OLDPWD = environment.PWD
  return environment
}

export function getCommand(): { command: string, options: Object, parameters: Array<string> } {
  const command = process.env.SHELL || 'sh'
{'fraction_non_alphanumeric': 0.14954296160877514, 'fraction_numerical': 0.003656307129798903, 'mean_word_length': 3.4881050041017225, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3046075', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1709, 'n_words': 403}