stigmergy 1.1.5 โ 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/STIGMERGY.md +43 -23
- package/package.json +14 -27
- package/scripts/preinstall-check.js +78 -16
- package/src/cli/router.js +378 -132
- package/src/core/cache_cleaner.js +744 -0
- package/src/core/cli_tools.js +1 -1
- package/src/core/coordination/nodejs/CLCommunication.js +86 -21
- package/src/core/coordination/nodejs/HookDeploymentManager.js +56 -13
- package/src/core/enhanced_installer.js +456 -0
- package/src/core/enhanced_uninstaller.js +618 -0
- package/src/core/installer.js +125 -28
- package/src/core/upgrade_manager.js +403 -0
- package/test/cache-cleaner-implemented.test.js +328 -0
- package/test/cache-cleaner.test.js +390 -0
- package/test/comprehensive-enhanced-features.test.js +252 -0
- package/test/enhanced-uninstaller-implemented.test.js +271 -0
- package/test/enhanced-uninstaller.test.js +284 -0
- package/test/plugin-deployment-test.js +1 -1
- package/test/safe-installation-cleaner.test.js +343 -0
- package/test/stigmergy-upgrade-test.js +243 -0
|
@@ -0,0 +1,744 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Comprehensive Cache Cleaner for Stigmergy
|
|
3
|
+
*
|
|
4
|
+
* Intelligent cache cleaning with selective removal, performance optimization,
|
|
5
|
+
* and error recovery capabilities.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const fs = require('fs');
|
|
9
|
+
const path = require('path');
|
|
10
|
+
const os = require('os');
|
|
11
|
+
const { spawnSync } = require('child_process');
|
|
12
|
+
|
|
13
|
+
class CacheCleaner {
|
|
14
|
+
constructor(options = {}) {
|
|
15
|
+
this.options = {
|
|
16
|
+
dryRun: options.dryRun || false,
|
|
17
|
+
force: options.force || false,
|
|
18
|
+
verbose: options.verbose || false,
|
|
19
|
+
preserveRecent: options.preserveRecent || 24 * 60 * 60 * 1000, // 24 hours
|
|
20
|
+
batchSize: options.batchSize || 50,
|
|
21
|
+
parallel: options.parallel || true,
|
|
22
|
+
...options
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
this.homeDir = os.homedir();
|
|
26
|
+
this.results = {
|
|
27
|
+
filesRemoved: 0,
|
|
28
|
+
directoriesRemoved: 0,
|
|
29
|
+
bytesFreed: 0,
|
|
30
|
+
errors: [],
|
|
31
|
+
skipped: []
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Clean all caches comprehensively
|
|
37
|
+
*/
|
|
38
|
+
async cleanAllCaches(options = {}) {
|
|
39
|
+
const config = {
|
|
40
|
+
cleanStigmergy: true,
|
|
41
|
+
cleanNPX: true,
|
|
42
|
+
cleanNPM: true,
|
|
43
|
+
cleanCLI: true,
|
|
44
|
+
cleanTemp: true,
|
|
45
|
+
...options
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
console.log('๐งน Starting Comprehensive Cache Cleaning...\n');
|
|
49
|
+
|
|
50
|
+
if (this.options.dryRun) {
|
|
51
|
+
console.log('๐ DRY RUN MODE - No files will be deleted\n');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
// 1. Clean Stigmergy cache
|
|
56
|
+
if (config.cleanStigmergy) {
|
|
57
|
+
await this.cleanStigmergyCache();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 2. Clean NPX cache
|
|
61
|
+
if (config.cleanNPX) {
|
|
62
|
+
await this.cleanNPXCache();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 3. Clean NPM cache
|
|
66
|
+
if (config.cleanNPM) {
|
|
67
|
+
await this.cleanNPMCache();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 4. Clean CLI configurations
|
|
71
|
+
if (config.cleanCLI) {
|
|
72
|
+
await this.cleanCLIConfigurations();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// 5. Clean temporary files
|
|
76
|
+
if (config.cleanTemp) {
|
|
77
|
+
await this.cleanTemporaryFiles();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 6. Print summary
|
|
81
|
+
this.printSummary();
|
|
82
|
+
|
|
83
|
+
return this.results;
|
|
84
|
+
|
|
85
|
+
} catch (error) {
|
|
86
|
+
console.error('โ Cache cleaning failed:', error.message);
|
|
87
|
+
this.results.errors.push(error.message);
|
|
88
|
+
return this.results;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Clean Stigmergy cache and temporary files
|
|
94
|
+
*/
|
|
95
|
+
async cleanStigmergyCache() {
|
|
96
|
+
console.log('๐ Cleaning Stigmergy cache...');
|
|
97
|
+
|
|
98
|
+
const stigmergyDir = path.join(this.homeDir, '.stigmergy');
|
|
99
|
+
const testDir = path.join(this.homeDir, '.stigmergy-test');
|
|
100
|
+
|
|
101
|
+
// Clean main cache directory
|
|
102
|
+
if (fs.existsSync(stigmergyDir)) {
|
|
103
|
+
await this.cleanStigmergyDirectory(stigmergyDir, 'main');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Clean test directory
|
|
107
|
+
if (fs.existsSync(testDir)) {
|
|
108
|
+
await this.cleanStigmergyDirectory(testDir, 'test');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Clean cache subdirectories specifically
|
|
112
|
+
const cachePaths = [
|
|
113
|
+
path.join(stigmergyDir, 'cache'),
|
|
114
|
+
path.join(stigmergyDir, 'logs'),
|
|
115
|
+
path.join(stigmergyDir, 'temp'),
|
|
116
|
+
path.join(stigmergyDir, '.tmp')
|
|
117
|
+
];
|
|
118
|
+
|
|
119
|
+
for (const cachePath of cachePaths) {
|
|
120
|
+
if (fs.existsSync(cachePath)) {
|
|
121
|
+
await this.cleanDirectory(cachePath);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
console.log('โ
Stigmergy cache cleaning completed');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Clean a specific Stigmergy directory
|
|
130
|
+
*/
|
|
131
|
+
async cleanStigmergyDirectory(dirPath, type) {
|
|
132
|
+
console.log(` ๐ Cleaning ${type} directory...`);
|
|
133
|
+
|
|
134
|
+
const files = await this.scanDirectory(dirPath);
|
|
135
|
+
const recentFiles = this.filterRecentFiles(files);
|
|
136
|
+
|
|
137
|
+
if (recentFiles.length === 0) {
|
|
138
|
+
console.log(` โน๏ธ No recent files to clean in ${type} directory`);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
console.log(` ๐ Found ${recentFiles.length} files to clean`);
|
|
143
|
+
|
|
144
|
+
if (this.options.dryRun) {
|
|
145
|
+
this.logFiles(recentFiles, ' ๐ ');
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const removed = await this.batchRemoveFiles(recentFiles);
|
|
150
|
+
console.log(` โ
Removed ${removed} files from ${type} directory`);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Clean NPX cache of Stigmergy entries
|
|
155
|
+
*/
|
|
156
|
+
async cleanNPXCache() {
|
|
157
|
+
console.log('๐ฆ Cleaning NPX cache...');
|
|
158
|
+
|
|
159
|
+
const npxCacheDirs = await this.findNPXCacheDirectories();
|
|
160
|
+
|
|
161
|
+
if (npxCacheDirs.length === 0) {
|
|
162
|
+
console.log(' โน๏ธ No Stigmergy entries in NPX cache');
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
console.log(` ๐ฆ Found ${npxCacheDirs.length} Stigmergy cache entries`);
|
|
167
|
+
|
|
168
|
+
if (this.options.dryRun) {
|
|
169
|
+
this.logFiles(npxCacheDirs, ' ๐ ');
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
let removed = 0;
|
|
174
|
+
const failed = [];
|
|
175
|
+
|
|
176
|
+
for (const cacheDir of npxCacheDirs) {
|
|
177
|
+
try {
|
|
178
|
+
const size = await this.getDirectorySize(cacheDir);
|
|
179
|
+
await this.removeDirectory(cacheDir);
|
|
180
|
+
this.results.bytesFreed += size;
|
|
181
|
+
removed++;
|
|
182
|
+
} catch (error) {
|
|
183
|
+
failed.push(cacheDir);
|
|
184
|
+
this.results.errors.push(`NPX cache ${cacheDir}: ${error.message}`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
console.log(` โ
Removed ${removed} NPX cache entries`);
|
|
189
|
+
if (failed.length > 0) {
|
|
190
|
+
console.log(` โ ๏ธ Failed to remove ${failed.length} entries`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Clean NPM cache
|
|
196
|
+
*/
|
|
197
|
+
async cleanNPMCache() {
|
|
198
|
+
console.log('๐ฆ Cleaning NPM cache...');
|
|
199
|
+
|
|
200
|
+
try {
|
|
201
|
+
// Use npm cache clean command
|
|
202
|
+
if (this.options.dryRun) {
|
|
203
|
+
console.log(' ๐ Would run: npm cache clean --force');
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const result = spawnSync('npm', ['cache', 'clean', '--force'], {
|
|
208
|
+
encoding: 'utf8',
|
|
209
|
+
shell: true,
|
|
210
|
+
stdio: this.options.verbose ? 'inherit' : 'pipe'
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
if (result.status === 0) {
|
|
214
|
+
console.log(' โ
NPM cache cleaned successfully');
|
|
215
|
+
} else {
|
|
216
|
+
console.log(' โ ๏ธ NPM cache clean failed, trying manual cleanup');
|
|
217
|
+
await this.manualNPMCacheClean();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
} catch (error) {
|
|
221
|
+
console.error(` โ Failed to clean NPM cache: ${error.message}`);
|
|
222
|
+
this.results.errors.push(`NPM cache: ${error.message}`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Manual NPM cache cleaning fallback
|
|
228
|
+
*/
|
|
229
|
+
async manualNPMCacheClean() {
|
|
230
|
+
const npmCacheDirs = [
|
|
231
|
+
path.join(this.homeDir, '.npm', '_cacache'),
|
|
232
|
+
path.join(this.homeDir, 'AppData', 'Local', 'npm-cache', '_cacache')
|
|
233
|
+
];
|
|
234
|
+
|
|
235
|
+
for (const cacheDir of npmCacheDirs) {
|
|
236
|
+
if (fs.existsSync(cacheDir)) {
|
|
237
|
+
console.log(` ๐งน Manual cleanup of ${cacheDir}`);
|
|
238
|
+
await this.cleanDirectory(cacheDir);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Clean CLI configurations
|
|
245
|
+
*/
|
|
246
|
+
async cleanCLIConfigurations() {
|
|
247
|
+
console.log('โ๏ธ Cleaning CLI configurations...');
|
|
248
|
+
|
|
249
|
+
const supportedCLIs = [
|
|
250
|
+
'claude', 'gemini', 'qwen', 'iflow', 'qodercli',
|
|
251
|
+
'codebuddy', 'codex', 'copilot', 'qwencode'
|
|
252
|
+
];
|
|
253
|
+
|
|
254
|
+
let totalCleaned = 0;
|
|
255
|
+
|
|
256
|
+
for (const cli of supportedCLIs) {
|
|
257
|
+
const cliConfig = path.join(this.homeDir, `.${cli}`);
|
|
258
|
+
|
|
259
|
+
if (!fs.existsSync(cliConfig)) {
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const stigmergyFiles = await this.findStigmergyFiles(cliConfig);
|
|
264
|
+
|
|
265
|
+
if (stigmergyFiles.length === 0) {
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
console.log(` ๐ ${cli}: ${stigmergyFiles.length} Stigmergy files`);
|
|
270
|
+
|
|
271
|
+
if (this.options.dryRun) {
|
|
272
|
+
this.logFiles(stigmergyFiles, ' ๐ ');
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const removed = await this.batchRemoveFiles(stigmergyFiles);
|
|
277
|
+
totalCleaned += removed;
|
|
278
|
+
|
|
279
|
+
if (removed > 0) {
|
|
280
|
+
console.log(` โ
Cleaned ${removed} files from ${cli}`);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (totalCleaned > 0) {
|
|
285
|
+
console.log(` โ
Cleaned ${totalCleaned} CLI configuration files`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Clean temporary files
|
|
291
|
+
*/
|
|
292
|
+
async cleanTemporaryFiles() {
|
|
293
|
+
console.log('๐๏ธ Cleaning temporary files...');
|
|
294
|
+
|
|
295
|
+
const tempDirs = [
|
|
296
|
+
os.tmpdir(),
|
|
297
|
+
path.join(this.homeDir, 'AppData', 'Local', 'Temp'),
|
|
298
|
+
path.join(this.homeDir, 'AppData', 'Local', 'npm-cache', '_tmp')
|
|
299
|
+
];
|
|
300
|
+
|
|
301
|
+
let totalRemoved = 0;
|
|
302
|
+
|
|
303
|
+
for (const tempDir of tempDirs) {
|
|
304
|
+
if (!fs.existsSync(tempDir)) {
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const tempFiles = await this.findStigmergyTempFiles(tempDir);
|
|
309
|
+
|
|
310
|
+
if (tempFiles.length === 0) {
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
console.log(` ๐ ${path.basename(tempDir)}: ${tempFiles.length} temporary files`);
|
|
315
|
+
|
|
316
|
+
if (this.options.dryRun) {
|
|
317
|
+
this.logFiles(tempFiles.slice(0, 5), ' ๐ ');
|
|
318
|
+
if (tempFiles.length > 5) {
|
|
319
|
+
console.log(` ... and ${tempFiles.length - 5} more`);
|
|
320
|
+
}
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const removed = await this.batchRemoveFiles(tempFiles);
|
|
325
|
+
totalRemoved += removed;
|
|
326
|
+
|
|
327
|
+
if (removed > 0) {
|
|
328
|
+
console.log(` โ
Removed ${removed} files from ${path.basename(tempDir)}`);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (totalRemoved > 0) {
|
|
333
|
+
console.log(` โ
Removed ${totalRemoved} temporary files`);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Selective cleaning with patterns
|
|
339
|
+
*/
|
|
340
|
+
async selectiveClean(targetDirectory, options = {}) {
|
|
341
|
+
const {
|
|
342
|
+
preservePatterns = [],
|
|
343
|
+
removePatterns = [],
|
|
344
|
+
preserveRecent = this.options.preserveRecent
|
|
345
|
+
} = options;
|
|
346
|
+
|
|
347
|
+
console.log(`๐ฏ Selective cleaning: ${targetDirectory}`);
|
|
348
|
+
|
|
349
|
+
if (!fs.existsSync(targetDirectory)) {
|
|
350
|
+
console.log(' โน๏ธ Directory not found');
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const allFiles = await this.scanDirectory(targetDirectory);
|
|
355
|
+
const filesToRemove = [];
|
|
356
|
+
|
|
357
|
+
for (const file of allFiles) {
|
|
358
|
+
// Check preserve patterns
|
|
359
|
+
const shouldPreserve = preservePatterns.some(pattern =>
|
|
360
|
+
this.matchPattern(file, pattern)
|
|
361
|
+
) || this.isRecentFile(file, preserveRecent);
|
|
362
|
+
|
|
363
|
+
// Check remove patterns
|
|
364
|
+
const shouldRemove = removePatterns.some(pattern =>
|
|
365
|
+
this.matchPattern(file, pattern)
|
|
366
|
+
);
|
|
367
|
+
|
|
368
|
+
if (shouldRemove && !shouldPreserve) {
|
|
369
|
+
filesToRemove.push(file);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
console.log(` ๐ Found ${filesToRemove.length} files to remove`);
|
|
374
|
+
|
|
375
|
+
if (this.options.dryRun) {
|
|
376
|
+
this.logFiles(filesToRemove, ' ๐ ');
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const removed = await this.batchRemoveFiles(filesToRemove);
|
|
381
|
+
console.log(` โ
Selectively removed ${removed} files`);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Performance-optimized cleaning
|
|
386
|
+
*/
|
|
387
|
+
async cleanWithPerformance(targetDirectory, options = {}) {
|
|
388
|
+
const {
|
|
389
|
+
batchSize = this.options.batchSize,
|
|
390
|
+
parallel = this.options.parallel,
|
|
391
|
+
maxConcurrency = 4
|
|
392
|
+
} = options;
|
|
393
|
+
|
|
394
|
+
console.log(`โก Performance cleaning: ${targetDirectory}`);
|
|
395
|
+
|
|
396
|
+
const files = await this.scanDirectory(targetDirectory);
|
|
397
|
+
const recentFiles = this.filterRecentFiles(files);
|
|
398
|
+
|
|
399
|
+
console.log(` ๐ Processing ${recentFiles.length} files in batches of ${batchSize}`);
|
|
400
|
+
|
|
401
|
+
if (this.options.dryRun) {
|
|
402
|
+
console.log(` ๐ Would process in ${Math.ceil(recentFiles.length / batchSize)} batches`);
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
let removed = 0;
|
|
407
|
+
const batches = this.createBatches(recentFiles, batchSize);
|
|
408
|
+
|
|
409
|
+
if (parallel && batches.length > 1) {
|
|
410
|
+
removed = await this.parallelRemoveBatches(batches, maxConcurrency);
|
|
411
|
+
} else {
|
|
412
|
+
for (const batch of batches) {
|
|
413
|
+
const batchRemoved = await this.batchRemoveFiles(batch);
|
|
414
|
+
removed += batchRemoved;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
console.log(` โ
Performance cleaned ${removed} files`);
|
|
419
|
+
return removed;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Helper methods
|
|
424
|
+
*/
|
|
425
|
+
async scanDirectory(dirPath, files = []) {
|
|
426
|
+
try {
|
|
427
|
+
const items = fs.readdirSync(dirPath, { withFileTypes: true });
|
|
428
|
+
|
|
429
|
+
for (const item of items) {
|
|
430
|
+
const fullPath = path.join(dirPath, item.name);
|
|
431
|
+
|
|
432
|
+
if (item.isDirectory()) {
|
|
433
|
+
await this.scanDirectory(fullPath, files);
|
|
434
|
+
files.push(fullPath);
|
|
435
|
+
} else {
|
|
436
|
+
files.push(fullPath);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
} catch (error) {
|
|
440
|
+
this.results.errors.push(`Scan error ${dirPath}: ${error.message}`);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
return files;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
async batchRemoveFiles(files) {
|
|
447
|
+
let removed = 0;
|
|
448
|
+
|
|
449
|
+
for (const file of files) {
|
|
450
|
+
try {
|
|
451
|
+
const stat = fs.statSync(file);
|
|
452
|
+
|
|
453
|
+
if (this.removeFile(file)) {
|
|
454
|
+
removed++;
|
|
455
|
+
this.results.bytesFreed += stat.size;
|
|
456
|
+
}
|
|
457
|
+
} catch (error) {
|
|
458
|
+
this.results.errors.push(`Remove error ${file}: ${error.message}`);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
this.results.filesRemoved += removed;
|
|
463
|
+
return removed;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
removeFile(filePath) {
|
|
467
|
+
try {
|
|
468
|
+
if (!fs.existsSync(filePath)) {
|
|
469
|
+
return false;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const stat = fs.statSync(filePath);
|
|
473
|
+
|
|
474
|
+
if (stat.isDirectory()) {
|
|
475
|
+
fs.rmSync(filePath, { recursive: true, force: true });
|
|
476
|
+
this.results.directoriesRemoved++;
|
|
477
|
+
} else {
|
|
478
|
+
fs.unlinkSync(filePath);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
if (this.options.verbose) {
|
|
482
|
+
console.log(` Removed: ${path.basename(filePath)}`);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
return true;
|
|
486
|
+
} catch (error) {
|
|
487
|
+
if (!this.options.force) {
|
|
488
|
+
throw error;
|
|
489
|
+
}
|
|
490
|
+
this.results.skipped.push(`${filePath}: ${error.message}`);
|
|
491
|
+
return false;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
async removeDirectory(dirPath) {
|
|
496
|
+
try {
|
|
497
|
+
if (fs.existsSync(dirPath)) {
|
|
498
|
+
const size = await this.getDirectorySize(dirPath);
|
|
499
|
+
fs.rmSync(dirPath, { recursive: true, force: true });
|
|
500
|
+
this.results.bytesFreed += size;
|
|
501
|
+
this.results.directoriesRemoved++;
|
|
502
|
+
return true;
|
|
503
|
+
}
|
|
504
|
+
return false;
|
|
505
|
+
} catch (error) {
|
|
506
|
+
if (!this.options.force) {
|
|
507
|
+
throw error;
|
|
508
|
+
}
|
|
509
|
+
this.results.skipped.push(`Directory: ${dirPath} (${error.message})`);
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
async getDirectorySize(dirPath) {
|
|
515
|
+
let totalSize = 0;
|
|
516
|
+
|
|
517
|
+
try {
|
|
518
|
+
const files = await this.scanDirectory(dirPath);
|
|
519
|
+
|
|
520
|
+
for (const file of files) {
|
|
521
|
+
try {
|
|
522
|
+
const stat = fs.statSync(file);
|
|
523
|
+
totalSize += stat.size;
|
|
524
|
+
} catch (error) {
|
|
525
|
+
// Skip files we can't stat
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
} catch (error) {
|
|
529
|
+
// Return 0 for directories we can't scan
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
return totalSize;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
filterRecentFiles(files) {
|
|
536
|
+
return files.filter(file => !this.isRecentFile(file, this.options.preserveRecent));
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
isRecentFile(filePath, maxAge) {
|
|
540
|
+
try {
|
|
541
|
+
const stat = fs.statSync(filePath);
|
|
542
|
+
const age = Date.now() - stat.mtime.getTime();
|
|
543
|
+
return age < maxAge;
|
|
544
|
+
} catch (error) {
|
|
545
|
+
return false;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
async findStigmergyFiles(dirPath) {
|
|
550
|
+
const stigmergyFiles = [];
|
|
551
|
+
|
|
552
|
+
try {
|
|
553
|
+
const files = fs.readdirSync(dirPath, { withFileTypes: true });
|
|
554
|
+
|
|
555
|
+
for (const file of files) {
|
|
556
|
+
const fullPath = path.join(dirPath, file.name);
|
|
557
|
+
|
|
558
|
+
if (file.isDirectory()) {
|
|
559
|
+
stigmergyFiles.push(...await this.findStigmergyFiles(fullPath));
|
|
560
|
+
} else if (this.isStigmergyFile(file.name)) {
|
|
561
|
+
stigmergyFiles.push(fullPath);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
} catch (error) {
|
|
565
|
+
// Skip directories we can't read
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
return stigmergyFiles;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
isStigmergyFile(fileName) {
|
|
572
|
+
const stigmergyPatterns = [
|
|
573
|
+
'stigmergy',
|
|
574
|
+
'cross-cli',
|
|
575
|
+
'hook',
|
|
576
|
+
'integration',
|
|
577
|
+
'cache',
|
|
578
|
+
'.tmp',
|
|
579
|
+
'temp'
|
|
580
|
+
];
|
|
581
|
+
|
|
582
|
+
const lowerFileName = fileName.toLowerCase();
|
|
583
|
+
return stigmergyPatterns.some(pattern =>
|
|
584
|
+
lowerFileName.includes(pattern.toLowerCase())
|
|
585
|
+
);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
async findNPXCacheDirectories() {
|
|
589
|
+
const cacheDirs = [];
|
|
590
|
+
const possibleNPXBases = [
|
|
591
|
+
path.join(this.homeDir, 'AppData', 'Local', 'npm-cache', '_npx'),
|
|
592
|
+
path.join(this.homeDir, '.npm', '_npx'),
|
|
593
|
+
path.join(os.tmpdir(), 'npm-cache', '_npx')
|
|
594
|
+
];
|
|
595
|
+
|
|
596
|
+
for (const npxCacheBase of possibleNPXBases) {
|
|
597
|
+
if (!fs.existsSync(npxCacheBase)) {
|
|
598
|
+
continue;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
try {
|
|
602
|
+
const entries = fs.readdirSync(npxCacheBase);
|
|
603
|
+
|
|
604
|
+
for (const entry of entries) {
|
|
605
|
+
const entryPath = path.join(npxCacheBase, entry);
|
|
606
|
+
const stigmergyPath = path.join(entryPath, 'node_modules', 'stigmergy');
|
|
607
|
+
|
|
608
|
+
if (fs.existsSync(stigmergyPath)) {
|
|
609
|
+
cacheDirs.push(entryPath);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
} catch (error) {
|
|
613
|
+
this.results.errors.push(`NPX scan error: ${error.message}`);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
return cacheDirs;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
async findStigmergyTempFiles(tempDir) {
|
|
621
|
+
const tempFiles = [];
|
|
622
|
+
|
|
623
|
+
try {
|
|
624
|
+
const files = fs.readdirSync(tempDir, { withFileTypes: true });
|
|
625
|
+
|
|
626
|
+
for (const file of files) {
|
|
627
|
+
if (this.isStigmergyFile(file.name) ||
|
|
628
|
+
file.name.startsWith('stigmergy-') ||
|
|
629
|
+
file.name.includes('stigmergy')) {
|
|
630
|
+
const fullPath = path.join(tempDir, file.name);
|
|
631
|
+
tempFiles.push(fullPath);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
} catch (error) {
|
|
635
|
+
// Skip temp directories we can't read
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
return tempFiles;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
async cleanDirectory(dirPath) {
|
|
642
|
+
try {
|
|
643
|
+
if (!fs.existsSync(dirPath)) {
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
const files = await this.scanDirectory(dirPath);
|
|
648
|
+
const removed = await this.batchRemoveFiles(files);
|
|
649
|
+
|
|
650
|
+
console.log(` ๐งน Cleaned ${removed} files from ${path.basename(dirPath)}`);
|
|
651
|
+
} catch (error) {
|
|
652
|
+
console.error(` โ Failed to clean ${dirPath}: ${error.message}`);
|
|
653
|
+
this.results.errors.push(`Clean error ${dirPath}: ${error.message}`);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
matchPattern(filePath, pattern) {
|
|
658
|
+
const fileName = path.basename(filePath);
|
|
659
|
+
|
|
660
|
+
// Simple glob pattern matching
|
|
661
|
+
const regexPattern = pattern
|
|
662
|
+
.replace(/\*/g, '.*')
|
|
663
|
+
.replace(/\?/g, '.');
|
|
664
|
+
|
|
665
|
+
const regex = new RegExp(regexPattern, 'i');
|
|
666
|
+
return regex.test(fileName);
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
createBatches(items, batchSize) {
|
|
670
|
+
const batches = [];
|
|
671
|
+
for (let i = 0; i < items.length; i += batchSize) {
|
|
672
|
+
batches.push(items.slice(i, i + batchSize));
|
|
673
|
+
}
|
|
674
|
+
return batches;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
async parallelRemoveBatches(batches, maxConcurrency) {
|
|
678
|
+
let totalRemoved = 0;
|
|
679
|
+
const executing = [];
|
|
680
|
+
|
|
681
|
+
for (const batch of batches) {
|
|
682
|
+
const promise = this.batchRemoveFiles(batch);
|
|
683
|
+
executing.push(promise);
|
|
684
|
+
|
|
685
|
+
if (executing.length >= maxConcurrency) {
|
|
686
|
+
await Promise.race(executing);
|
|
687
|
+
executing.splice(0, 1);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
await Promise.all(executing);
|
|
692
|
+
return totalRemoved;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
formatBytes(bytes) {
|
|
696
|
+
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
|
|
697
|
+
if (bytes === 0) return '0 Bytes';
|
|
698
|
+
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
699
|
+
return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i];
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
logFiles(files, prefix = '') {
|
|
703
|
+
files.slice(0, 10).forEach(file => {
|
|
704
|
+
console.log(`${prefix}${path.basename(file)}`);
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
if (files.length > 10) {
|
|
708
|
+
console.log(`${prefix}... and ${files.length - 10} more`);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
printSummary() {
|
|
713
|
+
console.log('\n๐ CACHE CLEANING SUMMARY:');
|
|
714
|
+
console.log('=' .repeat(50));
|
|
715
|
+
|
|
716
|
+
if (this.options.dryRun) {
|
|
717
|
+
console.log('๐ DRY RUN MODE - No files were actually deleted');
|
|
718
|
+
} else {
|
|
719
|
+
console.log(`๐ Directories removed: ${this.results.directoriesRemoved}`);
|
|
720
|
+
console.log(`๐ Files removed: ${this.results.filesRemoved}`);
|
|
721
|
+
console.log(`๐พ Space freed: ${this.formatBytes(this.results.bytesFreed)}`);
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
if (this.results.skipped.length > 0) {
|
|
725
|
+
console.log(`โญ๏ธ Items skipped: ${this.results.skipped.length}`);
|
|
726
|
+
if (this.options.verbose) {
|
|
727
|
+
this.results.skipped.forEach(item => {
|
|
728
|
+
console.log(` ${item}`);
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
if (this.results.errors.length > 0) {
|
|
734
|
+
console.log(`โ Errors: ${this.results.errors.length}`);
|
|
735
|
+
this.results.errors.forEach(error => {
|
|
736
|
+
console.log(` ${error}`);
|
|
737
|
+
});
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
console.log('\nโ
Cache cleaning completed!');
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
module.exports = CacheCleaner;
|