"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 logger_1 = require("@pnpm/logger");
const execa = require("execa");
const pLimit = require("p-limit");
const dividePackagesToChunks_1 = require("./dividePackagesToChunks");
exports.default = (pkgs, args, cmd, opts) => __awaiter(this, void 0, void 0, function* () {
    const { chunks } = dividePackagesToChunks_1.default(pkgs);
    const limitRun = pLimit(opts.concurrency);
    for (const chunk of chunks) {
        yield Promise.all(chunk.map((prefix) => limitRun(() => __awaiter(this, void 0, void 0, function* () {
            try {
                yield execa(args[0], args.slice(1), { cwd: prefix, stdio: 'inherit' });
            }
            catch (err) {
                logger_1.default.info(err);
                err['prefix'] = prefix; // tslint:disable-line:no-string-literal
                throw err;
            }
        }))));
    }
});
//# sourceMappingURL=exec.js.map