text: "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 pretty_task_1 = require("../util/pretty-task");
  const async_1 = require("../util/async");
  const dep_json_fs_1 = require("./dep-json-fs");
  const dep_objs_1 = require("./dep-objs");
  const path_1 = require("path");
  const install_utils_1 = require("./install-utils");
  function installPkgsViaLocalPath(pkgs, monoRepo, doRoot, forceCi, npmRunTimeArgs) {
      let { innerPkgsByName } = monoRepo;
      let undoTransformFuncs = [];
      let transformPromises = monoRepo.innerPkgs.map((pkg) => __awaiter(this, void 0, void 0, function* () {
          let undo = yield dep_json_fs_1.addDepEntries(pkg, transformToLocalePaths(pkg, innerPkgsByName));
          undoTransformFuncs.push(undo);
      }));
      let installTasks = pkgs.map((pkg) => ({
          label: pkg.readableId(),
          func: () => __awaiter(this, void 0, void 0, function* () {
              return install_utils_1.runPkgInstall(monoRepo, pkg, forceCi, npmRunTimeArgs, false);
          })
      }));
      if (doRoot && monoRepo.rootPkg) {
          installTasks.unshift({
              label: 'root',
              func: () => {
                  return install_utils_1.runPkgInstall(monoRepo, monoRepo.rootPkg, forceCi, npmRunTimeArgs, true);
              }
          });
      }
      function undoTransforms() {
          return async_1.runParallel(undoTransformFuncs);
      }
      return async_1.allSettled(transformPromises).then(() => {
          return pretty_task_1.runPrettyParallel(installTasks); // and THEN run installs
      }).finally(undoTransforms); // and then undo all package.json transforms
  }
  exports.installPkgsViaLocalPath = installPkgsViaLocalPath;
  function transformToLocalePaths(subjectPkg, innerPkgsByName) {
      return dep_objs_1.filterDeps(subjectPkg.jsonData, (pkgName, versionRange) => {
          let innerPkg = innerPkgsByName[pkgName];
          if (innerPkg) {
              return 'file:' + path_1.relative(subjectPkg.dir, innerPkg.dir);
          }
          else { // an external package. keep as-is
              return versionRange;
          }
      });
  }
  