import fs from 'fs'; import path from 'path'; import { exec, execSync } from 'child_process'; import { promisify } from 'util'; const execPromise = promisify(exec); export function installDeps() { let parentDirectory = process.cwd(); if (parentDirectory.includes('exam') && !fs.existsSync(path.join(parentDirectory, 'exam'))) { parentDirectory = path.join(parentDirectory, '..'); } const examDirectory = path.join(parentDirectory, 'exam'); // install starter/exam dependencies if (!fs.existsSync(path.join(examDirectory, 'node_modules'))) { console.log('Installing starter dependencies...'); execSync('npm install', { stdio: 'inherit', cwd: examDirectory }); console.log('Starter dependencies installed'); } // install dev dependencies if (!fs.existsSync(path.join(parentDirectory, 'node_modules'))) { // install dev dependencies (in the background) console.log(); console.log(); console.log(); console.log('Installing dev dependencies...'); installDevDeps(parentDirectory); } if (!fs.existsSync(path.join(parentDirectory, 'hardhat', 'node_modules'))) { // install hardhat dependencies (in the background) console.log(); console.log(); console.log(); console.log('Installing hardhat dependencies...'); installDevDeps(path.join(parentDirectory, 'hardhat')); } } async function installDevDeps(workingDirectory, attempt = 1) { try { await execPromise('npm install', { stdio: 'inherit', cwd: workingDirectory }); console.log('Dev dependencies installed'); } catch (error) { console.error('Error installing dev dependencies:', error.message); if (attempt < 3) { const retryDelay = 5000 * attempt; console.log(`Retrying in ${Math.round(retryDelay / 1000)} seconds...`); setTimeout(() => installDevDeps(workingDirectory, attempt + 1), retryDelay); } else { console.error('Failed to install dev dependencies.'); console.error('Please try again manually, by running "npm install" in the project directory.'); } } }