"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBinaryEntry = exports.createConfig = exports.PACT_STANDALONE_VERSION = void 0;
var chalk = require("chalk");
var path = require("path");
var fs = require("fs");
exports.PACT_STANDALONE_VERSION = '1.88.63';
var PACT_DEFAULT_LOCATION = "https://github.com/pact-foundation/pact-ruby-standalone/releases/download/v" + exports.PACT_STANDALONE_VERSION + "/";
var HTTP_REGEX = /^http(s?):\/\//;
function throwError(msg) {
    throw new Error(chalk.red("Error while installing binary: " + msg));
}
function getBinaryLocation(location, basePath) {
    if (!location || location.length === 0) {
        return undefined;
    }
    return HTTP_REGEX.test(location)
        ? location
        : path.resolve(basePath, location);
}
function findPackageConfig(location, tries) {
    if (tries === void 0) { tries = 10; }
    if (tries === 0) {
        return {};
    }
    var packagePath = path.resolve(location, 'package.json');
    if (fs.existsSync(packagePath)) {
        var config = require(packagePath).config;
        if (config && (config.pact_binary_location || config.pact_do_not_track)) {
            return {
                binaryLocation: getBinaryLocation(config.pact_binary_location, location),
                doNotTrack: config.pact_do_not_track,
            };
        }
    }
    return findPackageConfig(path.resolve(location, '..'), tries - 1);
}
function createConfig(location) {
    if (location === void 0) { location = process.cwd(); }
    var packageConfig = findPackageConfig(location);
    var PACT_BINARY_LOCATION = packageConfig.binaryLocation || PACT_DEFAULT_LOCATION;
    var CHECKSUM_SUFFIX = '.checksum';
    return {
        doNotTrack: packageConfig.doNotTrack ||
            process.env.PACT_DO_NOT_TRACK !== undefined ||
            false,
        binaries: [
            {
                platform: 'win32',
                binary: "pact-" + exports.PACT_STANDALONE_VERSION + "-win32.zip",
                binaryChecksum: "pact-" + exports.PACT_STANDALONE_VERSION + "-win32.zip" + CHECKSUM_SUFFIX,
                downloadLocation: PACT_BINARY_LOCATION,
                folderName: "win32-" + exports.PACT_STANDALONE_VERSION,
            },
            {
                platform: 'darwin',
                binary: "pact-" + exports.PACT_STANDALONE_VERSION + "-osx.tar.gz",
                binaryChecksum: "pact-" + exports.PACT_STANDALONE_VERSION + "-osx.tar.gz" + CHECKSUM_SUFFIX,
                downloadLocation: PACT_BINARY_LOCATION,
                folderName: "darwin-" + exports.PACT_STANDALONE_VERSION,
            },
            {
                platform: 'linux',
                arch: 'x64',
                binary: "pact-" + exports.PACT_STANDALONE_VERSION + "-linux-x86_64.tar.gz",
                binaryChecksum: "pact-" + exports.PACT_STANDALONE_VERSION + "-linux-x86_64.tar.gz" + CHECKSUM_SUFFIX,
                downloadLocation: PACT_BINARY_LOCATION,
                folderName: "linux-x64-" + exports.PACT_STANDALONE_VERSION,
            },
            {
                platform: 'linux',
                arch: 'ia32',
                binary: "pact-" + exports.PACT_STANDALONE_VERSION + "-linux-x86.tar.gz",
                binaryChecksum: "pact-" + exports.PACT_STANDALONE_VERSION + "-linux-x86.tar.gz" + CHECKSUM_SUFFIX,
                downloadLocation: PACT_BINARY_LOCATION,
                folderName: "linux-ia32-" + exports.PACT_STANDALONE_VERSION,
            },
        ],
    };
}
exports.createConfig = createConfig;
var CONFIG = createConfig();
function getBinaryEntry(platform, arch) {
    platform = platform || process.platform;
    arch = arch || process.arch;
    for (var _i = 0, _a = CONFIG.binaries; _i < _a.length; _i++) {
        var value = _a[_i];
        if (value.platform === platform &&
            (value.arch ? value.arch === arch : true)) {
            return value;
        }
    }
    throw throwError("Cannot find binary for platform '" + platform + "' with architecture '" + arch + "'.");
}
exports.getBinaryEntry = getBinaryEntry;
//# sourceMappingURL=install.js.map