"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 common_cli_options_help_1 = require("@pnpm/common-cli-options-help");
const config_1 = require("@pnpm/config");
const store_connection_manager_1 = require("@pnpm/store-connection-manager");
const common_tags_1 = require("common-tags");
const R = require("ramda");
const renderHelp = require("render-help");
const implementation_1 = require("./implementation");
const recursive_1 = require("./recursive");
function rcOptionsTypes() {
    return {
        ...R.pick([
            'npm-path',
            'reporter',
            'unsafe-perm',
        ], config_1.types),
    };
}
exports.rcOptionsTypes = rcOptionsTypes;
function cliOptionsTypes() {
    return {
        ...rcOptionsTypes(),
        pending: Boolean,
        recursive: Boolean,
    };
}
exports.cliOptionsTypes = cliOptionsTypes;
exports.commandNames = ['rebuild', 'rb'];
function help() {
    return renderHelp({
        aliases: ['rb'],
        description: 'Rebuild a package.',
        descriptionLists: [
            {
                title: 'Options',
                list: [
                    {
                        description: common_tags_1.oneLine `Rebuild every package found in subdirectories
              or 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: 'Rebuild packages that were not build during installation. Packages are not build when installing with the --ignore-scripts flag',
                        name: '--pending',
                    },
                    ...common_cli_options_help_1.UNIVERSAL_OPTIONS,
                ],
            },
            common_cli_options_help_1.FILTERING,
        ],
        url: cli_utils_1.docsUrl('rebuild'),
        usages: ['pnpm rebuild [<pkg> ...]'],
    });
}
exports.help = help;
async function handler(opts, params) {
    if (opts.recursive && opts.allProjects && opts.selectedProjectsGraph && opts.workspaceDir) {
        await recursive_1.default(opts.allProjects, params, { ...opts, selectedProjectsGraph: opts.selectedProjectsGraph, workspaceDir: opts.workspaceDir });
        return;
    }
    const store = await store_connection_manager_1.createOrConnectStoreController(opts);
    const rebuildOpts = Object.assign(opts, {
        sideEffectsCacheRead: opts.sideEffectsCache || opts.sideEffectsCacheReadonly,
        sideEffectsCacheWrite: opts.sideEffectsCache,
        storeController: store.ctrl,
        storeDir: store.dir,
    });
    if (params.length === 0) {
        await implementation_1.rebuild([
            {
                buildIndex: 0,
                manifest: await cli_utils_1.readProjectManifestOnly(rebuildOpts.dir, opts),
                rootDir: rebuildOpts.dir,
            },
        ], rebuildOpts);
    }
    await implementation_1.rebuildPkgs([
        {
            manifest: await cli_utils_1.readProjectManifestOnly(rebuildOpts.dir, opts),
            rootDir: rebuildOpts.dir,
        },
    ], params, rebuildOpts);
}
exports.handler = handler;
//# sourceMappingURL=rebuild.js.map