<commit_before>const Assert = require('assert')
const Child = require('child_process')
const Fs = require('fs')
const Path = require('path')
const Parallel = require('run-parallel')

module.exports = (options, done) => {
  if (typeof options === 'function') {
    done = options
    options = {}
  }

  Assert.equal(typeof options, 'object', 'Options must be an object')
  Assert.equal(typeof done, 'function', 'Must pass in a callback function')

  let out = { env: process.env.NODE_ENV }

  packageDetails((err, data) => {
    if (err) return done(err)

    Object.assign(out, data)

    const cmds = Object.assign({ node: 'node -v', npm: 'npm -v' }, options)

    // eslint-disable-next-line handle-callback-err
    commandDetails(cmds, (err, data) => { // never yields error
      Object.assign(out, data)

      done(null, out)
    })
  })
}

const packageDetails = (done) => {
  const packagePath = Path.join(process.cwd(), 'package.json')

  Fs.readFile(packagePath, 'utf8', (err, data) => {
    if (err) return done(err)

    const { name, version } = JSON.parse(data)

    done(null, { name, version })
  })
}

const commandDetails = (cmds, done) => {
  const tasks = Object.keys(cmds)
    .reduce((acc, key) => {
      acc[key] = (next) => proc(cmds[key], next)
      return acc
    }, {})

  Parallel(tasks, done)
}

const proc = (cmd, done) => {
  Child.exec(cmd, (err, stdout) => {
    if (err) done(null, err.toString())
    else done(null, stdout.replace(/\n/g, ''))
  })
}<commit_msg>refactor: Reduce nesting and extraneous variables<commit_after>const Assert = require('assert')
const Child = require('child_process')
const FS = require('fs')
const Parallel = require('run-parallel')
const Path = require('path')

// get details about package and environment
module.exports = (options, done) => {
  if (typeof options === 'function') {
    done = options
    options = {}
  }

  Assert.equal(typeof options, 'object', 'Options must be an object')
  Assert.equal(typeof done, 'function', 'Must pass in a callback function')

  Parallel([
    packageDetails,
    (next) => process.nextTick(next, null, { env: process.env.NODE_ENV }),
    (next) => {
      const cmds = Object.assign({ node: 'node -v', npm: 'npm -v' }, options)

      commandDetails(cmds, next)
    }
  ], (err, results) => {
    if (err) done(err)
    else done(null, Object.assign({}, ...results))
  })
}

// get name and version from package
const packageDetails = (done) => {
  const packagePath = Path.join(process.cwd(), 'package.json')

  FS.readFile(packagePath, 'utf8', (err, data) => {
    if (err) return done(err)

    const { name, version } = JSON.parse(data)

    done(null, { name, version })
  })
}

// execute all passed commands and yield results
const commandDetails = (cmds, done) => {
  const tasks = Object.keys(cmds)
    .reduce((acc, key) => Object.assign(acc, { [key]: proc(cmds[key]) }), {})

  Parallel(tasks, done)
}

// curry command, return function that yields error string or result
const proc = (cmd) => (done) => {
  Child.exec(cmd, (err, stdout) => {
    if (err) done(null, err.toString())
    else done(null, stdout.replace(/\n/g, ''))
  })
}
{'fraction_non_alphanumeric': 0.17077798861480076, 'fraction_numerical': 0.0006325110689437065, 'mean_word_length': 3.4675141242937855, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, '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': '2024907', 'n_tokens_mistral': 1055, 'n_tokens_neox': 1030, 'n_words': 284}