27
src/cache/cache-manager.js
vendido
27
src/cache/cache-manager.js
vendido
@@ -11,13 +11,15 @@ class CacheManager {
|
|||||||
constructor() {
|
constructor() {
|
||||||
this.cacheDir = path.join(require('os').homedir(), '.alepm', 'cache');
|
this.cacheDir = path.join(require('os').homedir(), '.alepm', 'cache');
|
||||||
this.metadataFile = path.join(this.cacheDir, 'metadata.json');
|
this.metadataFile = path.join(this.cacheDir, 'metadata.json');
|
||||||
this.init();
|
this._initialized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
|
if (this._initialized) return;
|
||||||
|
|
||||||
await fs.ensureDir(this.cacheDir);
|
await fs.ensureDir(this.cacheDir);
|
||||||
|
|
||||||
if (!fs.existsSync(this.metadataFile)) {
|
if (!await fs.pathExists(this.metadataFile)) {
|
||||||
await this.saveMetadata({
|
await this.saveMetadata({
|
||||||
version: '1.0.0',
|
version: '1.0.0',
|
||||||
entries: {},
|
entries: {},
|
||||||
@@ -25,9 +27,12 @@ class CacheManager {
|
|||||||
lastCleanup: Date.now()
|
lastCleanup: Date.now()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this._initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(packageName, version) {
|
async get(packageName, version) {
|
||||||
|
await this.init();
|
||||||
const key = this.generateKey(packageName, version);
|
const key = this.generateKey(packageName, version);
|
||||||
const metadata = await this.loadMetadata();
|
const metadata = await this.loadMetadata();
|
||||||
|
|
||||||
@@ -38,7 +43,7 @@ class CacheManager {
|
|||||||
const entry = metadata.entries[key];
|
const entry = metadata.entries[key];
|
||||||
const filePath = path.join(this.cacheDir, entry.file);
|
const filePath = path.join(this.cacheDir, entry.file);
|
||||||
|
|
||||||
if (!fs.existsSync(filePath)) {
|
if (!await fs.pathExists(filePath)) {
|
||||||
// Remove stale entry
|
// Remove stale entry
|
||||||
delete metadata.entries[key];
|
delete metadata.entries[key];
|
||||||
await this.saveMetadata(metadata);
|
await this.saveMetadata(metadata);
|
||||||
@@ -67,6 +72,7 @@ class CacheManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async has(packageName, version) {
|
async has(packageName, version) {
|
||||||
|
await this.init();
|
||||||
const key = this.generateKey(packageName, version);
|
const key = this.generateKey(packageName, version);
|
||||||
const metadata = await this.loadMetadata();
|
const metadata = await this.loadMetadata();
|
||||||
|
|
||||||
@@ -78,7 +84,7 @@ class CacheManager {
|
|||||||
const filePath = path.join(this.cacheDir, entry.file);
|
const filePath = path.join(this.cacheDir, entry.file);
|
||||||
|
|
||||||
// Check if file exists
|
// Check if file exists
|
||||||
if (!fs.existsSync(filePath)) {
|
if (!await fs.pathExists(filePath)) {
|
||||||
// Remove stale entry
|
// Remove stale entry
|
||||||
delete metadata.entries[key];
|
delete metadata.entries[key];
|
||||||
await this.saveMetadata(metadata);
|
await this.saveMetadata(metadata);
|
||||||
@@ -89,6 +95,7 @@ class CacheManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async store(packageName, version, data) {
|
async store(packageName, version, data) {
|
||||||
|
await this.init();
|
||||||
const key = this.generateKey(packageName, version);
|
const key = this.generateKey(packageName, version);
|
||||||
const metadata = await this.loadMetadata();
|
const metadata = await this.loadMetadata();
|
||||||
|
|
||||||
@@ -117,7 +124,7 @@ class CacheManager {
|
|||||||
if (metadata.entries[key]) {
|
if (metadata.entries[key]) {
|
||||||
const oldEntry = metadata.entries[key];
|
const oldEntry = metadata.entries[key];
|
||||||
const oldFilePath = path.join(this.cacheDir, oldEntry.file);
|
const oldFilePath = path.join(this.cacheDir, oldEntry.file);
|
||||||
if (fs.existsSync(oldFilePath)) {
|
if (await fs.pathExists(oldFilePath)) {
|
||||||
await fs.remove(oldFilePath);
|
await fs.remove(oldFilePath);
|
||||||
metadata.totalSize -= oldEntry.size;
|
metadata.totalSize -= oldEntry.size;
|
||||||
}
|
}
|
||||||
@@ -135,6 +142,7 @@ class CacheManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async remove(packageName, version) {
|
async remove(packageName, version) {
|
||||||
|
await this.init();
|
||||||
const key = this.generateKey(packageName, version);
|
const key = this.generateKey(packageName, version);
|
||||||
const metadata = await this.loadMetadata();
|
const metadata = await this.loadMetadata();
|
||||||
|
|
||||||
@@ -145,7 +153,7 @@ class CacheManager {
|
|||||||
const entry = metadata.entries[key];
|
const entry = metadata.entries[key];
|
||||||
const filePath = path.join(this.cacheDir, entry.file);
|
const filePath = path.join(this.cacheDir, entry.file);
|
||||||
|
|
||||||
if (fs.existsSync(filePath)) {
|
if (await fs.pathExists(filePath)) {
|
||||||
await fs.remove(filePath);
|
await fs.remove(filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,13 +165,14 @@ class CacheManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async clean() {
|
async clean() {
|
||||||
|
await this.init();
|
||||||
const metadata = await this.loadMetadata();
|
const metadata = await this.loadMetadata();
|
||||||
let cleanedSize = 0;
|
let cleanedSize = 0;
|
||||||
|
|
||||||
for (const [, entry] of Object.entries(metadata.entries)) {
|
for (const [, entry] of Object.entries(metadata.entries)) {
|
||||||
const filePath = path.join(this.cacheDir, entry.file);
|
const filePath = path.join(this.cacheDir, entry.file);
|
||||||
|
|
||||||
if (fs.existsSync(filePath)) {
|
if (await fs.pathExists(filePath)) {
|
||||||
await fs.remove(filePath);
|
await fs.remove(filePath);
|
||||||
cleanedSize += entry.size;
|
cleanedSize += entry.size;
|
||||||
}
|
}
|
||||||
@@ -182,6 +191,7 @@ class CacheManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async verify() {
|
async verify() {
|
||||||
|
await this.init();
|
||||||
const metadata = await this.loadMetadata();
|
const metadata = await this.loadMetadata();
|
||||||
const corrupted = [];
|
const corrupted = [];
|
||||||
const missing = [];
|
const missing = [];
|
||||||
@@ -189,7 +199,7 @@ class CacheManager {
|
|||||||
for (const [key, entry] of Object.entries(metadata.entries)) {
|
for (const [key, entry] of Object.entries(metadata.entries)) {
|
||||||
const filePath = path.join(this.cacheDir, entry.file);
|
const filePath = path.join(this.cacheDir, entry.file);
|
||||||
|
|
||||||
if (!fs.existsSync(filePath)) {
|
if (!await fs.pathExists(filePath)) {
|
||||||
missing.push(key);
|
missing.push(key);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -220,6 +230,7 @@ class CacheManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getStats() {
|
async getStats() {
|
||||||
|
await this.init();
|
||||||
const metadata = await this.loadMetadata();
|
const metadata = await this.loadMetadata();
|
||||||
const entries = Object.values(metadata.entries);
|
const entries = Object.values(metadata.entries);
|
||||||
|
|
||||||
|
|||||||
@@ -281,6 +281,34 @@ class PackageManager {
|
|||||||
// Update lock file
|
// Update lock file
|
||||||
await this.lock.update(results.filter(r => !r.error));
|
await this.lock.update(results.filter(r => !r.error));
|
||||||
|
|
||||||
|
// Show installation summary only for main installations (not dependency installations)
|
||||||
|
if (!options.fromPackageJson) {
|
||||||
|
const successfulInstalls = results.filter(r => !r.error);
|
||||||
|
const failedInstalls = results.filter(r => r.error);
|
||||||
|
|
||||||
|
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 => {
|
||||||
|
const sourceLabel = result.source === 'cache' ? '(cached)' : `(${result.source})`;
|
||||||
|
console.log(chalk.green(` • ${result.name}@${result.version} ${chalk.gray(sourceLabel)}`));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (failedInstalls.length > 0) {
|
||||||
|
console.log('');
|
||||||
|
console.log(chalk.red(`✗ Failed to install ${failedInstalls.length} package(s):`));
|
||||||
|
failedInstalls.forEach(result => {
|
||||||
|
console.log(chalk.red(` • ${result.packageSpec}: ${result.error}`));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('');
|
||||||
|
}
|
||||||
|
|
||||||
console.log(chalk.green(`Installation completed. ${results.filter(r => !r.error).length} packages installed.`));
|
console.log(chalk.green(`Installation completed. ${results.filter(r => !r.error).length} packages installed.`));
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|||||||
Referencia en una nueva incidencia
Block a user