// engine/plugin-installer/artifact-scanner.ts — Scan and cleanup plugin artifacts (SPEC-604)
import { readdir, unlink } from 'node:fs/promises';
import { join } from 'node:path';
const ARTIFACT_DIRS = ['.claude/rules', '.claude/skills', '.claude/hooks'];
function buildArtifactPattern(pluginId) {
    return `plugin-${pluginId}`;
}
async function listFilesInDir(dirPath) {
    try {
        const entries = await readdir(dirPath, { withFileTypes: true });
        return entries.filter((e) => e.isFile()).map((e) => join(dirPath, e.name));
    }
    catch {
        // Directory does not exist or is not readable — treat as empty
        return [];
    }
}
/**
 * Scans .claude/rules/, .claude/skills/, and .claude/hooks/ for files whose name
 * contains `plugin-{pluginId}`.
 *
 * @param pluginId - The plugin identifier (without the `plugin-` prefix)
 * @param projectPath - Absolute path to the project root
 * @returns Absolute paths of matching artifact files
 */
export async function scanPluginArtifacts(pluginId, projectPath) {
    const pattern = buildArtifactPattern(pluginId);
    const found = [];
    for (const dir of ARTIFACT_DIRS) {
        const fullDir = join(projectPath, dir);
        const files = await listFilesInDir(fullDir);
        for (const filePath of files) {
            const fileName = filePath.split('/').at(-1) ?? '';
            if (fileName.includes(pattern)) {
                found.push(filePath);
            }
        }
    }
    return found;
}
/**
 * Scans for plugin artifacts and deletes them.
 *
 * @param pluginId - The plugin identifier (without the `plugin-` prefix)
 * @param projectPath - Absolute path to the project root
 * @returns Absolute paths of files that were deleted
 */
export async function cleanupPluginArtifacts(pluginId, projectPath) {
    const artifacts = await scanPluginArtifacts(pluginId, projectPath);
    await Promise.all(artifacts.map((filePath) => unlink(filePath)));
    return artifacts;
}
//# sourceMappingURL=artifact-scanner.js.map