ktg-plugin-marketplace/plugins/llm-security-copilot/scanners/lib/fs-utils.mjs
Kjell Tore Guttormsen f418a8fe08 feat(llm-security-copilot): port llm-security v5.1.0 to GitHub Copilot CLI
Full port of llm-security plugin for internal use on Windows with GitHub
Copilot CLI. Protocol translation layer (copilot-hook-runner.mjs)
normalizes Copilot camelCase I/O to Claude Code snake_case format — all
original hook scripts run unmodified.

- 8 hooks with protocol translation (stdin/stdout/exit code)
- 18 SKILL.md skills (Agent Skills Open Standard)
- 6 .agent.md agent definitions
- 20 scanners + 14 scanner lib modules (unchanged)
- 14 knowledge files (unchanged)
- 39 test files including copilot-port-verify.mjs (17 tests)
- Windows-ready: node:path, os.tmpdir(), process.execPath, no bash

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-09 21:56:10 +02:00

66 lines
2.2 KiB
JavaScript

#!/usr/bin/env node
// fs-utils.mjs — Cross-platform file operations for /security clean
// Usage:
// node fs-utils.mjs backup <target> → prints backup path to stdout
// node fs-utils.mjs restore <backup> <target> → restores backup over target
// node fs-utils.mjs cleanup <backup> → removes backup directory
// node fs-utils.mjs tmppath <filename> → prints cross-platform temp file path
import { cpSync, rmSync, renameSync, existsSync } from 'node:fs';
import { join, basename } from 'node:path';
import { tmpdir } from 'node:os';
import { randomUUID } from 'node:crypto';
const [,, command, ...args] = process.argv;
switch (command) {
case 'backup': {
const target = args[0];
if (!target || !existsSync(target)) {
console.error(`backup: target does not exist: ${target}`);
process.exit(1);
}
const ts = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
const backupPath = `${target}.security-backup-${ts}`;
cpSync(target, backupPath, { recursive: true });
process.stdout.write(backupPath + '\n');
break;
}
case 'restore': {
const [backup, target] = args;
if (!backup || !existsSync(backup)) {
console.error(`restore: backup does not exist: ${backup}`);
process.exit(1);
}
if (target && existsSync(target)) {
rmSync(target, { recursive: true, force: true });
}
renameSync(backup, target);
process.stdout.write(`Restored ${backup}${target}\n`);
break;
}
case 'cleanup': {
const path = args[0];
if (path && existsSync(path)) {
rmSync(path, { recursive: true, force: true });
process.stdout.write(`Removed ${path}\n`);
}
break;
}
case 'tmppath': {
const base = args[0] || 'llm-security-temp.json';
const dotIdx = base.lastIndexOf('.');
const name = dotIdx > 0 ? base.slice(0, dotIdx) : base;
const ext = dotIdx > 0 ? base.slice(dotIdx) : '.json';
const unique = `${name}-${randomUUID().slice(0, 8)}${ext}`;
process.stdout.write(join(tmpdir(), unique) + '\n');
break;
}
default:
console.error('Usage: node fs-utils.mjs <backup|restore|cleanup|tmppath> [args...]');
process.exit(1);
}