#!/usr/bin/env node
"use strict";
if (~process.argv.indexOf('--debug')) {
    process.env.DEBUG = 'pnpm:*';
    process.argv.push('--silent');
}
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('../fileLogger');
const pnpmPkgJson_1 = require('../pnpmPkgJson');
const runNpm_1 = require('../cmd/runNpm');
const install_1 = require('../cmd/install');
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 cache_1 = require('../cmd/cache');
const pnpmCmds = {
    install: install_1.default,
    uninstall: uninstall_1.default,
    link: link_1.default,
    publish: publish_1.default,
    prune: prune_1.default,
    'install-test': installTest_1.default,
    cache: cache_1.default,
};
const supportedCmds = new Set([
    'install',
    'uninstall',
    'help',
    'link',
    'publish',
    'prune',
    'install-test',
    'cache',
]);
function run(argv) {
    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
            --debug           print verbose debug message`
    }, {
        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'
        }
    });
    if (!isCI) {
        updateNotifier({ pkg: pnpmPkgJson_1.default }).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'));
    // 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];
    });
    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));
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = run;
const err_1 = require('../err');
if (!module.parent)
    run(process.argv.slice(2)).catch(err_1.default);
//# sourceMappingURL=pnpm.js.map