#!/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 rc = require("rc");
const meow = require("meow");
const updateNotifier = require("update-notifier");
const camelcaseKeys = require("camelcase-keys");
const isCI = require("is-ci");
const common_tags_1 = require("common-tags");
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 publish_1 = require("../cmd/publish");
const prune_1 = require("../cmd/prune");
const installTest_1 = require("../cmd/installTest");
const run_1 = require("../cmd/run");
const verify_1 = require("../cmd/verify");
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,
    publish: publish_1.default,
    prune: prune_1.default,
    'install-test': installTest_1.default,
    run: run_1.default,
    verify: verify_1.default,
};
const supportedCmds = new Set([
    'install',
    'uninstall',
    'update',
    'help',
    'link',
    'publish',
    'prune',
    'install-test',
    'run',
    'verify',
]);
function run(argv) {
    return __awaiter(this, void 0, void 0, function* () {
        const cli = meow({
            argv: argv,
            help: common_tags_1.stripIndent `
      Usage:
        $ pnpm install
        $ pnpm install <name>
        $ pnpm uninstall
        $ pnpm uninstall <name>

      Options:
        -S, --save            save into package.json under dependencies
        -D, --save-dev        save into package.json under devDependencies
        -O, --save-optional   save into package.json under optionalDependencies
        -E, --save-exact      save exact spec

            --dry-run         simulate
        -g, --global          install globally

            --production      don't install devDependencies
            --silent           don't print progress`
        }, {
            boolean: [
                'save-dev',
                'save',
                'save-exact',
                'save-optional',
                'dry-run',
                'global',
                'silent',
                'debug',
                'flat-tree',
            ],
            alias: {
                quiet: 'silent',
                'no-progress': 'silent',
                D: 'save-dev',
                S: 'save',
                E: 'save-exact',
                O: 'save-optional',
                g: 'global',
                v: 'version',
                f: 'force',
            }
        });
        if (!isCI) {
            updateNotifier({
                packageName: pnpmPkgJson_1.default.name,
                packageVersion: pnpmPkgJson_1.default.version
            }).notify();
        }
        const cmd = getCommandFullName(cli.input[0]);
        if (!supportedCmds.has(cmd)) {
            runNpm_1.default(argv);
            return Promise.resolve();
        }
        cli.flags.silent = cli.flags.silent || cli.flags.debug || isCI;
        ['dryRun'].forEach(flag => {
            if (cli.flags[flag]) {
                console.error(`Error: '${flag}' is not supported yet, sorry!`);
                process.exit(1);
            }
        });
        const opts = Object.assign({}, getRC('npm'), getRC('pnpm'));
        if (opts.depth === 'Infinity') {
            opts.depth = Infinity;
        }
        // This is needed because the arg values should be used only if they were passed
        Object.keys(cli.flags).forEach(key => {
            opts[key] = opts[key] || cli.flags[key];
        });
        if (!opts.silent)
            reporter_1.default(opts.reporter || 'default');
        const cliArgs = cli.input.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 'help':
            return 'help';
        case 'link':
        case 'ln':
            return 'link';
        case 'install-test':
        case 'it':
            return 'install-test';
        // some commands have no aliases: publish, prune
        default:
            return cmd;
    }
}
function getRC(appName) {
    return camelcaseKeys(rc(appName));
}
exports.default = run;
const err_1 = require("../err");
if (!module.parent)
    run(process.argv.slice(2)).catch(err_1.default);
//# sourceMappingURL=pnpm.js.map