#!/usr/bin/env node
"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());
    });
};
const fs = require("fs");
const gfs = require("graceful-fs");
gfs.gracefulify(fs);
const loudRejection = require("loud-rejection");
loudRejection();
const camelcase = require("camelcase");
const isCI = require("is-ci");
const nopt = require("nopt");
const npm = require("not-bundled-npm");
const npmDefaults = require("not-bundled-npm/lib/config/defaults");
const path = require("path");
const R = require("ramda");
const checkForUpdates_1 = require("../checkForUpdates");
const pnpmCmds = require("../cmd");
const runNpm_1 = require("../cmd/runNpm");
const getCommandFullName_1 = require("../getCommandFullName");
require("../logging/fileLogger");
const pnpmPkgJson_1 = require("../pnpmPkgJson");
const reporter_1 = require("../reporter");
pnpmCmds['install-test'] = pnpmCmds.installTest;
const supportedCmds = new Set([
    'add',
    'install',
    'uninstall',
    'update',
    'link',
    'prune',
    'install-test',
    'run',
    'store',
    'list',
    'dislink',
    'help',
    'root',
    'outdated',
    'rebuild',
]);
const passedThroughCmds = new Set([
    'access',
    'adduser',
    'bin',
    'bugs',
    'c',
    'config',
    'deprecate',
    'dist-tag',
    'docs',
    'edit',
    'get',
    'init',
    'login',
    'logout',
    'owner',
    'pack',
    'ping',
    'prefix',
    'profile',
    'publish',
    'repo',
    'restart',
    's',
    'se',
    'search',
    'set',
    'star',
    'stars',
    'start',
    'stop',
    'team',
    't',
    'tst',
    'test',
    'token',
    'unpublish',
    'unstar',
    'v',
    'version',
    'view',
    'whoami',
]);
function run(argv) {
    return __awaiter(this, void 0, void 0, function* () {
        if (argv.indexOf('--help') !== -1 || argv.indexOf('-h') !== -1 || argv.indexOf('--h') !== -1) {
            argv = ['help'].concat(argv);
        }
        const pnpmTypes = {
            'child-concurrency': Number,
            'fetching-concurrency': Number,
            'global-path': path,
            'independent-leaves': Boolean,
            'lock': Boolean,
            'lock-stale-duration': Number,
            'network-concurrency': Number,
            'offline': Boolean,
            'reporter': String,
            'store': path,
            'store-path': path,
            'verify-store-integrity': Boolean,
        };
        const types = R.merge(npmDefaults.types, pnpmTypes);
        const cliConf = nopt(types, npmDefaults.shorthands, argv, 0);
        if (cliConf.version) {
            console.log(pnpmPkgJson_1.default.version);
            return;
        }
        if (!isCI) {
            checkForUpdates_1.default();
        }
        let cmd = getCommandFullName_1.default(cliConf.argv.remain[0]) || 'help';
        if (!supportedCmds.has(cmd)) {
            if (passedThroughCmds.has(cmd)) {
                runNpm_1.default(argv);
                return Promise.resolve();
            }
            cmd = 'help';
        }
        if (cliConf['dry-run']) {
            console.error(`Error: 'dry-run' is not supported yet, sorry!`);
            process.exit(1);
        }
        cliConf.save = cliConf.save || !cliConf.saveDev && !cliConf.saveOptional;
        if (!cliConf['user-agent']) {
            cliConf['user-agent'] = `${pnpmPkgJson_1.default.name}/${pnpmPkgJson_1.default.version} npm/? node/${process.version} ${process.platform} ${process.arch}`;
        }
        const force = cliConf.force === true;
        // removing force to avoid redundant logs from npm
        // see issue #878 and #877
        delete cliConf.force;
        yield new Promise((resolve, reject) => {
            npm.load(cliConf, (err) => {
                if (err) {
                    reject(err);
                    return;
                }
                resolve();
            });
        });
        const silent = npm.config.get('loglevel') === 'silent' || !npm.config.get('reporter') && isCI;
        const opts = R.fromPairs(R.keys(types).map(configKey => [camelcase(configKey), npm.config.get(configKey)])); // tslint:disable-line
        opts.rawNpmConfig = Object.assign.apply(Object, npm.config.list.reverse());
        opts.bin = npm.bin;
        opts.globalBin = npm.globalBin;
        opts.globalPrefix = path.join(npm.globalPrefix, 'pnpm-global');
        opts.prefix = opts.global ? opts.globalPrefix : npm.prefix;
        opts.packageManager = pnpmPkgJson_1.default;
        opts.force = force;
        if (opts.only === 'prod' || opts.only === 'production' || !opts.only && opts.production) {
            opts.production = true;
            opts.development = false;
            opts.optional = true;
        }
        else if (opts.only === 'dev' || opts.only === 'development') {
            opts.production = false;
            opts.development = true;
            opts.optional = false;
        }
        else {
            opts.production = true;
            opts.development = true;
            opts.optional = true;
        }
        reporter_1.default(silent ? 'silent' : (opts.reporter || 'default')); // tslint:disable-line
        // `pnpm install ""` is going to be just `pnpm install`
        const cliArgs = cliConf.argv.remain.slice(1).filter(Boolean);
        return pnpmCmds[cmd](cliArgs, opts, cliConf.argv.remain[0]);
    });
}
const err_1 = require("../err");
if (!module.parent)
    run(process.argv.slice(2)).catch(err_1.default);
module.exports = run;
//# sourceMappingURL=pnpm.js.map