{"size": 1180, "ext": "js", "max_stars_repo_path": "packages/build/src/utils/install.js", "max_stars_repo_name": "lachlanjc/build", "max_stars_repo_stars_event_min_datetime": "2021-09-24T22:51:00.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-24T22:51:00.000Z", "max_issues_repo_path": "packages/build/src/utils/install.js", "max_issues_repo_name": "lachlanjc/build", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/build/src/utils/install.js", "max_forks_repo_name": "lachlanjc/build", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.5, "max_line_length": 99, "alphanum_fraction": 0.6703389831}
const pathExists = require('path-exists')
const execa = require('execa')

const { addErrorInfo } = require('../error/info')

// Install Node.js dependencies in a specific directory
const installDependencies = async function(packageRoot) {
  if (await pathExists(`${packageRoot}/node_modules`)) {
    return
  }

  try {
    await execa.command('npm install --no-progress --no-audit --no-fund --no-package-lock', {
      cwd: packageRoot,
      stdio: 'inherit',
      preferLocal: true,
    })
  } catch (error) {
    error.message = `Error while installing dependencies in ${packageRoot}\n${error.message}`
    addErrorInfo(error, { type: 'dependencies' })
    throw error
  }
}

// Add new Node.js dependencies
const addDependency = async function(packageName, { packageRoot }) {
  try {
    await execa.command(`npm install --no-progress --no-audit --no-fund ${packageName}`, {
      cwd: packageRoot,
      all: true,
    })
  } catch (error) {
    const errorA = new Error(`Error while installing dependencies in ${packageRoot}\n${error.all}`)
    addErrorInfo(errorA, { type: 'dependencies' })
    throw errorA
  }
}

module.exports = { installDependencies, addDependency }
