// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { Colorize } from '@rushstack/terminal';
import { BaseRushAction } from './BaseRushAction';
import { VersionMismatchFinder } from '../../logic/versionMismatch/VersionMismatchFinder';
import { getVariantAsync, VARIANT_PARAMETER } from '../../api/Variants';
export class CheckAction extends BaseRushAction {
    constructor(parser) {
        super({
            actionName: 'check',
            summary: "Checks each project's package.json files and ensures that all dependencies are of the same " +
                'version throughout the repository.',
            documentation: "Checks each project's package.json files and ensures that all dependencies are of the " +
                'same version throughout the repository.',
            safeForSimultaneousRushProcesses: true,
            parser
        });
        this._jsonFlag = this.defineFlagParameter({
            parameterLongName: '--json',
            description: 'If this flag is specified, output will be in JSON format.'
        });
        this._verboseFlag = this.defineFlagParameter({
            parameterLongName: '--verbose',
            description: 'If this flag is specified, long lists of package names will not be truncated. ' +
                `This has no effect if the ${this._jsonFlag.longName} flag is also specified.`
        });
        this._subspaceParameter = this.defineStringParameter({
            parameterLongName: '--subspace',
            argumentName: 'SUBSPACE_NAME',
            description: '(EXPERIMENTAL) Specifies an individual Rush subspace to check, requiring versions to be ' +
                'consistent only within that subspace (ignoring other subspaces). This parameter is required when ' +
                'the "subspacesEnabled" setting is set to true in subspaces.json.'
        });
        this._variantParameter = this.defineStringParameter(VARIANT_PARAMETER);
    }
    async runAsync() {
        var _a;
        if (this.rushConfiguration.subspacesFeatureEnabled && !this._subspaceParameter) {
            throw new Error(`The --subspace parameter must be specified with "rush check" when subspaces is enabled.`);
        }
        const currentlyInstalledVariant = await this.rushConfiguration.getCurrentlyInstalledVariantAsync();
        const variant = await getVariantAsync(this._variantParameter, this.rushConfiguration, true);
        if (!variant && currentlyInstalledVariant) {
            this.terminal.writeWarningLine(Colorize.yellow(`Variant '${currentlyInstalledVariant}' has been installed, but 'rush check' is currently checking the default variant. ` +
                `Use 'rush ${this.actionName} ${this._variantParameter.longName} '${currentlyInstalledVariant}' to check the current installation.`));
        }
        VersionMismatchFinder.rushCheck(this.rushConfiguration, this.terminal, {
            variant,
            printAsJson: this._jsonFlag.value,
            truncateLongPackageNameLists: !this._verboseFlag.value,
            subspace: ((_a = this._subspaceParameter) === null || _a === void 0 ? void 0 : _a.value)
                ? this.rushConfiguration.getSubspace(this._subspaceParameter.value)
                : this.rushConfiguration.defaultSubspace
        });
    }
}
//# sourceMappingURL=CheckAction.js.map