text: "use strict";
  // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
  // See LICENSE in the project root for license information.
  var __extends = (this && this.__extends) || (function () {
      var extendStatics = Object.setPrototypeOf ||
          ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
          function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
      return function (d, b) {
          extendStatics(d, b);
          function __() { this.constructor = d; }
          d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
      };
  })();
  Object.defineProperty(exports, "__esModule", { value: true });
  var lodash_1 = require("lodash");
  var semver = require("semver");
  /**
   * Type of version bumps
   * @alpha
   */
  var BumpType;
  (function (BumpType) {
      // No version bump
      BumpType[BumpType["none"] = 0] = "none";
      // Prerelease version bump
      BumpType[BumpType["prerelease"] = 1] = "prerelease";
      // Patch version bump
      BumpType[BumpType["patch"] = 2] = "patch";
      // Preminor version bump
      BumpType[BumpType["preminor"] = 3] = "preminor";
      // Minor version bump
      BumpType[BumpType["minor"] = 4] = "minor";
      // Major version bump
      BumpType[BumpType["major"] = 5] = "major";
  })(BumpType = exports.BumpType || (exports.BumpType = {}));
  /**
   * Version policy base type names
   * @alpha
   */
  var VersionPolicyDefinitionName;
  (function (VersionPolicyDefinitionName) {
      VersionPolicyDefinitionName[VersionPolicyDefinitionName["lockStepVersion"] = 0] = "lockStepVersion";
      VersionPolicyDefinitionName[VersionPolicyDefinitionName["individualVersion"] = 1] = "individualVersion";
  })(VersionPolicyDefinitionName = exports.VersionPolicyDefinitionName || (exports.VersionPolicyDefinitionName = {}));
  /**
   * This is the base class for version policy which controls how versions get bumped.
   * @alpha
   */
  var VersionPolicy = (function () {
      function VersionPolicy(versionPolicyJson) {
          this._policyName = versionPolicyJson.policyName;
          this._definitionName = VersionPolicyDefinitionName[versionPolicyJson.definitionName];
      }
      /**
       * Loads from version policy json
       *
       * @param versionPolicyJson - version policy Json
       */
      VersionPolicy.load = function (versionPolicyJson) {
          var definition = VersionPolicyDefinitionName[versionPolicyJson.definitionName];
          if (definition === VersionPolicyDefinitionName.lockStepVersion) {
              // tslint:disable-next-line:no-use-before-declare
              return new LockStepVersionPolicy(versionPolicyJson);
          }
          else if (definition === VersionPolicyDefinitionName.individualVersion) {
              // tslint:disable-next-line:no-use-before-declare
              return new IndividualVersionPolicy(versionPolicyJson);
          }
          return undefined;
      };
      Object.defineProperty(VersionPolicy.prototype, "policyName", {
          /**
           * Version policy name
           */
          get: function () {
              return this._policyName;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(VersionPolicy.prototype, "definitionName", {
          /**
           * Version policy definition name
           */
          get: function () {
              return this._definitionName;
          },
          enumerable: true,
          configurable: true
      });
      return VersionPolicy;
  }());
  exports.VersionPolicy = VersionPolicy;
  /**
   * This policy indicates all related projects should use the same version.
   * @alpha
   */
  var LockStepVersionPolicy = (function (_super) {
      __extends(LockStepVersionPolicy, _super);
      function LockStepVersionPolicy(versionPolicyJson) {
          var _this = _super.call(this, versionPolicyJson) || this;
          _this._version = new semver.SemVer(versionPolicyJson.version);
          _this._nextBump = BumpType[versionPolicyJson.nextBump];
          return _this;
      }
      Object.defineProperty(LockStepVersionPolicy.prototype, "version", {
          /**
           * The value of the lockstep version
           */
          get: function () {
              return this._version;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(LockStepVersionPolicy.prototype, "nextBump", {
          /**
           * The type of bump for next bump.
           */
          get: function () {
              return this._nextBump;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(LockStepVersionPolicy.prototype, "json", {
          /**
           * Serialized json for this policy
           */
          get: function () {
              return {
                  policyName: this.policyName,
                  definitionName: VersionPolicyDefinitionName[this.definitionName],
                  version: this.version.format(),
                  nextBump: BumpType[this.nextBump]
              };
          },
          enumerable: true,
          configurable: true
      });
      /**
       * Returns an updated package json that satisfies the version policy.
       *
       * @param project - input package json
       */
      LockStepVersionPolicy.prototype.ensure = function (project) {
          var packageVersion = new semver.SemVer(project.version);
          var compareResult = packageVersion.compare(this._version);
          if (compareResult === 0) {
              return undefined;
          }
          else if (compareResult > 0) {
              var errorMessage = "Version " + project.version + " in package " + project.name
                  + (" is higher than locked version " + this._version.format() + ".");
              throw new Error(errorMessage);
          }
          return this._updatePackageVersion(project, this._version);
      };
      /**
       * Bumps the version of the lockstep policy
       *
       * @param bumpType - Overwrite bump type in version-policy.json with the provided value.
       * @param identifier - Prerelease identifier if bump type is prerelease.
       */
      LockStepVersionPolicy.prototype.bump = function (bumpType, identifier) {
          this.version.inc(this._getReleaseType(bumpType || this.nextBump), identifier);
      };
      /**
       * Validates the specified version and throws if the version does not satisfy lockstep version.
       *
       * @param versionString - version string
       * @param packageName - package name
       */
      LockStepVersionPolicy.prototype.validate = function (versionString, packageName) {
          var versionToTest = new semver.SemVer(versionString, false);
          if (this.version.compare(versionToTest) !== 0) {
              throw new Error("Invalid version " + versionString + " in " + packageName);
          }
      };
      LockStepVersionPolicy.prototype._updatePackageVersion = function (project, newVersion) {
          var updatedProject = lodash_1.cloneDeep(project);
          updatedProject.version = newVersion.format();
          return updatedProject;
      };
      LockStepVersionPolicy.prototype._getReleaseType = function (bumpType) {
          // Eventually we should just use ReleaseType and get rid of bump type.
          return BumpType[bumpType];
      };
      return LockStepVersionPolicy;
  }(VersionPolicy));
  exports.LockStepVersionPolicy = LockStepVersionPolicy;
  /**
   * This policy indicates all related projects get version bump driven by their own changes.
   * @alpha
   */
  var IndividualVersionPolicy = (function (_super) {
      __extends(IndividualVersionPolicy, _super);
      function IndividualVersionPolicy(versionPolicyJson) {
          var _this = _super.call(this, versionPolicyJson) || this;
          _this._lockedMajor = versionPolicyJson.lockedMajor;
          return _this;
      }
      Object.defineProperty(IndividualVersionPolicy.prototype, "lockedMajor", {
          /**
           * The major version that has been locked
           */
          get: function () {
              return this._lockedMajor;
          },
          enumerable: true,
          configurable: true
      });
      Object.defineProperty(IndividualVersionPolicy.prototype, "json", {
          /**
           * Serialized json for this policy
           */
          get: function () {
              return {
                  policyName: this.policyName,
                  definitionName: VersionPolicyDefinitionName[this.definitionName],
                  lockedMajor: this.lockedMajor
              };
          },
          enumerable: true,
          configurable: true
      });
      /**
       * Returns an updated package json that satisfies the version policy.
       *
       * @param project - input package json
       */
      IndividualVersionPolicy.prototype.ensure = function (project) {
          if (this.lockedMajor) {
              var version = new semver.SemVer(project.version);
              if (version.major < this._lockedMajor) {
                  var updatedProject = lodash_1.cloneDeep(project);
                  updatedProject.version = this._lockedMajor + ".0.0";
                  return updatedProject;
              }
              else if (version.major > this._lockedMajor) {
                  var errorMessage = "Version " + project.version + " in package " + project.name
                      + (" is higher than locked major version " + this._lockedMajor + ".");
                  throw new Error(errorMessage);
              }
          }
          return undefined;
      };
      /**
       * Bumps version.
       * Individual version policy lets change files drive version bump. This method currently does not do anything.
       *
       * @param bumpType - bump type
       * @param identifier - prerelease id
       */
      IndividualVersionPolicy.prototype.bump = function (bumpType, identifier) {
          // individual version policy lets change files drive version bump.
      };
      /**
       * Validates the specified version and throws if the version does not satisfy the policy.
       *
       * @param versionString - version string
       * @param packageName - package name
       */
      IndividualVersionPolicy.prototype.validate = function (versionString, packageName) {
          var versionToTest = new semver.SemVer(versionString, false);
          if (this._lockedMajor !== undefined) {
              if (this._lockedMajor !== versionToTest.major) {
                  throw new Error("Invalid major version " + versionString + " in " + packageName);
              }
          }
      };
      return IndividualVersionPolicy;
  }(VersionPolicy));
  exports.IndividualVersionPolicy = IndividualVersionPolicy;
  
  //# sourceMappingURL=VersionPolicy.js.map
  