All files install.js

100% Statements 22/22
100% Branches 10/10
100% Functions 2/2
100% Lines 19/19

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    6x 6x 6x   6x     5x 5x 4x 3x 2x 1x   1x 1x         8x 1x     7x 7x 5x   2x       6x        
'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'
  }
}
 
async function runInstall(dir, pm) {
  if (!SUPPORTED_PACKAGE_MANAGERS.has(pm)) {
    return { success: false, error: `Unsupported package manager: ${pm}` }
  }
 
  try {
    await execFile(pm, ['install'], { cwd: dir })
    return { success: true }
  } catch (err) {
    return { success: false, error: err.stderr || err.message }
  }
}
 
module.exports = {
  determinePackageManager,
  runInstall
}