"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const rimraf = require("rimraf-then");
const path = require("path");
const binify_1 = require("./binify");
const safeReadPkg_1 = require("./fs/safeReadPkg");
function removeTopDependency(dependencyName, modules) {
    return __awaiter(this, void 0, void 0, function* () {
        return Promise.all([
            removeBins(dependencyName, modules),
            rimraf(path.join(modules, dependencyName)),
        ]);
    });
}
exports.default = removeTopDependency;
function removeBins(uninstalledPkg, modules) {
    return __awaiter(this, void 0, void 0, function* () {
        const uninstalledPkgPath = path.join(modules, uninstalledPkg);
        const uninstalledPkgJson = yield safeReadPkg_1.fromDir(uninstalledPkgPath);
        if (!uninstalledPkgJson)
            return;
        const cmds = yield binify_1.default(uninstalledPkgJson, uninstalledPkgPath);
        return Promise.all(cmds.map(cmd => path.join(modules, '.bin', cmd.name)).map(rimraf));
    });
}
//# sourceMappingURL=removeTopDependency.js.map