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