"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handler = exports.help = exports.commandNames = exports.cliOptionsTypes = exports.rcOptionsTypes = void 0;
const cli_utils_1 = require("@pnpm/cli-utils");
const config_1 = require("@pnpm/config");
const error_1 = require("@pnpm/error");
const logger_1 = require("@pnpm/logger");
const store_connection_manager_1 = require("@pnpm/store-connection-manager");
const store_path_1 = require("@pnpm/store-path");
const common_tags_1 = require("common-tags");
const R = require("ramda");
const renderHelp = require("render-help");
const storeAdd_1 = require("./storeAdd");
const storePrune_1 = require("./storePrune");
const storeStatus_1 = require("./storeStatus");
exports.rcOptionsTypes = cliOptionsTypes;
function cliOptionsTypes() {
    return R.pick([
        'registry',
        'store',
        'store-dir',
    ], config_1.types);
}
exports.cliOptionsTypes = cliOptionsTypes;
exports.commandNames = ['store'];
function help() {
    return renderHelp({
        description: 'Reads and performs actions on pnpm store that is on the current filesystem.',
        descriptionLists: [
            {
                title: 'Commands',
                list: [
                    {
                        description: common_tags_1.oneLine `
              Checks for modified packages in the store.
              Returns exit code 0 if the content of the package is the same as it was at the time of unpacking
            `,
                        name: 'status',
                    },
                    {
                        description: 'Adds new packages to the store. Example: pnpm store add express@4 typescript@2.1.0',
                        name: 'add <pkg>...',
                    },
                    {
                        description: common_tags_1.oneLine `
              Removes unreferenced (extraneous, orphan) packages from the store.
              Pruning the store is not harmful, but might slow down future installations.
              Visit the documentation for more information on unreferenced packages and why they occur
            `,
                        name: 'prune',
                    },
                ],
            },
        ],
        url: cli_utils_1.docsUrl('store'),
        usages: ['pnpm store <command>'],
    });
}
exports.help = help;
class StoreStatusError extends error_1.default {
    constructor(modified) {
        super('MODIFIED_DEPENDENCY', '');
        this.modified = modified;
    }
}
async function handler(opts, params) {
    let store;
    switch (params[0]) {
        case 'status':
            return statusCmd(opts);
        case 'prune':
            store = await store_connection_manager_1.createOrConnectStoreController(opts);
            const storePruneOptions = Object.assign(opts, {
                storeController: store.ctrl,
                storeDir: store.dir,
            });
            return storePrune_1.default(storePruneOptions);
        case 'add':
            store = await store_connection_manager_1.createOrConnectStoreController(opts);
            return storeAdd_1.default(params.slice(1), {
                prefix: opts.dir,
                registries: opts.registries,
                reporter: opts.reporter,
                storeController: store.ctrl,
                tag: opts.tag,
            });
        default:
            return help();
            if (params[0]) {
                throw new error_1.default('INVALID_STORE_COMMAND', `"store ${params[0]}" is not a pnpm command. See "pnpm help store".`);
            }
    }
}
exports.handler = handler;
async function statusCmd(opts) {
    const modifiedPkgs = await storeStatus_1.default(Object.assign(opts, {
        storeDir: await store_path_1.default(opts.dir, opts.storeDir),
    }));
    if (!modifiedPkgs || !modifiedPkgs.length) {
        logger_1.default.info({
            message: 'Packages in the store are untouched',
            prefix: opts.dir,
        });
        return;
    }
    throw new StoreStatusError(modifiedPkgs);
}
//# sourceMappingURL=store.js.map