UNPKG

@korautils/peer-deps

Version:

A utility library designed to resolve peer dependency requirements for other packages. This tool simplifies the process of managing peer dependencies, ensuring compatibility and ease of use.

80 lines 3.31 kB
import { execSync } from 'child_process'; import fs from 'fs'; import path from 'path'; // Function to get the peerDependencies of a package from its package.json in node_modules function getPeerDependencies(packageName) { const packagePath = path.join(process.cwd(), 'node_modules', packageName, 'package.json'); if (fs.existsSync(packagePath)) { const packageJson = require(packagePath); return packageJson.peerDependencies || {}; } else { console.log(`No package.json found for ${packageName} in node_modules.`); return {}; } } // Function to detect the package manager function getPackageManager() { if (fs.existsSync(path.join(process.cwd(), 'pnpm-lock.yaml'))) { return 'pnpm'; } else if (fs.existsSync(path.join(process.cwd(), 'yarn.lock'))) { return 'yarn'; } else if (fs.existsSync(path.join(process.cwd(), 'package-lock.json'))) { return 'npm'; } else { console.log('No lock file found. Assuming npm...'); return 'npm'; } } // Function to install the packages and their peerDependencies async function installPackages(packages) { const packageManager = getPackageManager(); console.log(`Detected package manager: ${packageManager}`); try { // Install the main packages console.log('Installing main packages...'); const cmd = `${packageManager} add ${packages.join(' ')}`; console.log(cmd); execSync(cmd, { stdio: 'inherit' }); // Get and install the peerDependencies for (const pkg of packages) { const peerDependencies = getPeerDependencies(pkg); if (Object.keys(peerDependencies).length === 0) { console.log(`No peerDependencies found for ${pkg}.`); } else { console.log(`Installing peerDependencies for ${pkg}:`); for (const [dep, version] of Object.entries(peerDependencies)) { console.log('\n'); console.log('-----------------------------------------------------'); console.log(`- 📦 ${dep}@${version}`); console.log('-----------------------------------------------------'); execSync(`${packageManager} add ${dep}@${version}`, { stdio: 'inherit' }); } } } console.log('-----------------------------------------------------'); console.log('✅ Installation completed.'); console.log('-----------------------------------------------------'); } catch (error) { console.error('Error installing packages and their dependencies:', error); } } // Function to get the installed version of a package from package.json function getInstalledVersion(packageName) { const packageJson = require('./package.json'); const version = packageJson.dependencies?.[packageName] || packageJson.devDependencies?.[packageName]; return version || null; } // Get the package names from the command line arguments const packages = process.argv.slice(2); if (packages.length === 0) { console.log('Please provide the names of the packages to install.'); process.exit(1); } installPackages(packages); //# sourceMappingURL=index.js.map