"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.decachePlugin = exports.decacheModule = void 0;
const path_1 = require("path");
function decachePlugin(pluginPath, libraryPath) {
    const moduleName = find(libraryPath);
    if (!moduleName)
        return;
    searchCache(moduleName, function (mod) {
        delete require.cache[mod.id];
    });
    removeCachedPath(pluginPath);
}
exports.decachePlugin = decachePlugin;
function decacheModule(name) {
    const moduleName = find(name);
    if (!moduleName)
        return;
    searchCache(moduleName, function (mod) {
        delete require.cache[mod.id];
    });
    removeCachedPath(moduleName);
}
exports.decacheModule = decacheModule;
function find(moduleName) {
    try {
        return require.resolve(moduleName);
    }
    catch (_a) {
        return '';
    }
}
function searchCache(moduleName, callback) {
    const resolvedModule = require.resolve(moduleName);
    let mod;
    const visited = {};
    if (resolvedModule && ((mod = require.cache[resolvedModule]) !== undefined)) {
        (function run(current) {
            visited[current.id] = true;
            current.children.forEach(function (child) {
                if ((0, path_1.extname)(child.filename) !== '.node' && !visited[child.id]) {
                    run(child);
                }
            });
            callback(current);
        })(mod);
    }
}
;
function removeCachedPath(pluginPath) {
    const pathCache = module.constructor._pathCache;
    Object.keys(pathCache).forEach(function (cacheKey) {
        if (cacheKey.includes(pluginPath)) {
            delete pathCache[cacheKey];
        }
    });
}
//# sourceMappingURL=decache.js.map