"use strict";
// TODO: move to separate package. It is used in supi/lib/install.ts as well
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 lifecycle_1 = require("@pnpm/lifecycle");
const logger_1 = require("@pnpm/logger");
const pLimit = require("p-limit");
const R = require("ramda");
const constants_1 = require("./constants");
exports.default = (depGraph, opts) => __awaiter(this, void 0, void 0, function* () {
    // postinstall hooks
    const limitChild = pLimit(opts.childConcurrency || 4);
    // TODO: run depencies first then dependents. Use graph-sequencer to sort the graph
    yield Promise.all(R.keys(depGraph)
        .filter((depPath) => !depGraph[depPath].isBuilt)
        .map((depPath) => limitChild(() => __awaiter(this, void 0, void 0, function* () {
        const depNode = depGraph[depPath];
        try {
            const hasSideEffects = yield lifecycle_1.runPostinstallHooks({
                pkgId: depPath,
                pkgRoot: depNode.peripheralLocation,
                rawNpmConfig: opts.rawNpmConfig,
                rootNodeModulesDir: opts.prefix,
                unsafePerm: opts.unsafePerm || false,
            });
            if (hasSideEffects && opts.sideEffectsCache && !opts.sideEffectsCacheReadonly) {
                try {
                    yield opts.storeController.upload(depNode.peripheralLocation, {
                        engine: constants_1.ENGINE_NAME,
                        pkgId: depNode.pkgId,
                    });
                }
                catch (err) {
                    if (err && err.statusCode === 403) {
                        logger_1.default.warn(`The store server disabled upload requests, could not upload ${depNode.pkgId}`);
                    }
                    else {
                        logger_1.default.warn({
                            err,
                            message: `An error occurred while uploading ${depNode.pkgId}`,
                        });
                    }
                }
            }
        }
        catch (err) {
            if (depNode.optional) {
                logger_1.default.warn({
                    err,
                    message: `Skipping failed optional dependency ${depNode.pkgId}`,
                });
                return;
            }
            throw err;
        }
    }))));
});
//# sourceMappingURL=runDependenciesScripts.js.map