"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// Map SIGINT & SIGTERM to process exit
// so that lockfiles are removed automatically
process
    .once('SIGINT', () => process.exit(0))
    .once('SIGTERM', () => process.exit(0));
// Patch the global fs module here at the app level
const chalk_1 = require("chalk");
const fs = require("fs");
const gfs = require("graceful-fs");
gfs.gracefulify(fs);
const loud_rejection_1 = require("loud-rejection");
loud_rejection_1.default();
const config_1 = require("@pnpm/config");
const logger_1 = require("@pnpm/logger");
const isCI = require("is-ci");
const nopt = require("nopt");
const checkForUpdates_1 = require("./checkForUpdates");
const cmd_1 = require("./cmd");
const getCommandFullName_1 = require("./getCommandFullName");
const getConfig_1 = require("./getConfig");
const loggers_1 = require("./loggers");
require("./logging/fileLogger");
const pnpmPkgJson_1 = require("./pnpmPkgJson");
const reporter_1 = require("./reporter");
cmd_1.default['install-test'] = cmd_1.default.installTest;
const GLOBAL_OPTIONS = new Set(['color', 'filter', 'help', 'prefix']);
const INSTALL_CLI_OPTIONS = new Set([
    'child-concurrency',
    'dev',
    'engine-strict',
    'frozen-lockfile',
    'force',
    'global-pnpmfile',
    'global',
    'hoist',
    'hoist-pattern',
    'ignore-pnpmfile',
    'ignore-scripts',
    'independent-leaves',
    'link-workspace-packages',
    'lock',
    'lockfile-directory',
    'lockfile-only',
    'lockfile',
    'package-import-method',
    'pnpmfile',
    'prefer-frozen-lockfile',
    'prefer-offline',
    'production',
    'recursive',
    'registry',
    'reporter',
    'shamefully-flatten',
    'shamefully-hoist',
    'shared-workspace-lockfile',
    'side-effects-cache-readonly',
    'side-effects-cache',
    'store',
    'strict-peer-dependencies',
    'offline',
    'only',
    'optional',
    'use-running-store-server',
    'use-store-server',
    'verify-store-integrity',
    'workspace-prefix',
]);
const SUPPORTED_CLI_OPTIONS = {
    'add': new Set([
        'child-concurrency',
        'dev',
        'engine-strict',
        'force',
        'global-pnpmfile',
        'global',
        'hoist',
        'hoist-pattern',
        'ignore-pnpmfile',
        'ignore-scripts',
        'ignore-workspace-root-check',
        'independent-leaves',
        'link-workspace-packages',
        'lock',
        'lockfile-directory',
        'lockfile-only',
        'lockfile',
        'package-import-method',
        'pnpmfile',
        'prefer-offline',
        'production',
        'recursive',
        'registry',
        'reporter',
        'save-dev',
        'save-exact',
        'save-optional',
        'save-peer',
        'save-prod',
        'save-workspace-protocol',
        'shamefully-flatten',
        'shamefully-hoist',
        'shared-workspace-lockfile',
        'side-effects-cache-readonly',
        'side-effects-cache',
        'store',
        'strict-peer-dependencies',
        'offline',
        'only',
        'optional',
        'use-running-store-server',
        'use-store-server',
        'verify-store-integrity',
        'workspace-prefix',
    ]),
    'help': new Set([]),
    'import': new Set([]),
    'install': INSTALL_CLI_OPTIONS,
    'install-test': INSTALL_CLI_OPTIONS,
    'link': new Set([
        'global',
        'only',
        'package-import-method',
        'production',
        'registry',
        'reporter',
        'save-dev',
        'save-exact',
        'save-optional',
    ]),
    'list': new Set([
        'depth',
        'dev',
        'global',
        'json',
        'long',
        'only',
        'optional',
        'parseable',
        'production',
        'recursive',
    ]),
    'outdated': new Set([
        'depth',
        'global',
        'long',
        'recursive',
        'table',
    ]),
    'pack': new Set([]),
    'prune': new Set([
        'production',
    ]),
    'publish': new Set([
        'access',
        'otp',
        'tag',
    ]),
    'rebuild': new Set([
        'recursive',
    ]),
    'recursive': new Set([
        'bail',
        'link-workspace-packages',
        'shared-workspace-lockfile',
        'sort',
        'workspace-concurrency',
    ]),
    'remove': new Set([
        'force',
        'global-pnpmfile',
        'global',
        'lockfile-directory',
        'lockfile-only',
        'lockfile',
        'package-import-method',
        'pnpmfile',
        'recursive',
        'reporter',
        'shared-workspace-lockfile',
        'store',
    ]),
    'restart': new Set([]),
    'root': new Set([
        'global',
    ]),
    'run': new Set([
        'recursive',
    ]),
    'server': new Set([
        'background',
        'ignore-stop-requests',
        'ignore-upload-requests',
        'port',
        'protocol',
        'store',
    ]),
    'start': new Set([]),
    'stop': new Set([]),
    'store': new Set([
        'registry',
        'store',
    ]),
    'test': new Set([
        'recursive',
    ]),
    'unlink': new Set([
        'recursive',
        'global',
    ]),
    'update': new Set([
        'depth',
        'dev',
        'engine-strict',
        'force',
        'global-pnpmfile',
        'global',
        'ignore-pnpmfile',
        'ignore-scripts',
        'latest',
        'lockfile-directory',
        'lockfile-only',
        'lockfile',
        'offline',
        'only',
        'optional',
        'package-import-method',
        'pnpmfile',
        'prefer-offline',
        'production',
        'recursive',
        'registry',
        'reporter',
        'save-exact',
        'shamefully-flatten',
        'shamefully-hoist',
        'shared-workspace-lockfile',
        'side-effects-cache-readonly',
        'side-effects-cache',
        'store',
        'use-running-store-server',
    ]),
    'why': new Set([
        'dev',
        'global',
        'json',
        'long',
        'only',
        'optional',
        'parseable',
        'production',
        'recursive',
    ]),
};
const supportedCmds = new Set([
    'add',
    'install',
    'remove',
    'update',
    'link',
    'pack',
    'prune',
    'publish',
    'install-test',
    'restart',
    'server',
    'start',
    'stop',
    'store',
    'list',
    'unlink',
    'help',
    'root',
    'outdated',
    'rebuild',
    'recursive',
    'import',
    'test',
    'run',
    'why',
]);
async function run(inputArgv) {
    // tslint:disable
    const shortHands = {
        's': ['--reporter', 'silent'],
        'd': ['--loglevel', 'info'],
        'dd': ['--loglevel', 'verbose'],
        'ddd': ['--loglevel', 'silly'],
        'L': ['--latest'],
        'noreg': ['--no-registry'],
        'N': ['--no-registry'],
        'r': ['--recursive'],
        'no-reg': ['--no-registry'],
        'silent': ['--reporter', 'silent'],
        'verbose': ['--loglevel', 'verbose'],
        'quiet': ['--loglevel', 'warn'],
        'q': ['--loglevel', 'warn'],
        'h': ['--usage'],
        'H': ['--usage'],
        '?': ['--usage'],
        'help': ['--usage'],
        'v': ['--version'],
        'f': ['--force'],
        'desc': ['--description'],
        'no-desc': ['--no-description'],
        'local': ['--no-global'],
        'l': ['--long'],
        'm': ['--message'],
        'p': ['--parseable'],
        'porcelain': ['--parseable'],
        'prod': ['--production'],
        'g': ['--global'],
        'S': ['--save'],
        'D': ['--save-dev'],
        'P': ['--save-prod'],
        'E': ['--save-exact'],
        'O': ['--save-optional'],
        'y': ['--yes'],
        'n': ['--no-yes'],
        'B': ['--save-bundle'],
        'C': ['--prefix'],
        'lockfile-directory': ['--shrinkwrap-directory'],
        'lockfile-only': ['--shrinkwrap-only'],
        'shared-workspace-lockfile': ['--shared-workspace-shrinkwrap'],
        'frozen-lockfile': ['--frozen-shrinkwrap'],
        'prefer-frozen-lockfile': ['--prefer-frozen-shrinkwrap'],
        'W': ['--ignore-workspace-root-check'],
    };
    // tslint:enable
    const { argv, ...cliConf } = nopt(config_1.types, shortHands, inputArgv, 0);
    process.env['npm_config_argv'] = JSON.stringify(argv);
    let cmd = getCommandFullName_1.default(argv.remain[0])
        || 'help';
    if (!supportedCmds.has(cmd)) {
        cmd = 'help';
    }
    if (cliConf['dry-run']) {
        console.error(`Error: 'dry-run' is not supported yet, sorry!`);
        process.exit(1);
    }
    let subCmd = argv.remain[1] && getCommandFullName_1.default(argv.remain[1]);
    const filterArgs = [];
    // `pnpm install ""` is going to be just `pnpm install`
    const cliArgs = argv.remain.slice(1).filter(Boolean);
    if (cmd !== 'recursive' && (cliConf['filter'] || cliConf['recursive'] === true)) {
        subCmd = cmd;
        cmd = 'recursive';
        cliArgs.unshift(subCmd);
    }
    else if (subCmd && !supportedCmds.has(subCmd)) {
        subCmd = null;
    }
    let config;
    try {
        config = await getConfig_1.default(cliConf, {
            command: subCmd ? [cmd, subCmd] : [cmd],
            excludeReporter: false,
        });
        config.forceSharedLockfile = typeof config.workspacePrefix === 'string' && config.sharedWorkspaceLockfile === true;
        config.argv = argv;
        if (config.filter) {
            Array.prototype.push.apply(config.filter, filterArgs);
        }
        else {
            config.filter = filterArgs;
        }
    }
    catch (err) {
        // Reporting is not initialized at this point, so just printing the error
        console.error(`${chalk_1.default.bgRed.black('\u2009ERROR\u2009')} ${chalk_1.default.red(err.message)}`);
        console.log(`For help, run: pnpm help ${cmd}`);
        process.exit(1);
        return;
    }
    if ((cmd === 'add' || cmd === 'install') &&
        typeof config.workspacePrefix === 'string') {
        if (cliArgs.length === 0) {
            subCmd = cmd;
            cmd = 'recursive';
            cliArgs.unshift(subCmd);
        }
        else if (config.workspacePrefix === config.prefix &&
            !config.ignoreWorkspaceRootCheck) {
            // Reporting is not initialized at this point, so just printing the error
            console.error(`${chalk_1.default.bgRed.black('\u2009ERROR\u2009')} ${chalk_1.default.red('Running this command will add the dependency to the workspace root, ' +
                'which might not be what you want - if you really meant it, ' +
                'make it explicit by running this command again with the -W flag (or --ignore-workspace-root-check).')}`);
            console.log(`For help, run: pnpm help ${cmd}`);
            process.exit(1);
            return;
        }
    }
    if (cmd === 'install' && cliArgs.length > 0) {
        cmd = 'add';
    }
    else if (subCmd === 'install' && cliArgs.length > 1) {
        subCmd = 'add';
    }
    const allowedOptions = !subCmd
        ? SUPPORTED_CLI_OPTIONS[cmd]
        : new Set([...Array.from(SUPPORTED_CLI_OPTIONS[cmd]), ...Array.from(SUPPORTED_CLI_OPTIONS[subCmd])]);
    for (const cliOption of Object.keys(cliConf)) {
        if (!GLOBAL_OPTIONS.has(cliOption) && !allowedOptions.has(cliOption)) {
            console.error(`${chalk_1.default.bgRed.black('\u2009ERROR\u2009')} ${chalk_1.default.red(`Unknown option '${cliOption}'`)}`);
            console.log(`For help, run: pnpm help ${cmd}`);
            process.exit(1);
            return;
        }
    }
    const selfUpdate = config.global && (cmd === 'add' || cmd === 'update') && argv.remain.includes(pnpmPkgJson_1.default.name);
    // Don't check for updates
    //   1. on CI environments
    //   2. when in the middle of an actual update
    if (!isCI && !selfUpdate) {
        checkForUpdates_1.default();
    }
    const reporterType = (() => {
        if (config.loglevel === 'silent')
            return 'silent';
        if (config.reporter)
            return config.reporter;
        if (isCI || !process.stdout.isTTY)
            return 'append-only';
        return 'default';
    })();
    reporter_1.default(reporterType, {
        cmd,
        config,
        subCmd,
    });
    delete config.reporter; // This is a silly workaround because supi expects a function as config.reporter
    if (selfUpdate) {
        await cmd_1.default.server(['stop'], config); // tslint:disable-line:no-any
    }
    // NOTE: we defer the next stage, otherwise reporter might not catch all the logs
    await new Promise((resolve, reject) => {
        setTimeout(() => {
            if (config.force === true) {
                logger_1.default.warn({
                    message: 'using --force I sure hope you know what you are doing',
                    prefix: config.prefix,
                });
            }
            if (cmd !== 'recursive') {
                loggers_1.scopeLogger.debug({
                    selected: 1,
                    workspacePrefix: config.workspacePrefix,
                });
            }
            try {
                const result = cmd_1.default[cmd](cliArgs, config, argv.remain[0]);
                if (result instanceof Promise) {
                    result
                        .then((output) => {
                        if (typeof output === 'string') {
                            process.stdout.write(output);
                        }
                        resolve();
                    })
                        .catch(reject);
                }
                else {
                    if (typeof result === 'string') {
                        process.stdout.write(result);
                    }
                    resolve();
                }
            }
            catch (err) {
                reject(err);
            }
        }, 0);
    });
}
exports.default = run;
