"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cli_utils_1 = require("@pnpm/cli-utils");
const common_cli_options_help_1 = require("@pnpm/common-cli-options-help");
const config_1 = require("@pnpm/config");
const list_1 = require("@pnpm/list");
const common_tags_1 = require("common-tags");
const R = require("ramda");
const renderHelp = require("render-help");
const recursive_1 = require("./recursive");
exports.rcOptionsTypes = cliOptionsTypes;
function cliOptionsTypes() {
    return R.pick([
        'depth',
        'dev',
        'global-dir',
        'global',
        'json',
        'long',
        'only',
        'optional',
        'parseable',
        'production',
        'recursive',
    ], config_1.types);
}
exports.cliOptionsTypes = cliOptionsTypes;
exports.commandNames = ['list', 'ls'];
function help() {
    return renderHelp({
        aliases: ['list', 'ls', 'la', 'll'],
        description: common_tags_1.oneLine `When run as ll or la, it shows extended information by default.
      All dependencies are printed by default. Search by patterns is supported.
      For example: pnpm ls babel-* eslint-*`,
        descriptionLists: [
            {
                title: 'Options',
                list: [
                    {
                        description: common_tags_1.oneLine `Perform command on every package in subdirectories
              or on every workspace package, when executed inside a workspace.
              For options that may be used with \`-r\`, see "pnpm help recursive"`,
                        name: '--recursive',
                        shortAlias: '-r',
                    },
                    {
                        description: 'Show extended information',
                        name: '--long',
                    },
                    {
                        description: 'Show parseable output instead of tree view',
                        name: '--parseable',
                    },
                    {
                        description: 'Show information in JSON format',
                        name: '--json',
                    },
                    {
                        description: 'List packages in the global install prefix instead of in the current project',
                        name: '--global',
                        shortAlias: '-g',
                    },
                    {
                        description: 'Max display depth of the dependency tree',
                        name: '--depth <number>',
                    },
                    {
                        description: 'Display only direct dependencies',
                        name: '--depth 0',
                    },
                    {
                        description: 'Display only projects. Useful in a monorepo. \`pnpm ls -r --depth -1\` lists all projects in a monorepo',
                        name: '--depth -1',
                    },
                    {
                        description: 'Display only the dependency tree for packages in \`dependencies\`',
                        name: '--prod, --production',
                    },
                    {
                        description: 'Display only the dependency tree for packages in \`devDependencies\`',
                        name: '--dev',
                    },
                    common_cli_options_help_1.OPTIONS.globalDir,
                    ...common_cli_options_help_1.UNIVERSAL_OPTIONS,
                ],
            },
            common_cli_options_help_1.FILTERING,
        ],
        url: cli_utils_1.docsUrl('list'),
        usages: [
            'pnpm ls [<pkg> ...]',
        ],
    });
}
exports.help = help;
function handler(args, opts) {
    const include = {
        dependencies: opts.production !== false,
        devDependencies: opts.dev !== false,
        optionalDependencies: opts.optional !== false,
    };
    if (opts.recursive && opts.selectedProjectsGraph) {
        const pkgs = Object.values(opts.selectedProjectsGraph).map((wsPkg) => wsPkg.package);
        return recursive_1.default(pkgs, args, { ...opts, include });
    }
    return render([opts.dir], args, {
        ...opts,
        include,
        lockfileDir: opts.lockfileDir || opts.dir,
    });
}
exports.handler = handler;
async function render(prefixes, args, opts) {
    var _a;
    const listOpts = {
        alwaysPrintRootPackage: opts.alwaysPrintRootPackage,
        depth: (_a = opts.depth, (_a !== null && _a !== void 0 ? _a : 0)),
        include: opts.include,
        lockfileDir: opts.lockfileDir,
        long: opts.long,
        // tslint:disable-next-line: no-unnecessary-type-assertion
        reportAs: (opts.parseable ? 'parseable' : (opts.json ? 'json' : 'tree')),
    };
    return args.length
        ? list_1.forPackages(args, prefixes, listOpts)
        : list_1.default(prefixes, listOpts);
}
exports.render = render;
