"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.hasGitDependencies = void 0;
const path_1 = require("path");
const fs_extra_1 = require("fs-extra");
const ramda_1 = require("ramda");
const npa = require("npm-package-arg");
const WizError_1 = require("../modules/WizError");
const I18n_1 = require("../modules/I18n");
const GIT_PREFIX = 'git+';
function hasGitDependencies(cwd, args = []) {
    return __awaiter(this, void 0, void 0, function* () {
        if (validateGitDependencies(args)) {
            return true;
        }
        const packageJsonPath = (0, path_1.join)(cwd, 'package.json');
        const { dependencies = {}, devDependencies = {} } = yield (0, fs_extra_1.readJson)(packageJsonPath);
        const deps = Object.assign(Object.assign({}, dependencies), devDependencies);
        return args.length
            ? hasGitDepsExcludingArgs(args, deps)
            : validateGitDependencies(Object.values(deps));
    });
}
exports.hasGitDependencies = hasGitDependencies;
function validateGitDependencies(array) {
    return array.some(isGitDependency);
}
function hasGitDepsExcludingArgs(args, dependenciesDictionary) {
    const argsDeps = args.map(getRegistryDependencyName);
    const gitDeps = Object.entries(dependenciesDictionary)
        .filter(([, value]) => isGitDependency(value))
        .map(([key]) => key);
    return !!gitDeps.length && !!(0, ramda_1.without)(argsDeps, gitDeps).length;
}
function getRegistryDependencyName(pkg) {
    try {
        const { name } = npa(pkg);
        if (!name) {
            throw new Error('Package spec is invalid!');
        }
        return name;
    }
    catch (error) {
        throw new WizError_1.WizError(I18n_1.default.error('PACKAGE_NAME_NOT_EXISTS', pkg));
    }
}
function isGitDependency(dependency) {
    return dependency.startsWith(GIT_PREFIX);
}
//# sourceMappingURL=hasGitDependencies.js.map