Comparar commits

...

5 Commits

Autor SHA1 Mensaje Fecha
ale
511b545ffb 1.1.0 2025-08-19 23:13:15 +02:00
ale
76e9c939e2 v1.1.0
Signed-off-by: ale <ale@manalejandro.com>
2025-08-19 23:11:19 +02:00
ale
6c575750ff 1.0.7 2025-08-19 06:57:59 +02:00
ale
9eb10395ad v1.0.7
Signed-off-by: ale <ale@manalejandro.com>
2025-08-19 06:57:52 +02:00
ale
e46dd6a16a temporizador
Signed-off-by: ale <ale@manalejandro.com>
2025-08-19 06:51:59 +02:00
Se han modificado 2 ficheros con 164 adiciones y 72 borrados

Ver fichero

@@ -1,6 +1,6 @@
{
"name": "alepm",
"version": "1.0.6",
"version": "1.1.0",
"description": "Advanced and secure Node.js package manager with binary storage, intelligent caching, and comprehensive security features",
"main": "src/index.js",
"bin": {

Ver fichero

@@ -115,7 +115,7 @@ class PackageManager {
const packageJson = await fs.readJson(packageJsonPath);
const dependencies = {
...packageJson.dependencies,
...(options.includeDev ? packageJson.devDependencies : {})
...(options.includeDev || options.saveDev ? packageJson.devDependencies : {})
};
if (Object.keys(dependencies).length === 0) {
@@ -125,8 +125,8 @@ class PackageManager {
const packages = Object.entries(dependencies).map(([name, version]) => `${name}@${version}`);
// Call the main installation logic directly, bypassing the package.json check
return await this.installPackages(packages, { ...options, fromPackageJson: true });
// Call the main installation logic with special flag for package.json installs
return await this.installPackages(packages, { ...options, fromPackageJsonMain: true });
}
async installPackages(packages, options = {}) {
@@ -134,6 +134,13 @@ class PackageManager {
return;
}
// Start timer for installation (for main installations only)
const isMainInstall = !options.fromPackageJson && !options._depth;
const isPackageJsonMain = options.fromPackageJsonMain;
const isRecursiveInstall = options._depth > 0;
const shouldShowSummary = (isMainInstall || isPackageJsonMain) && !isRecursiveInstall;
const startTime = shouldShowSummary ? Date.now() : null;
console.log(chalk.blue(`Installing ${packages.length} package(s)...`));
const results = [];
@@ -281,21 +288,32 @@ class PackageManager {
// Update lock file
await this.lock.update(results.filter(r => !r.error));
// Show installation summary only for main installations (not dependency installations)
if (!options.fromPackageJson) {
// Show installation summary for main installations (including package.json main installs)
if (shouldShowSummary) {
const successfulInstalls = results.filter(r => !r.error);
const failedInstalls = results.filter(r => r.error);
// Calculate elapsed time
const endTime = Date.now();
const elapsedTime = startTime ? endTime - startTime : 0;
const elapsedSeconds = (elapsedTime / 1000).toFixed(2);
console.log('');
console.log(chalk.green('📦 Installation Summary:'));
console.log('');
if (successfulInstalls.length > 0) {
console.log(chalk.green(`✓ Successfully installed ${successfulInstalls.length} package(s):`));
successfulInstalls.forEach(result => {
if (successfulInstalls.length === 1 && !isPackageJsonMain) {
const result = successfulInstalls[0];
const sourceLabel = result.source === 'cache' ? '(cached)' : `(${result.source})`;
console.log(chalk.green(` ${result.name}@${result.version} ${chalk.gray(sourceLabel)}`));
});
console.log(chalk.green(`✓ Successfully installed ${result.name}@${result.version} ${chalk.gray(sourceLabel)}`));
} else {
console.log(chalk.green(`✓ Successfully installed ${successfulInstalls.length} package(s):`));
successfulInstalls.forEach(result => {
const sourceLabel = result.source === 'cache' ? '(cached)' : `(${result.source})`;
console.log(chalk.green(`${result.name}@${result.version} ${chalk.gray(sourceLabel)}`));
});
}
}
if (failedInstalls.length > 0) {
@@ -307,6 +325,8 @@ class PackageManager {
}
console.log('');
console.log(chalk.gray(`⏱️ Total time: ${elapsedSeconds}s`));
console.log('');
}
console.log(chalk.green(`Installation completed. ${results.filter(r => !r.error).length} packages installed.`));
@@ -329,9 +349,13 @@ class PackageManager {
...(options.includeDev ? packageJson.devDependencies : {})
};
const optionalDependencies = packageJson.optionalDependencies || {};
if (!dependencies || Object.keys(dependencies).length === 0) {
// No dependencies to install
return;
// If no regular dependencies, check if there are optional dependencies to install
if (Object.keys(optionalDependencies).length === 0) {
return;
}
}
// Initialize installed packages tracking if not exists
@@ -348,6 +372,14 @@ class PackageManager {
console.log(chalk.blue(`Installing dependencies for ${packageName}...`));
// 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
};
// Filter out already installed packages to avoid duplicates
const dependenciesToInstall = Object.entries(dependencies).filter(([name, version]) => {
const packageKey = `${name}@${version}`;
@@ -364,37 +396,74 @@ class PackageManager {
});
if (dependenciesToInstall.length === 0) {
return;
// No regular dependencies to install, but continue to check optional dependencies
} else {
// 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
await this.installPackages(dependencySpecs, depOptions);
}
// Prepare dependency specs for installation
const dependencySpecs = dependenciesToInstall.map(([name, version]) => {
// Mark as installed to prevent duplicates
options._installedPackages.add(`${name}@${version}`);
// Install optional dependencies (ignore failures)
if (Object.keys(optionalDependencies).length > 0) {
const optionalDependenciesToInstall = Object.entries(optionalDependencies).filter(([name, version]) => {
const packageKey = `${name}@${version}`;
if (options._installedPackages && options._installedPackages.has(packageKey)) {
return false; // Skip already installed package
}
// 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}`;
// 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);
const exists = fs.existsSync(targetDir);
return !exists;
});
if (optionalDependenciesToInstall.length > 0) {
const optionalSpecs = optionalDependenciesToInstall.map(([name, version]) => {
// Mark as installed to prevent duplicates
if (options._installedPackages) {
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 {
return `${name}@${version}`;
}
});
// Install optional dependencies (ignore failures)
try {
await this.installPackages(optionalSpecs, depOptions);
} catch (error) {
console.warn(chalk.yellow(`Some optional dependencies for ${packageName} could not be installed (this is usually safe to ignore)`));
}
}
});
// 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}`));
@@ -688,12 +757,8 @@ class PackageManager {
const { spawn } = require('child_process');
return new Promise((resolve, reject) => {
// Determine shell based on OS
const isWindows = process.platform === 'win32';
const shell = isWindows ? 'cmd' : 'sh';
const shellFlag = isWindows ? '/c' : '-c';
const childProcess = spawn(shell, [shellFlag, script], {
// Use cross-platform approach
const childProcess = spawn(script, [], {
cwd: this.projectRoot,
stdio: options.silent ? 'pipe' : 'inherit',
shell: true,
@@ -704,11 +769,12 @@ class PackageManager {
}
});
// Initialize output variables for silent mode
let stdout = '';
let stderr = '';
// Handle silent mode
if (options.silent) {
let stdout = '';
let stderr = '';
if (childProcess.stdout) {
childProcess.stdout.on('data', (data) => {
stdout += data.toString();
@@ -720,34 +786,60 @@ class PackageManager {
stderr += data.toString();
});
}
childProcess.on('close', (code) => {
if (code === 0) {
if (stdout.trim()) {
console.log(stdout.trim());
}
resolve();
} else {
if (stderr.trim()) {
console.error(stderr.trim());
}
reject(new Error(`Script "${scriptName}" exited with code ${code}`));
}
});
} else {
childProcess.on('close', (code) => {
if (code === 0) {
console.log(chalk.green(`✓ Script "${scriptName}" completed successfully`));
resolve();
} else {
reject(new Error(`Script "${scriptName}" exited with code ${code}`));
}
});
}
// Handle process completion
let completed = false;
const handleCompletion = (code, signal) => {
if (completed) return;
completed = true;
if (options.silent) {
if (stdout && stdout.trim()) {
console.log(stdout.trim());
}
if (stderr && stderr.trim()) {
console.error(stderr.trim());
}
}
if (code === 0) {
if (!options.silent) {
console.log(chalk.green(`✓ Script "${scriptName}" completed successfully`));
}
resolve();
} else {
const errorMsg = signal
? `Script "${scriptName}" was terminated by signal ${signal}`
: `Script "${scriptName}" exited with code ${code}`;
reject(new Error(errorMsg));
}
};
childProcess.on('close', handleCompletion);
childProcess.on('exit', handleCompletion);
childProcess.on('error', (error) => {
if (completed) return;
completed = true;
reject(new Error(`Failed to run script "${scriptName}": ${error.message}`));
});
// Handle process termination signals
const cleanup = () => {
if (!completed && !childProcess.killed) {
childProcess.kill('SIGTERM');
setTimeout(() => {
if (!childProcess.killed) {
childProcess.kill('SIGKILL');
}
}, 5000);
}
};
process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);
});
}