You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

412 lines
15 KiB

4 years ago
  1. semver(1) -- The semantic versioner for npm
  2. ===========================================
  3. ## Install
  4. ```bash
  5. npm install --save semver
  6. ````
  7. ## Usage
  8. As a node module:
  9. ```js
  10. const semver = require('semver')
  11. semver.valid('1.2.3') // '1.2.3'
  12. semver.valid('a.b.c') // null
  13. semver.clean(' =v1.2.3 ') // '1.2.3'
  14. semver.satisfies('1.2.3', '1.x || >=2.5.0 || 5.0.0 - 7.2.3') // true
  15. semver.gt('1.2.3', '9.8.7') // false
  16. semver.lt('1.2.3', '9.8.7') // true
  17. semver.minVersion('>=1.0.0') // '1.0.0'
  18. semver.valid(semver.coerce('v2')) // '2.0.0'
  19. semver.valid(semver.coerce('42.6.7.9.3-alpha')) // '42.6.7'
  20. ```
  21. As a command-line utility:
  22. ```
  23. $ semver -h
  24. A JavaScript implementation of the https://semver.org/ specification
  25. Copyright Isaac Z. Schlueter
  26. Usage: semver [options] <version> [<version> [...]]
  27. Prints valid versions sorted by SemVer precedence
  28. Options:
  29. -r --range <range>
  30. Print versions that match the specified range.
  31. -i --increment [<level>]
  32. Increment a version by the specified level. Level can
  33. be one of: major, minor, patch, premajor, preminor,
  34. prepatch, or prerelease. Default level is 'patch'.
  35. Only one version may be specified.
  36. --preid <identifier>
  37. Identifier to be used to prefix premajor, preminor,
  38. prepatch or prerelease version increments.
  39. -l --loose
  40. Interpret versions and ranges loosely
  41. -p --include-prerelease
  42. Always include prerelease versions in range matching
  43. -c --coerce
  44. Coerce a string into SemVer if possible
  45. (does not imply --loose)
  46. Program exits successfully if any valid version satisfies
  47. all supplied ranges, and prints all satisfying versions.
  48. If no satisfying versions are found, then exits failure.
  49. Versions are printed in ascending order, so supplying
  50. multiple versions to the utility will just sort them.
  51. ```
  52. ## Versions
  53. A "version" is described by the `v2.0.0` specification found at
  54. <https://semver.org/>.
  55. A leading `"="` or `"v"` character is stripped off and ignored.
  56. ## Ranges
  57. A `version range` is a set of `comparators` which specify versions
  58. that satisfy the range.
  59. A `comparator` is composed of an `operator` and a `version`. The set
  60. of primitive `operators` is:
  61. * `<` Less than
  62. * `<=` Less than or equal to
  63. * `>` Greater than
  64. * `>=` Greater than or equal to
  65. * `=` Equal. If no operator is specified, then equality is assumed,
  66. so this operator is optional, but MAY be included.
  67. For example, the comparator `>=1.2.7` would match the versions
  68. `1.2.7`, `1.2.8`, `2.5.3`, and `1.3.9`, but not the versions `1.2.6`
  69. or `1.1.0`.
  70. Comparators can be joined by whitespace to form a `comparator set`,
  71. which is satisfied by the **intersection** of all of the comparators
  72. it includes.
  73. A range is composed of one or more comparator sets, joined by `||`. A
  74. version matches a range if and only if every comparator in at least
  75. one of the `||`-separated comparator sets is satisfied by the version.
  76. For example, the range `>=1.2.7 <1.3.0` would match the versions
  77. `1.2.7`, `1.2.8`, and `1.2.99`, but not the versions `1.2.6`, `1.3.0`,
  78. or `1.1.0`.
  79. The range `1.2.7 || >=1.2.9 <2.0.0` would match the versions `1.2.7`,
  80. `1.2.9`, and `1.4.6`, but not the versions `1.2.8` or `2.0.0`.
  81. ### Prerelease Tags
  82. If a version has a prerelease tag (for example, `1.2.3-alpha.3`) then
  83. it will only be allowed to satisfy comparator sets if at least one
  84. comparator with the same `[major, minor, patch]` tuple also has a
  85. prerelease tag.
  86. For example, the range `>1.2.3-alpha.3` would be allowed to match the
  87. version `1.2.3-alpha.7`, but it would *not* be satisfied by
  88. `3.4.5-alpha.9`, even though `3.4.5-alpha.9` is technically "greater
  89. than" `1.2.3-alpha.3` according to the SemVer sort rules. The version
  90. range only accepts prerelease tags on the `1.2.3` version. The
  91. version `3.4.5` *would* satisfy the range, because it does not have a
  92. prerelease flag, and `3.4.5` is greater than `1.2.3-alpha.7`.
  93. The purpose for this behavior is twofold. First, prerelease versions
  94. frequently are updated very quickly, and contain many breaking changes
  95. that are (by the author's design) not yet fit for public consumption.
  96. Therefore, by default, they are excluded from range matching
  97. semantics.
  98. Second, a user who has opted into using a prerelease version has
  99. clearly indicated the intent to use *that specific* set of
  100. alpha/beta/rc versions. By including a prerelease tag in the range,
  101. the user is indicating that they are aware of the risk. However, it
  102. is still not appropriate to assume that they have opted into taking a
  103. similar risk on the *next* set of prerelease versions.
  104. Note that this behavior can be suppressed (treating all prerelease
  105. versions as if they were normal versions, for the purpose of range
  106. matching) by setting the `includePrerelease` flag on the options
  107. object to any
  108. [functions](https://github.com/npm/node-semver#functions) that do
  109. range matching.
  110. #### Prerelease Identifiers
  111. The method `.inc` takes an additional `identifier` string argument that
  112. will append the value of the string as a prerelease identifier:
  113. ```javascript
  114. semver.inc('1.2.3', 'prerelease', 'beta')
  115. // '1.2.4-beta.0'
  116. ```
  117. command-line example:
  118. ```bash
  119. $ semver 1.2.3 -i prerelease --preid beta
  120. 1.2.4-beta.0
  121. ```
  122. Which then can be used to increment further:
  123. ```bash
  124. $ semver 1.2.4-beta.0 -i prerelease
  125. 1.2.4-beta.1
  126. ```
  127. ### Advanced Range Syntax
  128. Advanced range syntax desugars to primitive comparators in
  129. deterministic ways.
  130. Advanced ranges may be combined in the same way as primitive
  131. comparators using white space or `||`.
  132. #### Hyphen Ranges `X.Y.Z - A.B.C`
  133. Specifies an inclusive set.
  134. * `1.2.3 - 2.3.4` := `>=1.2.3 <=2.3.4`
  135. If a partial version is provided as the first version in the inclusive
  136. range, then the missing pieces are replaced with zeroes.
  137. * `1.2 - 2.3.4` := `>=1.2.0 <=2.3.4`
  138. If a partial version is provided as the second version in the
  139. inclusive range, then all versions that start with the supplied parts
  140. of the tuple are accepted, but nothing that would be greater than the
  141. provided tuple parts.
  142. * `1.2.3 - 2.3` := `>=1.2.3 <2.4.0`
  143. * `1.2.3 - 2` := `>=1.2.3 <3.0.0`
  144. #### X-Ranges `1.2.x` `1.X` `1.2.*` `*`
  145. Any of `X`, `x`, or `*` may be used to "stand in" for one of the
  146. numeric values in the `[major, minor, patch]` tuple.
  147. * `*` := `>=0.0.0` (Any version satisfies)
  148. * `1.x` := `>=1.0.0 <2.0.0` (Matching major version)
  149. * `1.2.x` := `>=1.2.0 <1.3.0` (Matching major and minor versions)
  150. A partial version range is treated as an X-Range, so the special
  151. character is in fact optional.
  152. * `""` (empty string) := `*` := `>=0.0.0`
  153. * `1` := `1.x.x` := `>=1.0.0 <2.0.0`
  154. * `1.2` := `1.2.x` := `>=1.2.0 <1.3.0`
  155. #### Tilde Ranges `~1.2.3` `~1.2` `~1`
  156. Allows patch-level changes if a minor version is specified on the
  157. comparator. Allows minor-level changes if not.
  158. * `~1.2.3` := `>=1.2.3 <1.(2+1).0` := `>=1.2.3 <1.3.0`
  159. * `~1.2` := `>=1.2.0 <1.(2+1).0` := `>=1.2.0 <1.3.0` (Same as `1.2.x`)
  160. * `~1` := `>=1.0.0 <(1+1).0.0` := `>=1.0.0 <2.0.0` (Same as `1.x`)
  161. * `~0.2.3` := `>=0.2.3 <0.(2+1).0` := `>=0.2.3 <0.3.0`
  162. * `~0.2` := `>=0.2.0 <0.(2+1).0` := `>=0.2.0 <0.3.0` (Same as `0.2.x`)
  163. * `~0` := `>=0.0.0 <(0+1).0.0` := `>=0.0.0 <1.0.0` (Same as `0.x`)
  164. * `~1.2.3-beta.2` := `>=1.2.3-beta.2 <1.3.0` Note that prereleases in
  165. the `1.2.3` version will be allowed, if they are greater than or
  166. equal to `beta.2`. So, `1.2.3-beta.4` would be allowed, but
  167. `1.2.4-beta.2` would not, because it is a prerelease of a
  168. different `[major, minor, patch]` tuple.
  169. #### Caret Ranges `^1.2.3` `^0.2.5` `^0.0.4`
  170. Allows changes that do not modify the left-most non-zero digit in the
  171. `[major, minor, patch]` tuple. In other words, this allows patch and
  172. minor updates for versions `1.0.0` and above, patch updates for
  173. versions `0.X >=0.1.0`, and *no* updates for versions `0.0.X`.
  174. Many authors treat a `0.x` version as if the `x` were the major
  175. "breaking-change" indicator.
  176. Caret ranges are ideal when an author may make breaking changes
  177. between `0.2.4` and `0.3.0` releases, which is a common practice.
  178. However, it presumes that there will *not* be breaking changes between
  179. `0.2.4` and `0.2.5`. It allows for changes that are presumed to be
  180. additive (but non-breaking), according to commonly observed practices.
  181. * `^1.2.3` := `>=1.2.3 <2.0.0`
  182. * `^0.2.3` := `>=0.2.3 <0.3.0`
  183. * `^0.0.3` := `>=0.0.3 <0.0.4`
  184. * `^1.2.3-beta.2` := `>=1.2.3-beta.2 <2.0.0` Note that prereleases in
  185. the `1.2.3` version will be allowed, if they are greater than or
  186. equal to `beta.2`. So, `1.2.3-beta.4` would be allowed, but
  187. `1.2.4-beta.2` would not, because it is a prerelease of a
  188. different `[major, minor, patch]` tuple.
  189. * `^0.0.3-beta` := `>=0.0.3-beta <0.0.4` Note that prereleases in the
  190. `0.0.3` version *only* will be allowed, if they are greater than or
  191. equal to `beta`. So, `0.0.3-pr.2` would be allowed.
  192. When parsing caret ranges, a missing `patch` value desugars to the
  193. number `0`, but will allow flexibility within that value, even if the
  194. major and minor versions are both `0`.
  195. * `^1.2.x` := `>=1.2.0 <2.0.0`
  196. * `^0.0.x` := `>=0.0.0 <0.1.0`
  197. * `^0.0` := `>=0.0.0 <0.1.0`
  198. A missing `minor` and `patch` values will desugar to zero, but also
  199. allow flexibility within those values, even if the major version is
  200. zero.
  201. * `^1.x` := `>=1.0.0 <2.0.0`
  202. * `^0.x` := `>=0.0.0 <1.0.0`
  203. ### Range Grammar
  204. Putting all this together, here is a Backus-Naur grammar for ranges,
  205. for the benefit of parser authors:
  206. ```bnf
  207. range-set ::= range ( logical-or range ) *
  208. logical-or ::= ( ' ' ) * '||' ( ' ' ) *
  209. range ::= hyphen | simple ( ' ' simple ) * | ''
  210. hyphen ::= partial ' - ' partial
  211. simple ::= primitive | partial | tilde | caret
  212. primitive ::= ( '<' | '>' | '>=' | '<=' | '=' ) partial
  213. partial ::= xr ( '.' xr ( '.' xr qualifier ? )? )?
  214. xr ::= 'x' | 'X' | '*' | nr
  215. nr ::= '0' | ['1'-'9'] ( ['0'-'9'] ) *
  216. tilde ::= '~' partial
  217. caret ::= '^' partial
  218. qualifier ::= ( '-' pre )? ( '+' build )?
  219. pre ::= parts
  220. build ::= parts
  221. parts ::= part ( '.' part ) *
  222. part ::= nr | [-0-9A-Za-z]+
  223. ```
  224. ## Functions
  225. All methods and classes take a final `options` object argument. All
  226. options in this object are `false` by default. The options supported
  227. are:
  228. - `loose` Be more forgiving about not-quite-valid semver strings.
  229. (Any resulting output will always be 100% strict compliant, of
  230. course.) For backwards compatibility reasons, if the `options`
  231. argument is a boolean value instead of an object, it is interpreted
  232. to be the `loose` param.
  233. - `includePrerelease` Set to suppress the [default
  234. behavior](https://github.com/npm/node-semver#prerelease-tags) of
  235. excluding prerelease tagged versions from ranges unless they are
  236. explicitly opted into.
  237. Strict-mode Comparators and Ranges will be strict about the SemVer
  238. strings that they parse.
  239. * `valid(v)`: Return the parsed version, or null if it's not valid.
  240. * `inc(v, release)`: Return the version incremented by the release
  241. type (`major`, `premajor`, `minor`, `preminor`, `patch`,
  242. `prepatch`, or `prerelease`), or null if it's not valid
  243. * `premajor` in one call will bump the version up to the next major
  244. version and down to a prerelease of that major version.
  245. `preminor`, and `prepatch` work the same way.
  246. * If called from a non-prerelease version, the `prerelease` will work the
  247. same as `prepatch`. It increments the patch version, then makes a
  248. prerelease. If the input version is already a prerelease it simply
  249. increments it.
  250. * `prerelease(v)`: Returns an array of prerelease components, or null
  251. if none exist. Example: `prerelease('1.2.3-alpha.1') -> ['alpha', 1]`
  252. * `major(v)`: Return the major version number.
  253. * `minor(v)`: Return the minor version number.
  254. * `patch(v)`: Return the patch version number.
  255. * `intersects(r1, r2, loose)`: Return true if the two supplied ranges
  256. or comparators intersect.
  257. * `parse(v)`: Attempt to parse a string as a semantic version, returning either
  258. a `SemVer` object or `null`.
  259. ### Comparison
  260. * `gt(v1, v2)`: `v1 > v2`
  261. * `gte(v1, v2)`: `v1 >= v2`
  262. * `lt(v1, v2)`: `v1 < v2`
  263. * `lte(v1, v2)`: `v1 <= v2`
  264. * `eq(v1, v2)`: `v1 == v2` This is true if they're logically equivalent,
  265. even if they're not the exact same string. You already know how to
  266. compare strings.
  267. * `neq(v1, v2)`: `v1 != v2` The opposite of `eq`.
  268. * `cmp(v1, comparator, v2)`: Pass in a comparison string, and it'll call
  269. the corresponding function above. `"==="` and `"!=="` do simple
  270. string comparison, but are included for completeness. Throws if an
  271. invalid comparison string is provided.
  272. * `compare(v1, v2)`: Return `0` if `v1 == v2`, or `1` if `v1` is greater, or `-1` if
  273. `v2` is greater. Sorts in ascending order if passed to `Array.sort()`.
  274. * `rcompare(v1, v2)`: The reverse of compare. Sorts an array of versions
  275. in descending order when passed to `Array.sort()`.
  276. * `diff(v1, v2)`: Returns difference between two versions by the release type
  277. (`major`, `premajor`, `minor`, `preminor`, `patch`, `prepatch`, or `prerelease`),
  278. or null if the versions are the same.
  279. ### Comparators
  280. * `intersects(comparator)`: Return true if the comparators intersect
  281. ### Ranges
  282. * `validRange(range)`: Return the valid range or null if it's not valid
  283. * `satisfies(version, range)`: Return true if the version satisfies the
  284. range.
  285. * `maxSatisfying(versions, range)`: Return the highest version in the list
  286. that satisfies the range, or `null` if none of them do.
  287. * `minSatisfying(versions, range)`: Return the lowest version in the list
  288. that satisfies the range, or `null` if none of them do.
  289. * `minVersion(range)`: Return the lowest version that can possibly match
  290. the given range.
  291. * `gtr(version, range)`: Return `true` if version is greater than all the
  292. versions possible in the range.
  293. * `ltr(version, range)`: Return `true` if version is less than all the
  294. versions possible in the range.
  295. * `outside(version, range, hilo)`: Return true if the version is outside
  296. the bounds of the range in either the high or low direction. The
  297. `hilo` argument must be either the string `'>'` or `'<'`. (This is
  298. the function called by `gtr` and `ltr`.)
  299. * `intersects(range)`: Return true if any of the ranges comparators intersect
  300. Note that, since ranges may be non-contiguous, a version might not be
  301. greater than a range, less than a range, *or* satisfy a range! For
  302. example, the range `1.2 <1.2.9 || >2.0.0` would have a hole from `1.2.9`
  303. until `2.0.0`, so the version `1.2.10` would not be greater than the
  304. range (because `2.0.1` satisfies, which is higher), nor less than the
  305. range (since `1.2.8` satisfies, which is lower), and it also does not
  306. satisfy the range.
  307. If you want to know if a version satisfies or does not satisfy a
  308. range, use the `satisfies(version, range)` function.
  309. ### Coercion
  310. * `coerce(version)`: Coerces a string to semver if possible
  311. This aims to provide a very forgiving translation of a non-semver string to
  312. semver. It looks for the first digit in a string, and consumes all
  313. remaining characters which satisfy at least a partial semver (e.g., `1`,
  314. `1.2`, `1.2.3`) up to the max permitted length (256 characters). Longer
  315. versions are simply truncated (`4.6.3.9.2-alpha2` becomes `4.6.3`). All
  316. surrounding text is simply ignored (`v3.4 replaces v3.3.1` becomes
  317. `3.4.0`). Only text which lacks digits will fail coercion (`version one`
  318. is not valid). The maximum length for any semver component considered for
  319. coercion is 16 characters; longer components will be ignored
  320. (`10000000000000000.4.7.4` becomes `4.7.4`). The maximum value for any
  321. semver component is `Number.MAX_SAFE_INTEGER || (2**53 - 1)`; higher value
  322. components are invalid (`9999999999999999.4.7.4` is likely invalid).