project/lib/specifications/SpecificationVersion.js

  1. import semver from "semver";
  2. const SPEC_VERSION_PATTERN = /^\d+\.\d+$/;
  3. const SUPPORTED_VERSIONS = [
  4. "0.1", "1.0", "1.1",
  5. "2.0", "2.1", "2.2", "2.3", "2.4", "2.5", "2.6",
  6. "3.0", "3.1", "3.2",
  7. "4.0"
  8. ];
  9. /**
  10. * Helper class representing a Specification Version. Featuring helper functions for easy comparison
  11. * of versions.
  12. *
  13. * @public
  14. * @class
  15. * @alias @ui5/project/specifications/utils/SpecificationVersion
  16. */
  17. class SpecificationVersion {
  18. #specVersion;
  19. #semverVersion;
  20. /**
  21. * @public
  22. * @param {string} specVersion Specification Version to use for all comparison operations
  23. * @throws {Error} Throws if provided Specification Version is not supported by this version of @ui5/project
  24. */
  25. constructor(specVersion) {
  26. this.#specVersion = specVersion;
  27. this.#semverVersion = getSemverCompatibleVersion(specVersion); // Throws for unsupported versions
  28. }
  29. /**
  30. * Returns the Specification Version
  31. *
  32. * @public
  33. * @returns {string} Specification Version
  34. */
  35. toString() {
  36. return this.#specVersion;
  37. }
  38. /**
  39. * Returns the major-version of the instance's Specification Version
  40. *
  41. * @public
  42. * @returns {integer} Major version
  43. */
  44. major() {
  45. return semver.major(this.#semverVersion);
  46. }
  47. /**
  48. * Returns the minor-version of the instance's Specification Version
  49. *
  50. * @public
  51. * @returns {integer} Minor version
  52. */
  53. minor() {
  54. return semver.minor(this.#semverVersion);
  55. }
  56. /**
  57. * Test whether the instance's Specification Version falls into the provided range
  58. *
  59. * @public
  60. * @param {string} range [Semver]{@link https://www.npmjs.com/package/semver}-style version range,
  61. * for example <code>2.2 - 2.4</code> or <code>=3.0</code>
  62. * @returns {boolean} True if the instance's Specification Version falls into the provided range
  63. */
  64. satisfies(range) {
  65. return semver.satisfies(this.#semverVersion, range);
  66. }
  67. /**
  68. * Test whether the instance's Specification Version is greater than the provided test version
  69. *
  70. * @public
  71. * @param {string} testVersion A Specification Version to compare the instance's Specification Version to
  72. * @returns {boolean} True if the instance's Specification Version is greater than the provided version
  73. */
  74. gt(testVersion) {
  75. return handleSemverComparator(semver.gt, this.#semverVersion, testVersion);
  76. }
  77. /**
  78. * Test whether the instance's Specification Version is greater than or equal to the provided test version
  79. *
  80. * @public
  81. * @param {string} testVersion A Specification Version to compare the instance's Specification Version to
  82. * @returns {boolean} True if the instance's Specification Version is greater than or equal to the provided version
  83. */
  84. gte(testVersion) {
  85. return handleSemverComparator(semver.gte, this.#semverVersion, testVersion);
  86. }
  87. /**
  88. * Test whether the instance's Specification Version is smaller than the provided test version
  89. *
  90. * @public
  91. * @param {string} testVersion A Specification Version to compare the instance's Specification Version to
  92. * @returns {boolean} True if the instance's Specification Version is smaller than the provided version
  93. */
  94. lt(testVersion) {
  95. return handleSemverComparator(semver.lt, this.#semverVersion, testVersion);
  96. }
  97. /**
  98. * Test whether the instance's Specification Version is smaller than or equal to the provided test version
  99. *
  100. * @public
  101. * @param {string} testVersion A Specification Version to compare the instance's Specification Version to
  102. * @returns {boolean} True if the instance's Specification Version is smaller than or equal to the provided version
  103. */
  104. lte(testVersion) {
  105. return handleSemverComparator(semver.lte, this.#semverVersion, testVersion);
  106. }
  107. /**
  108. * Test whether the instance's Specification Version is equal to the provided test version
  109. *
  110. * @public
  111. * @param {string} testVersion A Specification Version to compare the instance's Specification Version to
  112. * @returns {boolean} True if the instance's Specification Version is equal to the provided version
  113. */
  114. eq(testVersion) {
  115. return handleSemverComparator(semver.eq, this.#semverVersion, testVersion);
  116. }
  117. /**
  118. * Test whether the instance's Specification Version is not equal to the provided test version
  119. *
  120. * @public
  121. * @param {string} testVersion A Specification Version to compare the instance's Specification Version to
  122. * @returns {boolean} True if the instance's Specification Version is not equal to the provided version
  123. */
  124. neq(testVersion) {
  125. return handleSemverComparator(semver.neq, this.#semverVersion, testVersion);
  126. }
  127. /**
  128. * Test whether the provided Specification Version is supported by this version of @ui5/project
  129. *
  130. * @public
  131. * @param {string} testVersion A Specification Version to compare the instance's Specification Version to
  132. * @returns {boolean} True if the provided Specification Version is supported
  133. */
  134. static isSupportedSpecVersion(testVersion) {
  135. return SUPPORTED_VERSIONS.includes(testVersion);
  136. }
  137. /**
  138. * Returns the major-version of the provided Specification Version
  139. *
  140. * @public
  141. * @param {string} specVersion Specification Version
  142. * @returns {integer} Major version
  143. */
  144. static major(specVersion) {
  145. const comparator = new SpecificationVersion(specVersion);
  146. return comparator.major();
  147. }
  148. /**
  149. * Returns the minor-version of the provided Specification Version
  150. *
  151. * @public
  152. * @param {string} specVersion Specification Version
  153. * @returns {integer} Minor version
  154. */
  155. static minor(specVersion) {
  156. const comparator = new SpecificationVersion(specVersion);
  157. return comparator.minor();
  158. }
  159. /**
  160. * Test whether the provided Specification Version falls into the provided range
  161. *
  162. * @public
  163. * @param {string} specVersion Specification Version
  164. * @param {string} range [Semver]{@link https://www.npmjs.com/package/semver}-style version range,
  165. * for example <code>2.2 - 2.4</code>
  166. * @returns {boolean} True if the provided Specification Version falls into the provided range
  167. */
  168. static satisfies(specVersion, range) {
  169. const comparator = new SpecificationVersion(specVersion);
  170. return comparator.satisfies(range);
  171. }
  172. /**
  173. * Test whether the provided Specification Version is greater than the provided test version
  174. *
  175. * @public
  176. * @param {string} specVersion Specification Version
  177. * @param {string} testVersion A Specification Version to compare the provided Specification Version to
  178. * @returns {boolean} True if the provided Specification Version is greater than the provided version
  179. */
  180. static gt(specVersion, testVersion) {
  181. const comparator = new SpecificationVersion(specVersion);
  182. return comparator.gt(testVersion);
  183. }
  184. /**
  185. * Test whether the provided Specification Version is greater than or equal to the provided test version
  186. *
  187. * @public
  188. * @param {string} specVersion Specification Version
  189. * @param {string} testVersion A Specification Version to compare the provided Specification Version to
  190. * @returns {boolean} True if the provided Specification Version is greater than or equal to the provided version
  191. */
  192. static gte(specVersion, testVersion) {
  193. const comparator = new SpecificationVersion(specVersion);
  194. return comparator.gte(testVersion);
  195. }
  196. /**
  197. * Test whether the provided Specification Version is smaller than the provided test version
  198. *
  199. * @public
  200. * @param {string} specVersion Specification Version
  201. * @param {string} testVersion A Specification Version to compare the provided Specification Version to
  202. * @returns {boolean} True if the provided Specification Version is smaller than the provided version
  203. */
  204. static lt(specVersion, testVersion) {
  205. const comparator = new SpecificationVersion(specVersion);
  206. return comparator.lt(testVersion);
  207. }
  208. /**
  209. * Test whether the provided Specification Version is smaller than or equal to the provided test version
  210. *
  211. * @public
  212. * @param {string} specVersion Specification Version
  213. * @param {string} testVersion A Specification Version to compare the provided Specification Version to
  214. * @returns {boolean} True if the provided Specification Version is smaller than or equal to the provided version
  215. */
  216. static lte(specVersion, testVersion) {
  217. const comparator = new SpecificationVersion(specVersion);
  218. return comparator.lte(testVersion);
  219. }
  220. /**
  221. * Test whether the provided Specification Version is equal to the provided test version
  222. *
  223. * @public
  224. * @param {string} specVersion Specification Version
  225. * @param {string} testVersion A Specification Version to compare the provided Specification Version to
  226. * @returns {boolean} True if the provided Specification Version is equal to the provided version
  227. */
  228. static eq(specVersion, testVersion) {
  229. const comparator = new SpecificationVersion(specVersion);
  230. return comparator.eq(testVersion);
  231. }
  232. /**
  233. * Test whether the provided Specification Version is not equal to the provided test version
  234. *
  235. * @public
  236. * @param {string} specVersion Specification Version
  237. * @param {string} testVersion A Specification Version to compare the provided Specification Version to
  238. * @returns {boolean} True if the provided Specification Version is not equal to the provided version
  239. */
  240. static neq(specVersion, testVersion) {
  241. const comparator = new SpecificationVersion(specVersion);
  242. return comparator.neq(testVersion);
  243. }
  244. /**
  245. * Creates an array of Specification Versions that match with the provided range. This is mainly used
  246. * for testing purposes. I.e. to execute identical tests for a range of specification versions.
  247. *
  248. * @public
  249. * @param {string} range [Semver]{@link https://www.npmjs.com/package/semver}-style version range,
  250. * for example <code>2.2 - 2.4</code> or <code>=3.0</code>
  251. * @returns {string[]} Array of versions that match the specified range
  252. */
  253. static getVersionsForRange(range) {
  254. return SUPPORTED_VERSIONS.filter((specVersion) => {
  255. const comparator = new SpecificationVersion(specVersion);
  256. return comparator.satisfies(range);
  257. });
  258. }
  259. }
  260. function getUnsupportedSpecVersionMessage(specVersion) {
  261. return `Unsupported Specification Version ${specVersion} defined. Your UI5 CLI installation might be outdated. ` +
  262. `For details, see https://sap.github.io/ui5-tooling/pages/Configuration/#specification-versions`;
  263. }
  264. function getSemverCompatibleVersion(specVersion) {
  265. if (SpecificationVersion.isSupportedSpecVersion(specVersion)) {
  266. return specVersion + ".0";
  267. }
  268. throw new Error(getUnsupportedSpecVersionMessage(specVersion));
  269. }
  270. function handleSemverComparator(comparator, baseVersion, testVersion) {
  271. if (SPEC_VERSION_PATTERN.test(testVersion)) {
  272. const a = baseVersion;
  273. const b = testVersion + ".0";
  274. return comparator(a, b);
  275. }
  276. throw new Error("Invalid spec version expectation given in comparator: " + testVersion);
  277. }
  278. export default SpecificationVersion;
  279. // Export local function for testing only
  280. export const __localFunctions__ = (process.env.NODE_ENV === "test") ?
  281. {getSemverCompatibleVersion, handleSemverComparator} : /* istanbul ignore next */ undefined;