matterbridge-roborock-vacuum-plugin
Version:
Matterbridge Roborock Vacuum Plugin
59 lines (47 loc) • 1.27 kB
JavaScript
/* eslint-disable no-console, n/no-process-exit */
import { existsSync, rmSync, statSync } from 'node:fs';
import { resolve } from 'node:path';
const args = process.argv.slice(2);
if (args.length === 0) {
console.log('CLEANUP: none (no paths provided)');
process.exit(0);
}
function isSafeWorkspacePath(path) {
const normalized = path.replace(/\\/g, '/');
if (normalized.includes('..')) {
return false;
}
return normalized === 'workspace' || normalized.startsWith('workspace/');
}
const removed = [];
const skipped = [];
for (const raw of args) {
const path = raw.replace(/\/$/, '');
if (!isSafeWorkspacePath(path)) {
skipped.push(`${path} (rejected: must be under workspace/)`);
continue;
}
const absolute = resolve(path);
if (!existsSync(absolute)) {
skipped.push(`${path} (not found)`);
continue;
}
const stat = statSync(absolute);
if (stat.isDirectory()) {
rmSync(absolute, { recursive: true, force: true });
removed.push(`${path}/`);
} else {
rmSync(absolute);
removed.push(path);
}
}
if (skipped.length > 0) {
console.log('CLEANUP SKIPPED:');
console.log(skipped.join('\n'));
}
if (removed.length === 0) {
console.log('CLEANUP: none');
process.exit(0);
}
console.log('CLEANUP:');
console.log(removed.join('\n'));