"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 camelcase = require("camelcase");
const loadNpmConf = require("npm-conf");
const npmTypes = require("npm-conf/lib/types");
const path = require("path");
const whichcb = require("which");
function which(cmd) {
    return new Promise((resolve, reject) => {
        whichcb(cmd, (err, resolvedPath) => err ? reject(err) : resolve(resolvedPath));
    });
}
exports.types = Object.assign({
    'background': Boolean,
    'child-concurrency': Number,
    'fetching-concurrency': Number,
    'global-path': path,
    'ignore-pnpmfile': Boolean,
    'ignore-stop-requests': Boolean,
    'ignore-upload-requests': Boolean,
    'independent-leaves': Boolean,
    'lock': Boolean,
    'lock-stale-duration': Number,
    'network-concurrency': Number,
    'offline': Boolean,
    'package-import-method': ['auto', 'hardlink', 'reflink', 'copy'],
    'pending': Boolean,
    'port': Number,
    'prefer-offline': Boolean,
    'protocol': ['auto', 'tcp', 'ipc'],
    'reporter': String,
    'shamefully-flatten': Boolean,
    'shrinkwrap-only': Boolean,
    'side-effects-cache': Boolean,
    'side-effects-cache-readonly': Boolean,
    'store': path,
    'store-path': path,
    'use-store-server': Boolean,
    'verify-store-integrity': Boolean,
}, npmTypes.types);
exports.default = (opts) => __awaiter(this, void 0, void 0, function* () {
    const packageManager = opts && opts.packageManager || { name: 'pnpm', version: 'undefined' };
    const cliArgs = opts && opts.cliArgs || {};
    // This is what npm does as well, overriding process.execPath with the resolved location of Node.
    // The value of process.execPath is changed only for the duration of config initialization.
    // Otherwise, npmConfig.globalPrefix would sometimes have the bad location.
    const originalExecPath = process.execPath;
    try {
        const node = yield which(process.argv[0]);
        if (node.toUpperCase() !== process.execPath.toUpperCase()) {
            process.execPath = node;
        }
    }
    catch (err) { } // tslint:disable-line:no-empty
    const npmConfig = loadNpmConf();
    process.execPath = originalExecPath;
    if (!cliArgs['user-agent']) {
        cliArgs['user-agent'] = `${packageManager.name}/${packageManager.version} npm/? node/${process.version} ${process.platform} ${process.arch}`;
    }
    const pnpmConfig = Object.keys(exports.types) // tslint:disable-line
        .reduce((acc, configKey) => {
        acc[camelcase(configKey)] = typeof cliArgs[configKey] !== 'undefined'
            ? cliArgs[configKey]
            : npmConfig.get(configKey);
        return acc;
    }, {});
    pnpmConfig.rawNpmConfig = Object.assign.apply(Object, npmConfig.list.reverse().concat([cliArgs]));
    pnpmConfig.globalBin = process.platform === 'win32'
        ? npmConfig.globalPrefix
        : path.resolve(npmConfig.globalPrefix, 'bin');
    pnpmConfig.bin = pnpmConfig.global
        ? pnpmConfig.globalBin
        : path.join(npmConfig.localPrefix, 'node_modules', '.bin');
    pnpmConfig.globalPrefix = path.join(npmConfig.globalPrefix, 'pnpm-global');
    pnpmConfig.prefix = pnpmConfig.global ? pnpmConfig.globalPrefix : npmConfig.prefix;
    pnpmConfig.packageManager = packageManager;
    if (pnpmConfig.only === 'prod' || pnpmConfig.only === 'production' || !pnpmConfig.only && pnpmConfig.production) {
        pnpmConfig.production = true;
        pnpmConfig.development = false;
    }
    else if (pnpmConfig.only === 'dev' || pnpmConfig.only === 'development') {
        pnpmConfig.production = false;
        pnpmConfig.development = true;
        pnpmConfig.optional = false;
    }
    else {
        pnpmConfig.production = true;
        pnpmConfig.development = true;
    }
    if (!pnpmConfig.packageLock && pnpmConfig.shrinkwrap) {
        pnpmConfig.shrinkwrap = false;
    }
    return pnpmConfig;
});
//# sourceMappingURL=index.js.map