@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.
85 lines • 3.7 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
// Function to get the peerDependencies of a package from its package.json in node_modules
function getPeerDependencies(packageName) {
const packagePath = path_1.default.join(process.cwd(), 'node_modules', packageName, 'package.json');
if (fs_1.default.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_1.default.existsSync(path_1.default.join(process.cwd(), 'pnpm-lock.yaml'))) {
return 'pnpm';
}
else if (fs_1.default.existsSync(path_1.default.join(process.cwd(), 'yarn.lock'))) {
return 'yarn';
}
else if (fs_1.default.existsSync(path_1.default.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);
(0, child_process_1.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('-----------------------------------------------------');
(0, child_process_1.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