"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLernaGraph = void 0;
const lerna_1 = require("../lerna");
const child_process_1 = require("./child-process");
exports.getLernaGraph = (projectRoot, scheduler, config, logger, defaultPort = 3001) => __awaiter(void 0, void 0, void 0, function* () {
    const packages = JSON.parse(yield child_process_1.execCmd('npx', ['lerna', 'la', '--json'], { cwd: projectRoot }, 'debug', 'debug', logger));
    const rawGraph = JSON.parse(yield child_process_1.execCmd('npx', ['lerna', 'la', '--graph'], { cwd: projectRoot }, 'debug', 'debug', logger));
    const names = new Set(packages.map((p) => p.name));
    const resolvePackage = (name) => {
        const pkg = packages.find((p) => p.name === name);
        const children = rawGraph[name].filter((n) => names.has(n));
        return Object.assign(Object.assign({}, pkg), { dependencies: children });
    };
    return new lerna_1.LernaGraph(scheduler, Object.keys(rawGraph)
        .filter((n) => names.has(n))
        .map((n) => resolvePackage(n)), projectRoot, config, logger, defaultPort);
});
//# sourceMappingURL=get-lerna-graph.js.map