"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RushConfiguration = void 0;
/* eslint max-lines: off */
const path = __importStar(require("node:path"));
const semver = __importStar(require("semver"));
const true_case_path_1 = require("true-case-path");
const node_core_library_1 = require("@rushstack/node-core-library");
const lookup_by_path_1 = require("@rushstack/lookup-by-path");
const Rush_1 = require("./Rush");
const RushConfigurationProject_1 = require("./RushConfigurationProject");
const RushConstants_1 = require("../logic/RushConstants");
const ApprovedPackagesPolicy_1 = require("./ApprovedPackagesPolicy");
const EventHooks_1 = require("./EventHooks");
const VersionPolicyConfiguration_1 = require("./VersionPolicyConfiguration");
const EnvironmentConfiguration_1 = require("./EnvironmentConfiguration");
const Utilities_1 = require("../utilities/Utilities");
const NpmPackageManager_1 = require("./packageManager/NpmPackageManager");
const YarnPackageManager_1 = require("./packageManager/YarnPackageManager");
const PnpmPackageManager_1 = require("./packageManager/PnpmPackageManager");
const ExperimentsConfiguration_1 = require("./ExperimentsConfiguration");
const PackageNameParsers_1 = require("./PackageNameParsers");
const RushPluginsConfiguration_1 = require("./RushPluginsConfiguration");
const PnpmOptionsConfiguration_1 = require("../logic/pnpm/PnpmOptionsConfiguration");
const NpmOptionsConfiguration_1 = require("../logic/npm/NpmOptionsConfiguration");
const YarnOptionsConfiguration_1 = require("../logic/yarn/YarnOptionsConfiguration");
const rush_schema_json_1 = __importDefault(require("../schemas/rush.schema.json"));
const CustomTipsConfiguration_1 = require("./CustomTipsConfiguration");
const SubspacesConfiguration_1 = require("./SubspacesConfiguration");
const Subspace_1 = require("./Subspace");
const MINIMUM_SUPPORTED_RUSH_JSON_VERSION = '0.0.0';
const DEFAULT_BRANCH = 'main';
const DEFAULT_REMOTE = 'origin';
/**
 * A list of known config filenames that are expected to appear in the "./common/config/rush" folder.
 * To avoid confusion/mistakes, any extra files will be reported as an error.
 */
const knownRushConfigFilenames = [
    '.npmrc-publish',
    '.npmrc',
    'deploy.json',
    RushConstants_1.RushConstants.artifactoryFilename,
    RushConstants_1.RushConstants.browserApprovedPackagesFilename,
    RushConstants_1.RushConstants.buildCacheFilename,
    RushConstants_1.RushConstants.cobuildFilename,
    RushConstants_1.RushConstants.commandLineFilename,
    RushConstants_1.RushConstants.commonVersionsFilename,
    RushConstants_1.RushConstants.customTipsFilename,
    RushConstants_1.RushConstants.experimentsFilename,
    RushConstants_1.RushConstants.nonbrowserApprovedPackagesFilename,
    RushConstants_1.RushConstants.pinnedVersionsFilename,
    RushConstants_1.RushConstants.repoStateFilename,
    RushConstants_1.RushConstants.versionPoliciesFilename,
    RushConstants_1.RushConstants.rushPluginsConfigFilename,
    RushConstants_1.RushConstants.pnpmConfigFilename,
    RushConstants_1.RushConstants.subspacesConfigFilename,
    RushConstants_1.RushConstants.rushAlertsConfigFilename
];
/**
 * This represents the Rush configuration for a repository, based on the "rush.json"
 * configuration file.
 * @public
 */
class RushConfiguration {
    /**
     * Use RushConfiguration.loadFromConfigurationFile() or Use RushConfiguration.loadFromDefaultLocation()
     * instead.
     */
    constructor(rushConfigurationJson, rushJsonFilename) {
        var _a, _b;
        this._subspaces = [];
        this.rushConfigurationJson = rushConfigurationJson;
        EnvironmentConfiguration_1.EnvironmentConfiguration.validate();
        if (rushConfigurationJson.nodeSupportedVersionRange) {
            if (!semver.validRange(rushConfigurationJson.nodeSupportedVersionRange)) {
                throw new Error('Error parsing the node-semver expression in the "nodeSupportedVersionRange"' +
                    ` field from ${RushConstants_1.RushConstants.rushJsonFilename}: "${rushConfigurationJson.nodeSupportedVersionRange}"`);
            }
            if (!semver.satisfies(process.version, rushConfigurationJson.nodeSupportedVersionRange)) {
                let message = `Your dev environment is running Node.js version ${process.version} which does` +
                    ` not meet the requirements for building this repository.  (The ${RushConstants_1.RushConstants.rushJsonFilename} configuration` +
                    ` requires nodeSupportedVersionRange="${rushConfigurationJson.nodeSupportedVersionRange}")`;
                if (rushConfigurationJson.nodeSupportedVersionInstructions) {
                    message += '\n\n' + rushConfigurationJson.nodeSupportedVersionInstructions;
                }
                if (EnvironmentConfiguration_1.EnvironmentConfiguration.allowUnsupportedNodeVersion) {
                    // eslint-disable-next-line no-console
                    console.warn(message);
                }
                else {
                    throw new Error(message);
                }
            }
        }
        this.rushJsonFile = rushJsonFilename;
        this.rushJsonFolder = path.dirname(rushJsonFilename);
        this.commonFolder = path.resolve(path.join(this.rushJsonFolder, RushConstants_1.RushConstants.commonFolderName));
        this.commonRushConfigFolder = path.join(this.commonFolder, 'config', 'rush');
        this.commonTempFolder =
            EnvironmentConfiguration_1.EnvironmentConfiguration.rushTempFolderOverride ||
                path.join(this.commonFolder, RushConstants_1.RushConstants.rushTempFolderName);
        this.commonScriptsFolder = path.join(this.commonFolder, 'scripts');
        this.npmCacheFolder = path.resolve(path.join(this.commonTempFolder, 'npm-cache'));
        this.npmTmpFolder = path.resolve(path.join(this.commonTempFolder, 'npm-tmp'));
        this.yarnCacheFolder = path.resolve(path.join(this.commonTempFolder, 'yarn-cache'));
        this.changesFolder = path.join(this.commonFolder, RushConstants_1.RushConstants.changeFilesFolderName);
        this.currentVariantJsonFilePath = path.join(this.commonTempFolder, RushConstants_1.RushConstants.currentVariantsFilename);
        this.suppressNodeLtsWarning = !!rushConfigurationJson.suppressNodeLtsWarning;
        this._ensureConsistentVersionsJsonValue = rushConfigurationJson.ensureConsistentVersions;
        this.ensureConsistentVersions = !!rushConfigurationJson.ensureConsistentVersions;
        // Try getting a subspace configuration
        this.subspacesConfiguration = SubspacesConfiguration_1.SubspacesConfiguration.tryLoadFromDefaultLocation(this);
        this.subspacesFeatureEnabled = !!((_a = this.subspacesConfiguration) === null || _a === void 0 ? void 0 : _a.subspacesEnabled);
        this._subspacesByName = new Map();
        const experimentsConfigFile = path.join(this.commonRushConfigFolder, RushConstants_1.RushConstants.experimentsFilename);
        this.experimentsConfiguration = new ExperimentsConfiguration_1.ExperimentsConfiguration(experimentsConfigFile);
        const rushPluginsConfigFilename = path.join(this.commonRushConfigFolder, RushConstants_1.RushConstants.rushPluginsConfigFilename);
        this._rushPluginsConfiguration = new RushPluginsConfiguration_1.RushPluginsConfiguration(rushPluginsConfigFilename);
        this.npmOptions = new NpmOptionsConfiguration_1.NpmOptionsConfiguration(rushConfigurationJson.npmOptions || {});
        this.yarnOptions = new YarnOptionsConfiguration_1.YarnOptionsConfiguration(rushConfigurationJson.yarnOptions || {});
        try {
            this.pnpmOptions = PnpmOptionsConfiguration_1.PnpmOptionsConfiguration.loadFromJsonFileOrThrow(`${this.commonRushConfigFolder}/${RushConstants_1.RushConstants.pnpmConfigFilename}`, this.commonTempFolder);
            if (rushConfigurationJson.pnpmOptions) {
                throw new Error('Because the new config file "common/config/rush/pnpm-config.json" is being used, ' +
                    `you must remove the old setting "pnpmOptions" from ${RushConstants_1.RushConstants.rushJsonFilename}`);
            }
        }
        catch (error) {
            if (node_core_library_1.FileSystem.isNotExistError(error)) {
                this.pnpmOptions = PnpmOptionsConfiguration_1.PnpmOptionsConfiguration.loadFromJsonObject(rushConfigurationJson.pnpmOptions || {}, this.commonTempFolder);
            }
            else {
                throw error;
            }
        }
        // TODO: Add an actual "packageManager" field in rush.json
        const packageManagerFields = [];
        this.isPnpm = false;
        if (rushConfigurationJson.npmVersion) {
            this.packageManager = 'npm';
            this.packageManagerOptions = this.npmOptions;
            packageManagerFields.push('npmVersion');
        }
        if (rushConfigurationJson.pnpmVersion) {
            this.packageManager = 'pnpm';
            this.isPnpm = true;
            this.packageManagerOptions = this.pnpmOptions;
            packageManagerFields.push('pnpmVersion');
        }
        if (rushConfigurationJson.yarnVersion) {
            this.packageManager = 'yarn';
            this.packageManagerOptions = this.yarnOptions;
            packageManagerFields.push('yarnVersion');
        }
        if (packageManagerFields.length === 0) {
            throw new Error(`The ${RushConstants_1.RushConstants.rushJsonFilename} configuration must specify one of: npmVersion, pnpmVersion, or yarnVersion`);
        }
        if (packageManagerFields.length > 1) {
            throw new Error(`The ${RushConstants_1.RushConstants.rushJsonFilename} configuration cannot specify both ${packageManagerFields[0]}` +
                ` and ${packageManagerFields[1]} `);
        }
        if (this.packageManager === 'npm') {
            this.packageManagerToolVersion = rushConfigurationJson.npmVersion;
            this.packageManagerWrapper = new NpmPackageManager_1.NpmPackageManager(this.packageManagerToolVersion);
        }
        else if (this.packageManager === 'pnpm') {
            this.packageManagerToolVersion = rushConfigurationJson.pnpmVersion;
            this.packageManagerWrapper = new PnpmPackageManager_1.PnpmPackageManager(this.packageManagerToolVersion);
        }
        else {
            this.packageManagerToolVersion = rushConfigurationJson.yarnVersion;
            this.packageManagerWrapper = new YarnPackageManager_1.YarnPackageManager(this.packageManagerToolVersion);
        }
        this.shrinkwrapFilename = this.packageManagerWrapper.shrinkwrapFilename;
        this.packageManagerToolFilename = path.resolve(path.join(this.commonTempFolder, `${this.packageManager}-local`, 'node_modules', '.bin', `${this.packageManager}`));
        RushConfiguration._validateCommonRushConfigFolder(this.commonRushConfigFolder, this.packageManagerWrapper, this.experimentsConfiguration, this.subspacesFeatureEnabled);
        this.projectFolderMinDepth =
            rushConfigurationJson.projectFolderMinDepth !== undefined
                ? rushConfigurationJson.projectFolderMinDepth
                : 1;
        if (this.projectFolderMinDepth < 1) {
            throw new Error('Invalid projectFolderMinDepth; the minimum possible value is 1');
        }
        this.projectFolderMaxDepth =
            rushConfigurationJson.projectFolderMaxDepth !== undefined
                ? rushConfigurationJson.projectFolderMaxDepth
                : 2;
        if (this.projectFolderMaxDepth < this.projectFolderMinDepth) {
            throw new Error('The projectFolderMaxDepth cannot be smaller than the projectFolderMinDepth');
        }
        this.allowMostlyStandardPackageNames = !!rushConfigurationJson.allowMostlyStandardPackageNames;
        this.packageNameParser = this.allowMostlyStandardPackageNames
            ? PackageNameParsers_1.PackageNameParsers.mostlyStandard
            : PackageNameParsers_1.PackageNameParsers.rushDefault;
        this.approvedPackagesPolicy = new ApprovedPackagesPolicy_1.ApprovedPackagesPolicy(this, rushConfigurationJson);
        this.gitAllowedEmailRegExps = [];
        this.gitSampleEmail = '';
        if (rushConfigurationJson.gitPolicy) {
            if (rushConfigurationJson.gitPolicy.sampleEmail) {
                this.gitSampleEmail = rushConfigurationJson.gitPolicy.sampleEmail;
            }
            if (rushConfigurationJson.gitPolicy.allowedEmailRegExps) {
                this.gitAllowedEmailRegExps = rushConfigurationJson.gitPolicy.allowedEmailRegExps;
                if (this.gitSampleEmail.trim().length < 1) {
                    throw new Error(`The ${RushConstants_1.RushConstants.rushJsonFilename} file is missing the "sampleEmail" option, ` +
                        'which is required when using "allowedEmailRegExps"');
                }
            }
            if (rushConfigurationJson.gitPolicy.versionBumpCommitMessage) {
                this.gitVersionBumpCommitMessage = rushConfigurationJson.gitPolicy.versionBumpCommitMessage;
            }
            if (rushConfigurationJson.gitPolicy.changeLogUpdateCommitMessage) {
                this.gitChangeLogUpdateCommitMessage = rushConfigurationJson.gitPolicy.changeLogUpdateCommitMessage;
            }
            if (rushConfigurationJson.gitPolicy.changefilesCommitMessage) {
                this.gitChangefilesCommitMessage = rushConfigurationJson.gitPolicy.changefilesCommitMessage;
            }
            if (rushConfigurationJson.gitPolicy.tagSeparator) {
                this.gitTagSeparator = rushConfigurationJson.gitPolicy.tagSeparator;
            }
        }
        this.hotfixChangeEnabled = false;
        if (rushConfigurationJson.hotfixChangeEnabled) {
            this.hotfixChangeEnabled = rushConfigurationJson.hotfixChangeEnabled;
        }
        if (!rushConfigurationJson.repository) {
            rushConfigurationJson.repository = {};
        }
        this.repositoryDefaultBranch = rushConfigurationJson.repository.defaultBranch || DEFAULT_BRANCH;
        this.repositoryDefaultRemote = rushConfigurationJson.repository.defaultRemote || DEFAULT_REMOTE;
        const repositoryFieldWithMultipleUrls = rushConfigurationJson.repository;
        const repositoryFieldWithSingleUrl = rushConfigurationJson.repository;
        if (repositoryFieldWithMultipleUrls.urls) {
            if (repositoryFieldWithSingleUrl.url) {
                throw new Error("The 'repository.url' field cannot be used when 'repository.urls' is present");
            }
            this.repositoryUrls = repositoryFieldWithMultipleUrls.urls;
        }
        else if (repositoryFieldWithSingleUrl.url) {
            this.repositoryUrls = [repositoryFieldWithSingleUrl.url];
        }
        else {
            this.repositoryUrls = [];
        }
        this.telemetryEnabled = !!rushConfigurationJson.telemetryEnabled;
        this.eventHooks = new EventHooks_1.EventHooks(rushConfigurationJson.eventHooks || {});
        this.versionPolicyConfigurationFilePath = path.join(this.commonRushConfigFolder, RushConstants_1.RushConstants.versionPoliciesFilename);
        this.versionPolicyConfiguration = new VersionPolicyConfiguration_1.VersionPolicyConfiguration(this.versionPolicyConfigurationFilePath);
        this.customTipsConfigurationFilePath = path.join(this.commonRushConfigFolder, RushConstants_1.RushConstants.customTipsFilename);
        this.customTipsConfiguration = new CustomTipsConfiguration_1.CustomTipsConfiguration(this.customTipsConfigurationFilePath);
        const variants = new Set();
        for (const variantOptions of (_b = rushConfigurationJson.variants) !== null && _b !== void 0 ? _b : []) {
            const { variantName } = variantOptions;
            if (variants.has(variantName)) {
                throw new Error(`Duplicate variant named '${variantName}' specified in configuration.`);
            }
            variants.add(variantName);
        }
        this.variants = variants;
        this._pathTrees = new Map();
    }
    _initializeAndValidateLocalProjects() {
        var _a;
        this._projects = [];
        this._projectsByName = new Map();
        this._subspacesByName.clear();
        this._subspaces.length = 0;
        // Build the subspaces map
        const subspaceNames = [];
        let splitWorkspaceCompatibility = false;
        if ((_a = this.subspacesConfiguration) === null || _a === void 0 ? void 0 : _a.subspacesEnabled) {
            splitWorkspaceCompatibility = this.subspacesConfiguration.splitWorkspaceCompatibility;
            subspaceNames.push(...this.subspacesConfiguration.subspaceNames);
        }
        if (subspaceNames.indexOf(RushConstants_1.RushConstants.defaultSubspaceName) < 0) {
            subspaceNames.push(RushConstants_1.RushConstants.defaultSubspaceName);
        }
        // Sort the subspaces in alphabetical order.  This ensures that they are processed
        // in a deterministic order by the various Rush algorithms.
        subspaceNames.sort();
        for (const subspaceName of subspaceNames) {
            const subspace = new Subspace_1.Subspace({
                subspaceName,
                rushConfiguration: this,
                splitWorkspaceCompatibility
            });
            this._subspacesByName.set(subspaceName, subspace);
            this._subspaces.push(subspace);
        }
        const defaultSubspace = this._subspacesByName.get(RushConstants_1.RushConstants.defaultSubspaceName);
        if (!defaultSubspace) {
            throw new node_core_library_1.InternalError('The default subspace was not created');
        }
        // Sort the projects array in alphabetical order.  This ensures that the packages
        // are processed in a deterministic order by the various Rush algorithms.
        const sortedProjectJsons = this.rushConfigurationJson.projects.slice(0);
        sortedProjectJsons.sort((a, b) => a.packageName.localeCompare(b.packageName));
        const allowedProjectTags = this.rushConfigurationJson.allowedProjectTags
            ? new Set(this.rushConfigurationJson.allowedProjectTags)
            : undefined;
        const usedTempNames = new Set();
        for (let i = 0, len = sortedProjectJsons.length; i < len; i++) {
            const projectJson = sortedProjectJsons[i];
            const tempProjectName = RushConfiguration._generateTempNameForProject(projectJson, usedTempNames);
            let subspace = undefined;
            if (this.subspacesFeatureEnabled) {
                if (projectJson.subspaceName) {
                    subspace = this._subspacesByName.get(projectJson.subspaceName);
                    if (subspace === undefined) {
                        throw new Error(`The project "${projectJson.packageName}" in ${RushConstants_1.RushConstants.rushJsonFilename} references` +
                            ` a nonexistent subspace "${projectJson.subspaceName}"`);
                    }
                }
            }
            if (subspace === undefined) {
                subspace = defaultSubspace;
            }
            const project = new RushConfigurationProject_1.RushConfigurationProject({
                projectJson,
                rushConfiguration: this,
                tempProjectName,
                allowedProjectTags,
                subspace
            });
            subspace._addProject(project);
            this._projects.push(project);
            if (this._projectsByName.has(project.packageName)) {
                throw new Error(`The project name "${project.packageName}" was specified more than once` +
                    ` in the ${RushConstants_1.RushConstants.rushJsonFilename} configuration file.`);
            }
            this._projectsByName.set(project.packageName, project);
        }
        for (const project of this._projects) {
            project.decoupledLocalDependencies.forEach((decoupledLocalDependency) => {
                if (!this.getProjectByName(decoupledLocalDependency)) {
                    throw new Error(`In ${RushConstants_1.RushConstants.rushJsonFilename}, the "${decoupledLocalDependency}" project does not exist,` +
                        ` but was referenced by the decoupledLocalDependencies (previously cyclicDependencyProjects) for ${project.packageName}`);
                }
            });
            this.versionPolicyConfiguration.validate(this.projectsByName);
            // Consumer relationships will be established the first time one is requested
        }
    }
    /**
     * Loads the configuration data from an Rush.json configuration file and returns
     * an RushConfiguration object.
     */
    static loadFromConfigurationFile(rushJsonFilename) {
        let resolvedRushJsonFilename = path.resolve(rushJsonFilename);
        // Load the rush.json before we fix the casing. If the case is wrong on a case-sensitive filesystem,
        // the next line show throw.
        const rushConfigurationJson = node_core_library_1.JsonFile.load(resolvedRushJsonFilename);
        try {
            resolvedRushJsonFilename = (0, true_case_path_1.trueCasePathSync)(resolvedRushJsonFilename);
        }
        catch (error) {
            /* ignore errors from true-case-path */
        }
        // Check the Rush version *before* we validate the schema, since if the version is outdated
        // then the schema may have changed. This should no longer be a problem after Rush 4.0 and the C2R wrapper,
        // but we'll validate anyway.
        const expectedRushVersion = rushConfigurationJson.rushVersion;
        const rushJsonBaseName = path.basename(resolvedRushJsonFilename);
        // If the version is missing or malformed, fall through and let the schema handle it.
        if (expectedRushVersion && semver.valid(expectedRushVersion)) {
            // Make sure the requested version isn't too old
            if (semver.lt(expectedRushVersion, MINIMUM_SUPPORTED_RUSH_JSON_VERSION)) {
                throw new Error(`${rushJsonBaseName} is version ${expectedRushVersion}, which is too old for this tool. ` +
                    `The minimum supported version is ${MINIMUM_SUPPORTED_RUSH_JSON_VERSION}.`);
            }
            // Make sure the requested version isn't too new.
            //
            // If the major/minor versions are the same, then we consider the file to be compatible.
            // This is somewhat lax, e.g. "5.0.2-dev.3" will be assumed to be loadable by rush-lib 5.0.0.
            //
            // IMPORTANT: Whenever a breaking change is introduced for one of the config files, we must
            // increment the minor version number for Rush.
            if (semver.major(Rush_1.Rush.version) !== semver.major(expectedRushVersion) ||
                semver.minor(Rush_1.Rush.version) !== semver.minor(expectedRushVersion)) {
                // If the major/minor are different, then make sure it's an older version.
                if (semver.lt(Rush_1.Rush.version, expectedRushVersion)) {
                    throw new Error(`Unable to load ${rushJsonBaseName} because its RushVersion is` +
                        ` ${rushConfigurationJson.rushVersion}, whereas @microsoft/rush-lib is version ${Rush_1.Rush.version}.` +
                        ` Consider upgrading the library.`);
                }
            }
        }
        RushConfiguration._jsonSchema.validateObject(rushConfigurationJson, resolvedRushJsonFilename);
        return new RushConfiguration(rushConfigurationJson, resolvedRushJsonFilename);
    }
    static tryLoadFromDefaultLocation(options) {
        const rushJsonLocation = RushConfiguration.tryFindRushJsonLocation(options);
        if (rushJsonLocation) {
            return RushConfiguration.loadFromConfigurationFile(rushJsonLocation);
        }
    }
    static loadFromDefaultLocation(options) {
        const rushConfiguration = RushConfiguration.tryLoadFromDefaultLocation(options);
        if (rushConfiguration) {
            return rushConfiguration;
        }
        else {
            throw Utilities_1.Utilities.getRushConfigNotFoundError();
        }
    }
    /**
     * Find the rush.json location and return the path, or undefined if a rush.json can't be found.
     *
     * @privateRemarks
     * Keep this in sync with `findRushJsonLocation` in `rush-sdk/src/index.ts`.
     */
    static tryFindRushJsonLocation(options) {
        const optionsIn = options || {};
        const verbose = optionsIn.showVerbose || false;
        let currentFolder = optionsIn.startingFolder || process.cwd();
        let parentFolder = path.dirname(currentFolder);
        // look upwards at parent folders until we find a folder containing rush.json,
        // or we reach the root directory without finding a rush.json file
        while (parentFolder && parentFolder !== currentFolder) {
            const rushJsonFilename = path.join(currentFolder, RushConstants_1.RushConstants.rushJsonFilename);
            if (node_core_library_1.FileSystem.exists(rushJsonFilename)) {
                if (currentFolder !== optionsIn.startingFolder && verbose) {
                    // eslint-disable-next-line no-console
                    console.log('Found configuration in ' + rushJsonFilename);
                }
                if (verbose) {
                    // eslint-disable-next-line no-console
                    console.log('');
                }
                return rushJsonFilename;
            }
            currentFolder = parentFolder;
            parentFolder = path.dirname(currentFolder);
        }
        // no match
        return undefined;
    }
    /**
     * This generates the unique names that are used to create temporary projects
     * in the Rush common folder.
     * NOTE: sortedProjectJsons is sorted by the caller.
     */
    static _generateTempNameForProject(projectJson, usedTempNames) {
        // If the name is "@ms/MyProject", extract the "MyProject" part
        const unscopedName = PackageNameParsers_1.PackageNameParsers.permissive.getUnscopedName(projectJson.packageName);
        // Generate a unique like name "@rush-temp/MyProject", or "@rush-temp/MyProject-2" if
        // there is a naming conflict
        let counter = 0;
        let tempProjectName = `${RushConstants_1.RushConstants.rushTempNpmScope}/${unscopedName}`;
        while (usedTempNames.has(tempProjectName)) {
            ++counter;
            tempProjectName = `${RushConstants_1.RushConstants.rushTempNpmScope}/${unscopedName}-${counter}`;
        }
        usedTempNames.add(tempProjectName);
        return tempProjectName;
    }
    /**
     * If someone adds a config file in the "common/rush/config" folder, it would be a bad
     * experience for Rush to silently ignore their file simply because they misspelled the
     * filename, or maybe it's an old format that's no longer supported.  The
     * _validateCommonRushConfigFolder() function makes sure that this folder only contains
     * recognized config files.
     */
    static _validateCommonRushConfigFolder(commonRushConfigFolder, packageManagerWrapper, experiments, subspacesFeatureEnabled) {
        if (!node_core_library_1.FileSystem.exists(commonRushConfigFolder)) {
            // eslint-disable-next-line no-console
            console.log(`Creating folder: ${commonRushConfigFolder}`);
            node_core_library_1.FileSystem.ensureFolder(commonRushConfigFolder);
            return;
        }
        for (const filename of node_core_library_1.FileSystem.readFolderItemNames(commonRushConfigFolder)) {
            // Ignore things that aren't actual files
            const stat = node_core_library_1.FileSystem.getLinkStatistics(path.join(commonRushConfigFolder, filename));
            if (!stat.isFile() && !stat.isSymbolicLink()) {
                continue;
            }
            // Ignore harmless file extensions
            const fileExtension = path.extname(filename);
            if (['.bak', '.disabled', '.md', '.old', '.orig'].indexOf(fileExtension) >= 0) {
                continue;
            }
            // Check if there are prohibited files when subspaces is enabled
            if (subspacesFeatureEnabled) {
                if (filename === RushConstants_1.RushConstants.pnpmfileV6Filename || filename === RushConstants_1.RushConstants.pnpmfileV1Filename) {
                    throw new Error('When the subspaces feature is enabled, a separate lockfile is stored in each subspace folder. ' +
                        `To avoid confusion, remove this file: ${commonRushConfigFolder}/${filename}`);
                }
            }
            // Ignore hidden files such as ".DS_Store"
            if (filename.startsWith('.')) {
                continue;
            }
            if (filename.startsWith('deploy-') && fileExtension === '.json') {
                // Ignore "rush deploy" files, which use the naming pattern "deploy-<scenario-name>.json".
                continue;
            }
            const knownSet = new Set(knownRushConfigFilenames.map((x) => x.toUpperCase()));
            // Add the shrinkwrap filename for the package manager to the known set.
            knownSet.add(packageManagerWrapper.shrinkwrapFilename.toUpperCase());
            // If the package manager is pnpm, then also add the pnpm file to the known set.
            if (packageManagerWrapper.packageManager === 'pnpm') {
                const pnpmPackageManager = packageManagerWrapper;
                knownSet.add(pnpmPackageManager.pnpmfileFilename.toUpperCase());
            }
            // Is the filename something we know?  If not, report an error.
            if (!knownSet.has(filename.toUpperCase())) {
                throw new Error(`An unrecognized file "${filename}" was found in the Rush config folder:` +
                    ` ${commonRushConfigFolder}`);
            }
        }
        const pinnedVersionsFilename = path.join(commonRushConfigFolder, RushConstants_1.RushConstants.pinnedVersionsFilename);
        if (node_core_library_1.FileSystem.exists(pinnedVersionsFilename)) {
            throw new Error('The "pinned-versions.json" config file is no longer supported;' +
                ' please move your settings to the "preferredVersions" field of a "common-versions.json" config file.' +
                ` (See the ${RushConstants_1.RushConstants.rushWebSiteUrl} documentation for details.)\n\n` +
                pinnedVersionsFilename);
        }
    }
    /**
     * The fully resolved path for the "autoinstallers" folder.
     * Example: `C:\MyRepo\common\autoinstallers`
     */
    get commonAutoinstallersFolder() {
        return path.join(this.commonFolder, 'autoinstallers');
    }
    /**
     * The folder where rush-plugin options json files are stored.
     * Example: `C:\MyRepo\common\config\rush-plugins`
     */
    get rushPluginOptionsFolder() {
        return path.join(this.commonFolder, 'config', 'rush-plugins');
    }
    /**
     * The full path of the temporary shrinkwrap file that is used during "rush install".
     * This file may get rewritten by the package manager during installation.
     * @remarks
     * This property merely reports the filename; the file itself may not actually exist.
     * Example: `C:\MyRepo\common\temp\npm-shrinkwrap.json` or `C:\MyRepo\common\temp\pnpm-lock.yaml`
     *
     * @deprecated Introduced with subspaces is subspace specific tempShrinkwrapFilename accessible from the Subspace class.
     */
    get tempShrinkwrapFilename() {
        if (this.subspacesFeatureEnabled) {
            throw new Error('tempShrinkwrapFilename() is not available when using subspaces. Use the subspace specific temp shrinkwrap filename.');
        }
        return path.join(this.commonTempFolder, this.shrinkwrapFilename);
    }
    /**
     * The full path of a backup copy of tempShrinkwrapFilename. This backup copy is made
     * before installation begins, and can be compared to determine how the package manager
     * modified tempShrinkwrapFilename.
     * @remarks
     * This property merely reports the filename; the file itself may not actually exist.
     * Example: `C:\MyRepo\common\temp\npm-shrinkwrap-preinstall.json`
     * or `C:\MyRepo\common\temp\pnpm-lock-preinstall.yaml`
     *
     * @deprecated Introduced with subspaces is subspace specific tempShrinkwrapPreinstallFilename accessible from the Subspace class.
     */
    get tempShrinkwrapPreinstallFilename() {
        if (this.subspacesFeatureEnabled) {
            throw new Error('tempShrinkwrapPreinstallFilename() is not available when using subspaces. Use the subspace specific temp shrinkwrap preinstall filename.');
        }
        const parsedPath = path.parse(this.tempShrinkwrapFilename);
        return path.join(parsedPath.dir, parsedPath.name + '-preinstall' + parsedPath.ext);
    }
    /**
     * Returns an English phrase such as "shrinkwrap file" that can be used in logging messages
     * to refer to the shrinkwrap file using appropriate terminology for the currently selected
     * package manager.
     */
    get shrinkwrapFilePhrase() {
        return `shrinkwrap file (${this.shrinkwrapFilename})`;
    }
    /**
     * The filename of the build dependency data file.  By default this is
     * called 'rush-link.json' resides in the Rush common folder.
     * Its data structure is defined by IRushLinkJson.
     *
     * Example: `C:\MyRepo\common\temp\rush-link.json`
     *
     * @deprecated The "rush-link.json" file was removed in Rush 5.30.0.
     * Use `RushConfigurationProject.localDependencyProjects` instead.
     */
    get rushLinkJsonFilename() {
        throw new Error('The "rush-link.json" file was removed in Rush 5.30.0. Use ' +
            'RushConfigurationProject.localDependencyProjects instead.');
    }
    /**
     * The default fully-qualified git remote branch of the repository. This helps "rush change" find the right branch to compare against.
     */
    get repositoryDefaultFullyQualifiedRemoteBranch() {
        return `${this.repositoryDefaultRemote}/${this.repositoryDefaultBranch}`;
    }
    get projects() {
        if (!this._projects) {
            this._initializeAndValidateLocalProjects();
        }
        return this._projects;
    }
    /**
     * @beta
     */
    get defaultSubspace() {
        // TODO: Enable the default subspace to be obtained without initializing the full set of all projects
        if (!this._projects) {
            this._initializeAndValidateLocalProjects();
        }
        const defaultSubspace = this.tryGetSubspace(RushConstants_1.RushConstants.defaultSubspaceName);
        if (!defaultSubspace) {
            throw new node_core_library_1.InternalError('Default subspace was not created');
        }
        return defaultSubspace;
    }
    /**
     * A list of all the available subspaces in this workspace.
     * @beta
     */
    get subspaces() {
        if (!this._projects) {
            this._initializeAndValidateLocalProjects();
        }
        return this._subspaces;
    }
    /**
     * @beta
     */
    tryGetSubspace(subspaceName) {
        var _a;
        if (!this._projects) {
            this._initializeAndValidateLocalProjects();
        }
        const subspace = this._subspacesByName.get(subspaceName);
        if (!subspace) {
            // If the name is not even valid, that is more important information than if the subspace doesn't exist
            SubspacesConfiguration_1.SubspacesConfiguration.requireValidSubspaceName(subspaceName, (_a = this.subspacesConfiguration) === null || _a === void 0 ? void 0 : _a.splitWorkspaceCompatibility);
        }
        return subspace;
    }
    /**
     * @beta
     */
    getSubspace(subspaceName) {
        const subspace = this.tryGetSubspace(subspaceName);
        if (!subspace) {
            throw new Error(`The specified subspace "${subspaceName}" does not exist`);
        }
        return subspace;
    }
    /**
     * Returns the set of subspaces that the given projects belong to
     * @beta
     */
    getSubspacesForProjects(projects) {
        if (!this._projects) {
            this._initializeAndValidateLocalProjects();
        }
        const subspaceSet = new Set();
        for (const project of projects) {
            subspaceSet.add(project.subspace);
        }
        return subspaceSet;
    }
    /**
     * @beta
     */
    get projectsByName() {
        if (!this._projectsByName) {
            this._initializeAndValidateLocalProjects();
        }
        return this._projectsByName;
    }
    /**
     * Obtains the mapping from custom tags to projects.
     * @beta
     */
    get projectsByTag() {
        if (!this._projectsByTag) {
            const projectsByTag = new Map();
            for (const project of this.projects) {
                for (const tag of project.tags) {
                    let collection = projectsByTag.get(tag);
                    if (!collection) {
                        projectsByTag.set(tag, (collection = new Set()));
                    }
                    collection.add(project);
                }
            }
            this._projectsByTag = projectsByTag;
        }
        return this._projectsByTag;
    }
    /**
     * Settings from the common-versions.json config file.
     * @remarks
     * If the common-versions.json file is missing, this property will not be undefined.
     * Instead it will be initialized in an empty state, and calling CommonVersionsConfiguration.save()
     * will create the file.
     *
     * @deprecated Use `getCommonVersions` instead, which gets the correct common version data
     * for a given active variant.
     */
    get commonVersions() {
        return this.defaultSubspace.getCommonVersions(undefined);
    }
    /**
     * Gets the currently-installed variant, if an installation has occurred.
     * For Rush operations which do not take a --variant parameter, this method
     * determines which variant, if any, was last specified when performing "rush install"
     * or "rush update".
     */
    async getCurrentlyInstalledVariantAsync() {
        var _a, _b;
        if (!this._currentVariantJsonLoadingPromise) {
            this._currentVariantJsonLoadingPromise = this._loadCurrentVariantJsonAsync();
        }
        return (_b = (_a = (await this._currentVariantJsonLoadingPromise)) === null || _a === void 0 ? void 0 : _a.variant) !== null && _b !== void 0 ? _b : undefined;
    }
    /**
     * @deprecated Use {@link Subspace.getCommonVersionsFilePath} instead
     */
    getCommonVersionsFilePath(subspace, variant) {
        return (subspace !== null && subspace !== void 0 ? subspace : this.defaultSubspace).getCommonVersionsFilePath(variant);
    }
    /**
     * @deprecated Use {@link Subspace.getCommonVersions} instead
     */
    getCommonVersions(subspace, variant) {
        return (subspace !== null && subspace !== void 0 ? subspace : this.defaultSubspace).getCommonVersions(variant);
    }
    /**
     * Returns a map of all direct dependencies that only have a single semantic version specifier.
     *
     * @param subspace - The subspace to use
     * @param variant - The name of the current variant in use by the active command.
     *
     * @returns A map of dependency name --\> version specifier for implicitly preferred versions.
     */
    getImplicitlyPreferredVersions(subspace, variant) {
        // TODO: During the next major release of Rush, replace this `require` call with a dynamic import, and
        // change this function to be async.
        const DependencyAnalyzerModule = require('../logic/DependencyAnalyzer');
        const dependencyAnalyzer = DependencyAnalyzerModule.DependencyAnalyzer.forRushConfiguration(this);
        const dependencyAnalysis = dependencyAnalyzer.getAnalysis(subspace, variant, false);
        return dependencyAnalysis.implicitlyPreferredVersionByPackageName;
    }
    /**
     * @deprecated Use {@link Subspace.getRepoStateFilePath} instead
     */
    getRepoStateFilePath(subspace) {
        return (subspace !== null && subspace !== void 0 ? subspace : this.defaultSubspace).getRepoStateFilePath();
    }
    /**
     * @deprecated Use {@link Subspace.getRepoState} instead
     */
    getRepoState(subspace) {
        return (subspace !== null && subspace !== void 0 ? subspace : this.defaultSubspace).getRepoState();
    }
    /**
     * @deprecated Use {@link Subspace.getCommittedShrinkwrapFilePath} instead
     */
    getCommittedShrinkwrapFilename(subspace, variant) {
        return (subspace !== null && subspace !== void 0 ? subspace : this.defaultSubspace).getCommittedShrinkwrapFilePath(variant);
    }
    /**
     * @deprecated Use {@link Subspace.getPnpmfilePath} instead
     */
    getPnpmfilePath(subspace, variant) {
        return (subspace !== null && subspace !== void 0 ? subspace : this.defaultSubspace).getPnpmfilePath(variant);
    }
    /**
     * Looks up a project in the projectsByName map.  If the project is not found,
     * then undefined is returned.
     */
    getProjectByName(projectName) {
        return this.projectsByName.get(projectName);
    }
    /**
     * This is used e.g. by command-line interfaces such as "rush build --to example".
     * If "example" is not a project name, then it also looks for a scoped name
     * like `@something/example`.  If exactly one project matches this heuristic, it
     * is returned.  Otherwise, undefined is returned.
     */
    findProjectByShorthandName(shorthandProjectName) {
        // Is there an exact match?
        let result = this.projectsByName.get(shorthandProjectName);
        if (result) {
            return result;
        }
        // Is there an approximate match?
        for (const project of this.projects) {
            if (this.packageNameParser.getUnscopedName(project.packageName) === shorthandProjectName) {
                if (result) {
                    // Ambiguous -- there is more than one match
                    return undefined;
                }
                else {
                    result = project;
                }
            }
        }
        return result;
    }
    /**
     * Looks up a project by its RushConfigurationProject.tempProjectName field.
     * @returns The found project, or undefined if no match was found.
     */
    findProjectByTempName(tempProjectName) {
        // Is there an approximate match?
        for (const project of this.projects) {
            if (project.tempProjectName === tempProjectName) {
                return project;
            }
        }
        return undefined;
    }
    /**
     * @returns An optimized lookup engine to find a project by its path relative to the specified root.
     * @beta
     */
    getProjectLookupForRoot(rootPath) {
        let pathTree = this._pathTrees.get(rootPath);
        if (!pathTree) {
            this._pathTrees.set(rootPath, (pathTree = new lookup_by_path_1.LookupByPath()));
            for (const project of this.projects) {
                const relativePath = path.relative(rootPath, project.projectFolder);
                pathTree.setItemFromSegments(lookup_by_path_1.LookupByPath.iteratePathSegments(relativePath, path.sep), project);
            }
        }
        return pathTree;
    }
    /**
     * Returns the project for which the specified path is underneath that project's folder.
     * If the path is not under any project's folder, returns undefined.
     */
    tryGetProjectForPath(currentFolderPath) {
        // TODO: Improve the method in which a package is found, perhaps without having to sort / loop though the entire package list
        const resolvedPath = path.resolve(currentFolderPath);
        const sortedProjects = this.projects.sort((a, b) => b.projectFolder.length - a.projectFolder.length);
        for (const project of sortedProjects) {
            if (node_core_library_1.Path.isUnderOrEqual(resolvedPath, project.projectFolder)) {
                return project;
            }
        }
        return undefined;
    }
    async _loadCurrentVariantJsonAsync() {
        try {
            return await node_core_library_1.JsonFile.loadAsync(this.currentVariantJsonFilePath);
        }
        catch (e) {
            if (!node_core_library_1.FileSystem.isNotExistError(e)) {
                throw e;
            }
        }
    }
}
exports.RushConfiguration = RushConfiguration;
RushConfiguration._jsonSchema = node_core_library_1.JsonSchema.fromLoadedObject(rush_schema_json_1.default);
//# sourceMappingURL=RushConfiguration.js.map