"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.reinstallDependencies1 = exports.reinstallDependencies = exports.collectPackageDiff = void 0;

var _fs = _interopRequireDefault(require("fs"));

var _path = _interopRequireDefault(require("path"));

var _child_process = require("child_process");

var _gitRootDir = _interopRequireDefault(require("git-root-dir"));

var _log = require("./log");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

let packageJsons = {};

let packageDiffChecker = (pre, current) => {
  let {
    dependencies = {},
    devDependencies = {}
  } = current;
  let diffs = {};
  Object.keys(dependencies).forEach(pack => {
    let version = dependencies[pack];

    if (version !== pre.dependencies && pre.dependencies[pack]) {
      diffs[pack] = version;
    }
  });
  Object.keys(devDependencies).forEach(pack => {
    let version = devDependencies[pack];

    if (version !== pre.devDependencies && pre.devDependencies[pack]) {
      diffs[pack] = version;
    }
  });
  return diffs;
};

let collectPackageDiff = pathToSubProjects => new Promise(resolve => {
  (0, _gitRootDir.default)(process.cwd()).then(rootDir => {
    let projects = _fs.default.readdirSync(_path.default.join(rootDir, pathToSubProjects));

    projects.forEach(project => {
      let packageJson = _path.default.join(_path.default.join(rootDir, pathToSubProjects), project, 'package.json');

      if (_fs.default.existsSync(packageJson)) {
        packageJsons[project] = require(packageJson);
      }
    });
    resolve();
  });
});

exports.collectPackageDiff = collectPackageDiff;

let reinstallDependencies1 = pathToSubProjects => {
  (0, _gitRootDir.default)(process.cwd()).then(rootDir => {
    let projects = _fs.default.readdirSync(_path.default.join(rootDir, pathToSubProjects));

    projects.forEach(project => {
      let prePackageJson = packageJsons[project];
      let currentPackageJson;

      if (prePackageJson) {
        currentPackageJson = _path.default.join(_path.default.join(rootDir, pathToSubProjects), project, 'package.json');

        if (_fs.default.existsSync(currentPackageJson)) {
          currentPackageJson = require(currentPackageJson);
        }
      }

      let projPath = _path.default.join(_path.default.join(rootDir, pathToSubProjects), project);

      let diffs = packageDiffChecker(prePackageJson, currentPackageJson);

      if (Object.keys(diffs).length) {
        (0, _log.log)(diffs, `package diffs between branches for ${project} project`);
        (0, _child_process.spawnSync)('rm', ['-rf', 'package-lock.json'], {
          cwd: projPath,
          stdio: 'inherit'
        });
        let packages = Object.keys(diffs);
        let dummy = [];

        for (let name of packages) {
          let version = diffs[name];
          dummy.push(`${name}@${version}`);
        }

        (0, _log.log)(`npm ${['install'].concat(dummy).join(' ')}`);
        (0, _child_process.spawnSync)('npm', ['install'].concat(dummy), {
          cwd: projPath,
          stdio: 'inherit'
        });
      } else {
        (0, _child_process.spawnSync)('rm', ['-rf', 'node_modules', 'package-lock.json'], {
          cwd: projPath,
          stdio: 'inherit'
        });
        (0, _child_process.spawnSync)('npm', ['install'], {
          cwd: projPath,
          stdio: 'inherit'
        });
      }
    });
  });
};

exports.reinstallDependencies1 = reinstallDependencies1;

let reinstallDependencies = pathToSubProjects => new Promise(resolve => {
  (0, _gitRootDir.default)(process.cwd()).then(rootDir => {
    let projects = _fs.default.readdirSync(_path.default.join(rootDir, pathToSubProjects));

    projects.forEach(project => {
      let projPath = _path.default.join(_path.default.join(rootDir, pathToSubProjects), project);

      (0, _child_process.spawnSync)('npm', ['install'], {
        cwd: projPath,
        stdio: 'inherit'
      });
      resolve();
    });
  });
});

exports.reinstallDependencies = reinstallDependencies;