#!/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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
require("../fs/gracefulify");
const loudRejection = require("loud-rejection");
loudRejection();
const path = require("path");
const updateNotifier = require("update-notifier");
const camelcase = require("camelcase");
const isCI = require("is-ci");
const nopt = require("nopt");
const R = require("ramda");
const npm = require("npm");
const npmDefaults = require("npm/lib/config/defaults");
require("../logging/fileLogger");
const pnpmPkgJson_1 = require("../pnpmPkgJson");
const runNpm_1 = require("../cmd/runNpm");
const install_1 = require("../cmd/install");
const update_1 = require("../cmd/update");
const uninstall_1 = require("../cmd/uninstall");
const link_1 = require("../cmd/link");
const prune_1 = require("../cmd/prune");
const installTest_1 = require("../cmd/installTest");
const run_1 = require("../cmd/run");
const store_1 = require("../cmd/store");
const bole = require("bole");
const reporter_1 = require("../reporter");
bole.setFastTime();
const pnpmCmds = {
    install: install_1.default,
    update: update_1.default,
    uninstall: uninstall_1.default,
    link: link_1.default,
    prune: prune_1.default,
    'install-test': installTest_1.default,
    run: run_1.default,
    store: store_1.default,
};
const supportedCmds = new Set([
    'install',
    'uninstall',
    'update',
    'link',
    'prune',
    'install-test',
    'run',
    'store',
]);
function run(argv) {
    return __awaiter(this, void 0, void 0, function* () {
        const pnpmTypes = {
            'store-path': path,
            'local-registry': path,
            'global-path': path,
            'network-concurrency': Number,
            'fetching-concurrency': Number,
            'lock-stale-duration': Number,
            'lock': Boolean,
            'child-concurrency': Number,
            'offline': Boolean,
            'reporter': String,
        };
        const types = R.merge(npmDefaults.types, pnpmTypes);
        const cliConf = nopt(types, npmDefaults.shorthands, argv, 0 // argv should be already sliced by now
        );
        if (cliConf.version) {
            console.log(pnpmPkgJson_1.default.version);
            return;
        }
        if (!isCI) {
            updateNotifier({
                packageName: pnpmPkgJson_1.default.name,
                packageVersion: pnpmPkgJson_1.default.version
            }).notify();
        }
        const cmd = getCommandFullName(cliConf.argv.remain[0]);
        if (!supportedCmds.has(cmd)) {
            runNpm_1.default(argv);
            return Promise.resolve();
        }
        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;
        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.globalPrefix = path.join(npm['globalPrefix'], 'pnpm-global');
        opts.prefix = opts.global ? opts.globalPrefix : npm.prefix;
        reporter_1.default(silent ? 'silent' : (opts.reporter || 'default')); // tslint:disable-line
        const cliArgs = cliConf.argv.remain.slice(1);
        return pnpmCmds[cmd](cliArgs, opts);
    });
}
function getCommandFullName(cmd) {
    switch (cmd) {
        case 'install':
        case 'i':
            return 'install';
        case 'uninstall':
        case 'r':
        case 'rm':
        case 'un':
        case 'unlink':
            return 'uninstall';
        case 'link':
        case 'ln':
            return 'link';
        case 'install-test':
        case 'it':
            return 'install-test';
        // some commands have no aliases: publish, prune
        default:
            return cmd;
    }
}
exports.default = run;
const err_1 = require("../err");
if (!module.parent)
    run(process.argv.slice(2)).catch(err_1.default);
//# sourceMappingURL=pnpm.js.map