"use strict";
exports.id = "VersionManager";
exports.ids = ["VersionManager"];
exports.modules = {

/***/ 47087
/*!******************************************************!*\
  !*** ./lib-intermediate-esm/logic/VersionManager.js ***!
  \******************************************************/
(__unused_webpack_module, __webpack_exports__, __webpack_require__) {

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   VersionManager: () => (/* binding */ VersionManager)
/* harmony export */ });
/* harmony import */ var node_path__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! node:path */ 176760);
/* harmony import */ var node_path__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(node_path__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var semver__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! semver */ 338064);
/* harmony import */ var semver__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(semver__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _rushstack_node_core_library__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @rushstack/node-core-library */ 277275);
/* harmony import */ var _rushstack_node_core_library__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_rushstack_node_core_library__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _api_VersionPolicy__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../api/VersionPolicy */ 861558);
/* harmony import */ var _api_ChangeFile__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ../api/ChangeFile */ 804004);
/* harmony import */ var _api_ChangeManagement__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ../api/ChangeManagement */ 683853);
/* harmony import */ var _api_RushConfiguration__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ../api/RushConfiguration */ 103220);
/* harmony import */ var _PublishUtilities__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./PublishUtilities */ 504381);
/* harmony import */ var _ChangeManager__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./ChangeManager */ 543789);
/* harmony import */ var _DependencySpecifier__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./DependencySpecifier */ 508969);
/* harmony import */ var _utilities_objectUtilities__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(/*! ../utilities/objectUtilities */ 483809);
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.











class VersionManager {
    constructor(rushConfiguration, userEmail, versionPolicyConfiguration) {
        this._rushConfiguration = rushConfiguration;
        this._userEmail = userEmail;
        this._versionPolicyConfiguration = versionPolicyConfiguration
            ? versionPolicyConfiguration
            : this._rushConfiguration.versionPolicyConfiguration;
        this.updatedProjects = new Map();
        this.changeFiles = new Map();
    }
    /**
     * Ensures project versions follow the provided version policy. If version policy is not
     * provided, all projects will have their version checked according to the associated version policy.
     * package.json files will be updated if needed.
     * This method does not commit changes.
     * @param versionPolicyName -- version policy name
     * @param shouldCommit -- should update files to disk
     * @param force -- update even when project version is higher than policy version.
     */
    ensure(versionPolicyName, shouldCommit, force) {
        this._ensure(versionPolicyName, shouldCommit, force);
    }
    /**
     * Bumps versions following version policies.
     *
     * @param lockStepVersionPolicyName - a specified lock step version policy name. Without this value,
     * versions for all lock step policies and all individual policies will be bumped.
     * With this value, only the specified lock step policy will be bumped along with all individual policies.
     * @param bumpType - overrides the default bump type and only works for lock step policy
     * @param identifier - overrides the prerelease identifier and only works for lock step policy
     * @param shouldCommit - whether the changes will be written to disk
     */
    async bumpAsync(terminal, lockStepVersionPolicyName, bumpType, identifier, shouldCommit) {
        // Bump all the lock step version policies.
        this._versionPolicyConfiguration.bump(lockStepVersionPolicyName, bumpType, identifier, shouldCommit);
        // Update packages and generate change files due to lock step bump.
        this._ensure(lockStepVersionPolicyName, shouldCommit);
        // Refresh rush configuration since we may have modified the package.json versions
        // when calling this._ensure(...)
        this._rushConfiguration = _api_RushConfiguration__WEBPACK_IMPORTED_MODULE_6__.RushConfiguration.loadFromConfigurationFile(this._rushConfiguration.rushJsonFile);
        // Update projects based on individual policies
        const changeManager = new _ChangeManager__WEBPACK_IMPORTED_MODULE_8__.ChangeManager(this._rushConfiguration, this._getManuallyVersionedProjects());
        await changeManager.loadAsync();
        if (changeManager.hasChanges()) {
            changeManager.validateChanges(this._versionPolicyConfiguration);
            changeManager.apply(!!shouldCommit).forEach((packageJson) => {
                this.updatedProjects.set(packageJson.name, packageJson);
            });
            await changeManager.updateChangelogAsync(terminal, !!shouldCommit);
        }
        // Refresh rush configuration again, since we've further modified the package.json files
        // by calling changeManager.apply(...)
        this._rushConfiguration = _api_RushConfiguration__WEBPACK_IMPORTED_MODULE_6__.RushConfiguration.loadFromConfigurationFile(this._rushConfiguration.rushJsonFile);
    }
    _ensure(versionPolicyName, shouldCommit, force) {
        this._updateVersionsByPolicy(versionPolicyName, force);
        let changed = false;
        do {
            changed = false;
            // Update all dependencies if needed.
            const dependenciesUpdated = this._updateDependencies();
            changed = changed || dependenciesUpdated;
        } while (changed);
        if (shouldCommit) {
            this._updatePackageJsonFiles();
            this.changeFiles.forEach((changeFile) => {
                changeFile.writeSync();
            });
        }
    }
    _getManuallyVersionedProjects() {
        const lockStepVersionPolicyNames = new Set();
        this._versionPolicyConfiguration.versionPolicies.forEach((versionPolicy) => {
            if (versionPolicy instanceof _api_VersionPolicy__WEBPACK_IMPORTED_MODULE_3__.LockStepVersionPolicy && versionPolicy.nextBump !== undefined) {
                lockStepVersionPolicyNames.add(versionPolicy.policyName);
            }
        });
        const lockStepProjectNames = new Set();
        this._rushConfiguration.projects.forEach((rushProject) => {
            if (lockStepVersionPolicyNames.has(rushProject.versionPolicyName)) {
                lockStepProjectNames.add(rushProject.packageName);
            }
        });
        return lockStepProjectNames;
    }
    _updateVersionsByPolicy(versionPolicyName, force) {
        let changed = false;
        // Update versions based on version policy
        this._rushConfiguration.projects.forEach((rushProject) => {
            var _a;
            const projectVersionPolicyName = rushProject.versionPolicyName;
            if (projectVersionPolicyName &&
                (!versionPolicyName || projectVersionPolicyName === versionPolicyName)) {
                const versionPolicy = this._versionPolicyConfiguration.getVersionPolicy(projectVersionPolicyName);
                const oldVersion = ((_a = this.updatedProjects.get(rushProject.packageName)) === null || _a === void 0 ? void 0 : _a.version) || rushProject.packageJson.version;
                const updatedProject = versionPolicy.ensure(rushProject.packageJson, force);
                changed = changed || (updatedProject === null || updatedProject === void 0 ? void 0 : updatedProject.version) !== oldVersion;
                if (updatedProject) {
                    this.updatedProjects.set(updatedProject.name, updatedProject);
                    // No need to create an entry for prerelease version bump.
                    if (!this._isPrerelease(updatedProject.version) && rushProject.isMainProject) {
                        this._addChangeInfo(updatedProject.name, [this._createChangeInfo(updatedProject, rushProject)]);
                    }
                }
            }
        });
        return changed;
    }
    _isPrerelease(version) {
        return !!semver__WEBPACK_IMPORTED_MODULE_1__.prerelease(version);
    }
    _addChangeInfo(packageName, changeInfos) {
        if (!changeInfos.length) {
            return;
        }
        let changeFile = this.changeFiles.get(packageName);
        if (!changeFile) {
            changeFile = new _api_ChangeFile__WEBPACK_IMPORTED_MODULE_4__.ChangeFile({
                changes: [],
                packageName: packageName,
                email: this._userEmail
            }, this._rushConfiguration);
            this.changeFiles.set(packageName, changeFile);
        }
        changeInfos.forEach((changeInfo) => {
            changeFile.addChange(changeInfo);
        });
    }
    _updateDependencies() {
        let updated = false;
        this._rushConfiguration.projects.forEach((rushProject) => {
            let clonedProject = this.updatedProjects.get(rushProject.packageName);
            let projectVersionChanged = true;
            if (!clonedProject) {
                clonedProject = (0,_utilities_objectUtilities__WEBPACK_IMPORTED_MODULE_10__.cloneDeep)(rushProject.packageJson);
                projectVersionChanged = false;
            }
            const dependenciesUpdated = this._updateProjectAllDependencies(rushProject, clonedProject, projectVersionChanged);
            updated = updated || dependenciesUpdated;
        });
        return updated;
    }
    _updateProjectAllDependencies(rushProject, clonedProject, projectVersionChanged) {
        if (!clonedProject.dependencies && !clonedProject.devDependencies) {
            return false;
        }
        const changes = [];
        let updated = false;
        if (this._updateProjectDependencies(clonedProject.dependencies, changes, clonedProject, rushProject, projectVersionChanged)) {
            updated = true;
        }
        if (this._updateProjectDependencies(clonedProject.devDependencies, changes, clonedProject, rushProject, projectVersionChanged)) {
            updated = true;
        }
        if (this._updateProjectDependencies(clonedProject.peerDependencies, changes, clonedProject, rushProject, projectVersionChanged)) {
            updated = true;
        }
        if (updated) {
            this.updatedProjects.set(clonedProject.name, clonedProject);
            this._addChangeInfo(clonedProject.name, changes);
        }
        return updated;
    }
    _updateProjectDependencies(dependencies, changes, clonedProject, rushProject, projectVersionChanged) {
        if (!dependencies) {
            return false;
        }
        let updated = false;
        this.updatedProjects.forEach((updatedDependentProject, updatedDependentProjectName) => {
            if (dependencies[updatedDependentProjectName]) {
                if (rushProject.decoupledLocalDependencies.has(updatedDependentProjectName)) {
                    // Skip if cyclic
                    // eslint-disable-next-line no-console
                    console.log(`Found cyclic ${rushProject.packageName} ${updatedDependentProjectName}`);
                    return;
                }
                const oldDependencyVersion = dependencies[updatedDependentProjectName];
                const newDependencyVersion = _PublishUtilities__WEBPACK_IMPORTED_MODULE_7__.PublishUtilities.getNewDependencyVersion(dependencies, updatedDependentProjectName, updatedDependentProject.version);
                if (newDependencyVersion !== oldDependencyVersion) {
                    updated = true;
                    if (this._shouldTrackDependencyChange(rushProject, updatedDependentProjectName)) {
                        this._trackDependencyChange(changes, clonedProject, projectVersionChanged, updatedDependentProject, oldDependencyVersion, newDependencyVersion);
                    }
                    dependencies[updatedDependentProjectName] = newDependencyVersion;
                }
            }
        });
        return updated;
    }
    _shouldTrackDependencyChange(rushProject, dependencyName) {
        const dependencyRushProject = this._rushConfiguration.projectsByName.get(dependencyName);
        return (!!dependencyRushProject &&
            rushProject.shouldPublish &&
            (!rushProject.versionPolicy ||
                !rushProject.versionPolicy.isLockstepped ||
                (rushProject.isMainProject &&
                    dependencyRushProject.versionPolicyName !== rushProject.versionPolicyName)));
    }
    _trackDependencyChange(changes, clonedProject, projectVersionChanged, updatedDependentProject, oldDependencyVersion, newDependencyVersion) {
        const oldSpecifier = _DependencySpecifier__WEBPACK_IMPORTED_MODULE_9__.DependencySpecifier.parseWithCache(updatedDependentProject.name, oldDependencyVersion);
        if (!semver__WEBPACK_IMPORTED_MODULE_1__.satisfies(updatedDependentProject.version, oldSpecifier.versionSpecifier) &&
            !projectVersionChanged) {
            this._addChange(changes, {
                changeType: _api_ChangeManagement__WEBPACK_IMPORTED_MODULE_5__.ChangeType.patch,
                packageName: clonedProject.name
            });
        }
        // If current version is not a prerelease version and new dependency is also not a prerelease version,
        // add change entry. Otherwise, too many changes will be created for frequent releases.
        if (!this._isPrerelease(updatedDependentProject.version) && !this._isPrerelease(clonedProject.version)) {
            this._addChange(changes, {
                changeType: _api_ChangeManagement__WEBPACK_IMPORTED_MODULE_5__.ChangeType.dependency,
                comment: `Dependency ${updatedDependentProject.name} version bump from ${oldDependencyVersion}` +
                    ` to ${newDependencyVersion}.`,
                packageName: clonedProject.name
            });
        }
    }
    _addChange(changes, newChange) {
        const exists = changes.some((changeInfo) => {
            return (changeInfo.author === newChange.author &&
                changeInfo.changeType === newChange.changeType &&
                changeInfo.comment === newChange.comment &&
                changeInfo.commit === newChange.commit &&
                changeInfo.packageName === newChange.packageName &&
                changeInfo.type === newChange.type);
        });
        if (!exists) {
            changes.push(newChange);
        }
    }
    _updatePackageJsonFiles() {
        this.updatedProjects.forEach((newPackageJson, packageName) => {
            const rushProject = this._rushConfiguration.getProjectByName(packageName);
            // Update package.json
            if (rushProject) {
                const packagePath = node_path__WEBPACK_IMPORTED_MODULE_0__.join(rushProject.projectFolder, _rushstack_node_core_library__WEBPACK_IMPORTED_MODULE_2__.FileConstants.PackageJson);
                _rushstack_node_core_library__WEBPACK_IMPORTED_MODULE_2__.JsonFile.save(newPackageJson, packagePath, { updateExistingFile: true });
            }
        });
    }
    _createChangeInfo(newPackageJson, rushProject) {
        return {
            changeType: _api_ChangeManagement__WEBPACK_IMPORTED_MODULE_5__.ChangeType.none,
            newVersion: newPackageJson.version,
            packageName: newPackageJson.name,
            comment: ''
        };
    }
}
//# sourceMappingURL=VersionManager.js.map

/***/ }

};
;
//# sourceMappingURL=VersionManager.js.map