1
0
mirror of https://github.com/sasjs/lint.git synced 2026-01-14 07:40:05 +00:00

feat: honour .gitignore and ignoreList from config when linting filesystem

This commit is contained in:
2022-08-12 15:49:28 +05:00
parent e1bcf5b06b
commit 40aea383b7
7 changed files with 6679 additions and 18 deletions

33
src/utils/isIgnored.ts Normal file
View File

@@ -0,0 +1,33 @@
import { fileExists, readFile } from '@sasjs/utils'
import path from 'path'
import ignore from 'ignore'
import { getLintConfig, getProjectRoot } from '.'
import { LintConfig } from '../types'
/**
*
* @param fPath absolute path of file or folder
* @returns {Promise<boolean>} true if path matches the patterns from .gitignore file otherwise false
*/
export const isIgnored = async (
fPath: string,
configuration?: LintConfig
): Promise<boolean> => {
const config = configuration || (await getLintConfig())
const projectRoot = await getProjectRoot()
const gitIgnoreFilePath = path.join(projectRoot, '.gitignore')
const rootPath = projectRoot + path.sep
const relativePath = fPath.replace(rootPath, '')
if (fPath === projectRoot) return false
let gitIgnoreFileContent = ''
if (await fileExists(gitIgnoreFilePath))
gitIgnoreFileContent = await readFile(gitIgnoreFilePath)
return ignore()
.add(gitIgnoreFileContent)
.add(config.ignoreList)
.ignores(relativePath)
}