1
0
mirror of https://github.com/sasjs/lint.git synced 2025-12-10 17:34:36 +00:00
Files
lint/src/utils/isIgnored.ts
2022-08-15 21:57:18 +05:00

35 lines
1.1 KiB
TypeScript

import { fileExists, readFile } from '@sasjs/utils'
import path from 'path'
import ignore from 'ignore'
import { getLintConfig, getProjectRoot } from '.'
import { LintConfig } from '../types'
/**
* A function to check if file/folder path matches any pattern from .gitignore or ignoreList (.sasjsLint)
*
* @param {string} 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)
}