get-linters.js 826 B

123456789101112131415161718192021222324252627282930313233
  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 path = require("path")
  7. const needle = `${path.sep}node_modules${path.sep}eslint${path.sep}`
  8. module.exports = () => {
  9. const eslintPaths = new Set(
  10. Object.keys(require.cache)
  11. .filter(id => id.includes(needle))
  12. .map(id => id.slice(0, id.indexOf(needle) + needle.length))
  13. )
  14. const linters = []
  15. for (const eslintPath of eslintPaths) {
  16. try {
  17. const linter = require(eslintPath).Linter
  18. if (linter) {
  19. linters.push(linter)
  20. }
  21. } catch (error) {
  22. if (error.code !== "MODULE_NOT_FOUND") {
  23. throw error
  24. }
  25. }
  26. }
  27. return linters
  28. }