Signed-off-by: ale <ale@manalejandro.com>
Este commit está contenido en:
ale
2025-08-19 03:59:14 +02:00
padre 629919cbbc
commit 83419364ab
Se han modificado 2 ficheros con 104 adiciones y 8 borrados

Ver fichero

@@ -228,6 +228,9 @@ class PackageManager {
console.log(chalk.green(`✓ Installed ${name}@${version || 'latest'}`));
// Install dependencies recursively (with depth control)
await this.installDependenciesRecursively(name, targetDir, options);
} catch (error) {
console.error(chalk.red(`✗ Failed to install ${packageSpec}: ${error.message}`));
results.push({ packageSpec, error: error.message });
@@ -246,6 +249,94 @@ class PackageManager {
return results;
}
async installDependenciesRecursively(packageName, packageDir, options = {}) {
try {
// Read the package.json of the installed package
const packageJsonPath = path.join(packageDir, 'package.json');
if (!await fs.pathExists(packageJsonPath)) {
// No package.json found, skip dependency installation
return;
}
const packageJson = await fs.readJson(packageJsonPath);
const dependencies = {
...packageJson.dependencies,
...(options.includeDev ? packageJson.devDependencies : {})
};
if (!dependencies || Object.keys(dependencies).length === 0) {
// No dependencies to install
return;
}
// Initialize installed packages tracking if not exists
if (!options._installedPackages) {
options._installedPackages = new Set();
}
// Limit recursion depth to prevent infinite loops
const currentDepth = options._depth || 0;
if (currentDepth > 8) { // Increased from 5 to 8 for better dependency coverage
// Silent skip for deep dependencies to avoid log noise
return;
}
console.log(chalk.blue(`Installing dependencies for ${packageName}...`));
// Filter out already installed packages to avoid duplicates
const dependenciesToInstall = Object.entries(dependencies).filter(([name, version]) => {
const packageKey = `${name}@${version}`;
if (options._installedPackages.has(packageKey)) {
return false; // Skip already installed package
}
// Check if package already exists in node_modules
const targetDir = options.global
? path.join(this.globalRoot, 'node_modules', name)
: path.join(this.projectRoot, 'node_modules', name);
return !fs.existsSync(targetDir);
});
if (dependenciesToInstall.length === 0) {
return;
}
// Prepare dependency specs for installation
const dependencySpecs = dependenciesToInstall.map(([name, version]) => {
// Mark as installed to prevent duplicates
options._installedPackages.add(`${name}@${version}`);
// Handle various version formats
if (version.startsWith('^') || version.startsWith('~') || version.startsWith('>=') || version.startsWith('<=')) {
return `${name}@${version}`;
} else if (version === '*' || version === 'latest') {
return `${name}@latest`;
} else if (semver.validRange(version)) {
return `${name}@${version}`;
} else {
// For non-semver versions (git urls, file paths, etc.), use as-is
return `${name}@${version}`;
}
});
// Install dependencies recursively by calling installPackages
const depOptions = {
...options,
fromPackageJson: true, // Prevent updating package.json
_depth: currentDepth + 1, // Increment depth
_installedPackages: options._installedPackages // Pass along installed packages set
};
// Install dependencies
await this.installPackages(dependencySpecs, depOptions);
} catch (error) {
console.warn(chalk.yellow(`Failed to install dependencies for ${packageName}: ${error.message}`));
}
}
async installPackage(pkg, options = {}) {
const targetDir = options.global
? path.join(this.globalRoot, 'node_modules', pkg.name)