2022-04-15 15:19:05 +00:00
|
|
|
import { existsSync, readFileSync } from 'node:fs'
|
2022-02-28 16:11:46 +00:00
|
|
|
import ignore from 'ignore'
|
2023-08-25 12:08:38 +00:00
|
|
|
import { join, relative, resolve } from 'pathe'
|
2022-04-04 09:41:48 +00:00
|
|
|
import { tryUseNuxt } from './context'
|
2022-02-28 16:11:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a filter function to filter an array of paths
|
|
|
|
*/
|
|
|
|
export function isIgnored (pathname: string): boolean {
|
2022-04-04 09:41:48 +00:00
|
|
|
const nuxt = tryUseNuxt()
|
|
|
|
|
|
|
|
// Happens with CLI reloads
|
|
|
|
if (!nuxt) {
|
2022-08-22 10:12:02 +00:00
|
|
|
return false
|
2022-04-04 09:41:48 +00:00
|
|
|
}
|
2022-02-28 16:11:46 +00:00
|
|
|
|
|
|
|
if (!nuxt._ignore) {
|
|
|
|
nuxt._ignore = ignore(nuxt.options.ignoreOptions)
|
2023-08-25 12:08:38 +00:00
|
|
|
nuxt._ignore.add(resolveIgnorePatterns())
|
2022-02-28 16:11:46 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 14:57:58 +00:00
|
|
|
const cwds = nuxt.options._layers?.map(layer => layer.cwd).sort((a, b) => b.length - a.length)
|
|
|
|
const layer = cwds?.find(cwd => pathname.startsWith(cwd))
|
|
|
|
const relativePath = relative(layer ?? nuxt.options.rootDir, pathname)
|
2022-02-28 16:11:46 +00:00
|
|
|
if (relativePath.startsWith('..')) {
|
|
|
|
return false
|
|
|
|
}
|
2022-08-22 10:12:02 +00:00
|
|
|
return !!(relativePath && nuxt._ignore.ignores(relativePath))
|
2022-02-28 16:11:46 +00:00
|
|
|
}
|
2023-01-20 16:23:16 +00:00
|
|
|
|
2023-08-25 12:08:38 +00:00
|
|
|
export function resolveIgnorePatterns (relativePath?: string): string[] {
|
|
|
|
const nuxt = tryUseNuxt()
|
|
|
|
|
|
|
|
// Happens with CLI reloads
|
|
|
|
if (!nuxt) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nuxt._ignorePatterns) {
|
|
|
|
nuxt._ignorePatterns = nuxt.options.ignore.flatMap(s => resolveGroupSyntax(s))
|
|
|
|
|
|
|
|
const nuxtignoreFile = join(nuxt.options.rootDir, '.nuxtignore')
|
|
|
|
if (existsSync(nuxtignoreFile)) {
|
|
|
|
const contents = readFileSync(nuxtignoreFile, 'utf-8')
|
|
|
|
nuxt._ignorePatterns.push(...contents.trim().split(/\r?\n/))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (relativePath) {
|
|
|
|
return nuxt._ignorePatterns.map(p => p.startsWith('*') || p.startsWith('!*') ? p : relative(relativePath, resolve(nuxt.options.rootDir, p)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nuxt._ignorePatterns
|
|
|
|
}
|
|
|
|
|
2023-01-20 16:23:16 +00:00
|
|
|
/**
|
|
|
|
* This function turns string containing groups '**\/*.{spec,test}.{js,ts}' into an array of strings.
|
|
|
|
* For example will '**\/*.{spec,test}.{js,ts}' be resolved to:
|
|
|
|
* ['**\/*.spec.js', '**\/*.spec.ts', '**\/*.test.js', '**\/*.test.ts']
|
|
|
|
* @param group string containing the group syntax
|
|
|
|
* @returns {string[]} array of strings without the group syntax
|
|
|
|
*/
|
|
|
|
export function resolveGroupSyntax (group: string): string[] {
|
|
|
|
let groups = [group]
|
|
|
|
while (groups.some(group => group.includes('{'))) {
|
|
|
|
groups = groups.flatMap((group) => {
|
|
|
|
const [head, ...tail] = group.split('{')
|
|
|
|
if (tail.length) {
|
|
|
|
const [body, ...rest] = tail.join('{').split('}')
|
|
|
|
return body.split(',').map(part => `${head}${part}${rest.join('')}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return group
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return groups
|
|
|
|
}
|