All files / install-all/src install.js

100% Statements 24/24
100% Branches 13/13
100% Functions 2/2
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48    8x 8x 8x   8x     5x 5x 4x 3x 2x 1x   1x 1x       8x               16x 1x     15x   15x 15x 13x   2x       8x        
'use strict'
 
const fs = require('node:fs')
const promisify = require('node:util').promisify
const execFile = promisify(require('node:child_process').execFile)
 
const SUPPORTED_PACKAGE_MANAGERS = new Set(['npm', 'yarn', 'pnpm', 'bun'])
 
function determinePackageManager(dir) {
  try {
    const files = fs.readdirSync(dir)
    if (files.includes('yarn.lock')) return 'yarn'
    if (files.includes('pnpm-lock.yaml')) return 'pnpm'
    if (files.includes('bun.lockb')) return 'bun'
    return 'npm'
  } catch (err) {
    console.error(`Failed to read directory ${dir}:`, err)
    return 'npm'
  }
}
 
const CLEAN_INSTALL_ARGS = {
  npm:  ['ci'],
  yarn: ['install', '--frozen-lockfile'],
  pnpm: ['install', '--frozen-lockfile'],
  bun:  ['install', '--frozen-lockfile'],
}
 
async function runInstall(dir, pm, options = {}) {
  if (!SUPPORTED_PACKAGE_MANAGERS.has(pm)) {
    return { success: false, error: `Unsupported package manager: ${pm}` }
  }
 
  const args = options.clean ? CLEAN_INSTALL_ARGS[pm] : ['install']
 
  try {
    await execFile(pm, args, { cwd: dir })
    return { success: true }
  } catch (err) {
    return { success: false, error: err.stderr || err.message }
  }
}
 
module.exports = {
  determinePackageManager,
  runInstall
}