get-configured-node-version.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * @author Toru Nagashima <https://github.com/mysticatea>
  3. * See LICENSE file in root directory for full license.
  4. */
  5. "use strict"
  6. const { Range } = require("semver") //eslint-disable-line no-unused-vars
  7. const getPackageJson = require("./get-package-json")
  8. const getSemverRange = require("./get-semver-range")
  9. /**
  10. * Get the `engines.node` field of package.json.
  11. * @param {string} filename The path to the current linting file.
  12. * @returns {Range|null} The range object of the `engines.node` field.
  13. */
  14. function getEnginesNode(filename) {
  15. const info = getPackageJson(filename)
  16. return getSemverRange(info && info.engines && info.engines.node)
  17. }
  18. /**
  19. * Gets version configuration.
  20. *
  21. * 1. Parse a given version then return it if it's valid.
  22. * 2. Look package.json up and parse `engines.node` then return it if it's valid.
  23. * 3. Return `>=8.0.0`.
  24. *
  25. * @param {string|undefined} version The version range text.
  26. * @param {string} filename The path to the current linting file.
  27. * This will be used to look package.json up if `version` is not a valid version range.
  28. * @returns {Range} The configured version range.
  29. */
  30. module.exports = function getConfiguredNodeVersion(version, filename) {
  31. return (
  32. getSemverRange(version) ||
  33. getEnginesNode(filename) ||
  34. getSemverRange(">=8.0.0")
  35. )
  36. }