get-root-dirs.js 1011 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // @ts-check
  2. const glob = require('glob')
  3. /**
  4. * Process a Next.js root directory glob.
  5. *
  6. * @param {string} rootDir - A Next.js root directory glob.
  7. * @returns {string[]} - An array of Root directories.
  8. */
  9. const processRootDir = (rootDir) => {
  10. // Ensures we only match folders.
  11. if (!rootDir.endsWith('/')) rootDir += '/'
  12. return glob.sync(rootDir)
  13. }
  14. /**
  15. * Gets one or more Root
  16. *
  17. * @param {import('eslint').Rule.RuleContext} context - ESLint rule context
  18. * @returns An array of root directories.
  19. */
  20. const getRootDirs = (context) => {
  21. let rootDirs = [context.getCwd()]
  22. /** @type {{rootDir?:string|string[]}|undefined} */
  23. const nextSettings = context.settings.next || {}
  24. let rootDir = nextSettings.rootDir
  25. if (typeof rootDir === 'string') {
  26. rootDirs = processRootDir(rootDir)
  27. } else if (Array.isArray(rootDir)) {
  28. rootDirs = rootDir
  29. .map((dir) => (typeof dir === 'string' ? processRootDir(dir) : []))
  30. .flat()
  31. }
  32. return rootDirs
  33. }
  34. module.exports = getRootDirs