-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configured include and ignore directories and patterns 🚦
- Loading branch information
Showing
5 changed files
with
239 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import colourLog from '@Utils/colourLog' | ||
import { Linter } from '@Types' | ||
|
||
import getFilePatterns from '../filePatterns' | ||
|
||
describe('filePatterns', () => { | ||
|
||
jest.spyOn(colourLog, 'config').mockImplementation(() => {}) | ||
jest.spyOn(console, 'log').mockImplementation(() => {}) | ||
|
||
it('returns the correct file patterns for eslint', () => { | ||
const filePatterns = getFilePatterns({}) | ||
|
||
const expectedPatterns = [ | ||
'**/*.{cjs,js,jsx,mjs,ts,tsx}', | ||
] | ||
|
||
expect(filePatterns.includePatterns[Linter.ESLint]).toEqual(expectedPatterns) | ||
expect(colourLog.config).toHaveBeenCalledWith('ESLint Patterns', expectedPatterns) | ||
}) | ||
|
||
it('returns the correct file patterns for eslint with an additional include pattern', () => { | ||
const filePatterns = getFilePatterns({ | ||
eslintInclude: 'foo' | ||
}) | ||
|
||
const expectedPatterns = [ | ||
'**/*.{cjs,js,jsx,mjs,ts,tsx}', | ||
'foo', | ||
] | ||
|
||
expect(filePatterns.includePatterns[Linter.ESLint]).toEqual(expectedPatterns) | ||
expect(colourLog.config).toHaveBeenCalledWith('ESLint Patterns', expectedPatterns) | ||
}) | ||
|
||
it('returns the correct file patterns for eslint with several additional include patterns', () => { | ||
const filePatterns = getFilePatterns({ | ||
eslintInclude: ['foo', 'bar'], | ||
}) | ||
|
||
const expectedPatterns = [ | ||
'**/*.{cjs,js,jsx,mjs,ts,tsx}', | ||
'foo', | ||
'bar', | ||
] | ||
|
||
expect(filePatterns.includePatterns[Linter.ESLint]).toEqual(expectedPatterns) | ||
expect(colourLog.config).toHaveBeenCalledWith('ESLint Patterns', expectedPatterns) | ||
}) | ||
|
||
it('returns the correct file patterns for markdownlint', () => { | ||
const filePatterns = getFilePatterns({}) | ||
|
||
const expectedPatterns = [ | ||
'**/*.{md,mdx}', | ||
] | ||
|
||
expect(filePatterns.includePatterns[Linter.Markdownlint]).toEqual(expectedPatterns) | ||
expect(colourLog.config).toHaveBeenCalledWith('Markdownlint Patterns', expectedPatterns) | ||
}) | ||
|
||
it('returns the correct file patterns for stylelint', () => { | ||
const filePatterns = getFilePatterns({}) | ||
|
||
const expectedPatterns = [ | ||
'**/*.{css,scss,less,sass,styl,stylus}', | ||
] | ||
|
||
expect(filePatterns.includePatterns[Linter.Stylelint]).toEqual(expectedPatterns) | ||
expect(colourLog.config).toHaveBeenCalledWith('Stylelint Patterns', expectedPatterns) | ||
}) | ||
|
||
it('returns the correct ignore file patterns', () => { | ||
const filePatterns = getFilePatterns({}) | ||
|
||
const expectedPatterns = [ | ||
'**/+(coverage|dist|node_modules|tmp|tscOutput|vendor)/**', | ||
'**/*.+(map|min).*', | ||
] | ||
|
||
expect(filePatterns.ignorePatterns).toEqual(expectedPatterns) | ||
expect(colourLog.config).toHaveBeenCalledWith('Ignore', expectedPatterns) | ||
}) | ||
|
||
it('returns the correct ignore file patterns with an additional directory ignored', () => { | ||
const filePatterns = getFilePatterns({ | ||
ignoreDirs: 'foo' | ||
}) | ||
|
||
const expectedPatterns = [ | ||
'**/+(coverage|dist|foo|node_modules|tmp|tscOutput|vendor)/**', | ||
'**/*.+(map|min).*', | ||
] | ||
|
||
expect(filePatterns.ignorePatterns).toEqual(expectedPatterns) | ||
expect(colourLog.config).toHaveBeenCalledWith('Ignore', expectedPatterns) | ||
}) | ||
|
||
it('returns the correct ignore file patterns with several additional directories ignored', () => { | ||
const filePatterns = getFilePatterns({ | ||
ignoreDirs: ['foo', 'bar'], | ||
}) | ||
|
||
const expectedPatterns = [ | ||
'**/+(bar|coverage|dist|foo|node_modules|tmp|tscOutput|vendor)/**', | ||
'**/*.+(map|min).*', | ||
] | ||
|
||
expect(filePatterns.ignorePatterns).toEqual(expectedPatterns) | ||
expect(colourLog.config).toHaveBeenCalledWith('Ignore', expectedPatterns) | ||
}) | ||
|
||
it('returns the correct ignore file patterns with an additional pattern ignored', () => { | ||
const filePatterns = getFilePatterns({ | ||
ignorePatterns: 'foo' | ||
}) | ||
|
||
const expectedPatterns = [ | ||
'**/+(coverage|dist|node_modules|tmp|tscOutput|vendor)/**', | ||
'**/*.+(map|min).*', | ||
'foo', | ||
] | ||
|
||
expect(filePatterns.ignorePatterns).toEqual(expectedPatterns) | ||
expect(colourLog.config).toHaveBeenCalledWith('Ignore', expectedPatterns) | ||
}) | ||
|
||
it('returns the correct ignore file patterns with several additional patterns ignored', () => { | ||
const filePatterns = getFilePatterns({ | ||
ignorePatterns: ['foo', 'bar'], | ||
}) | ||
|
||
const expectedPatterns = [ | ||
'**/+(coverage|dist|node_modules|tmp|tscOutput|vendor)/**', | ||
'**/*.+(map|min).*', | ||
'bar', | ||
'foo', | ||
] | ||
|
||
expect(filePatterns.ignorePatterns).toEqual(expectedPatterns) | ||
expect(colourLog.config).toHaveBeenCalledWith('Ignore', expectedPatterns) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { type FilePatterns, Linter } from '@Types' | ||
import colourLog from '@Utils/colourLog' | ||
|
||
type StringOrArray = string | Array<string> | ||
|
||
interface GetFilePatterns { | ||
eslintInclude?: StringOrArray | ||
ignoreDirs?: StringOrArray | ||
ignorePatterns?: StringOrArray | ||
} | ||
|
||
const enforceArray = (value: StringOrArray = []): Array<string> => Array.of(value).flat() | ||
|
||
const getFilePatterns = ({ eslintInclude, ignoreDirs, ignorePatterns }: GetFilePatterns): FilePatterns => { | ||
const eslintIncludePatterns = [ | ||
'**/*.{cjs,js,jsx,mjs,ts,tsx}', | ||
...enforceArray(eslintInclude), | ||
] | ||
|
||
const ignoreDirectories = [ | ||
'coverage', | ||
'dist', | ||
'node_modules', | ||
'tmp', | ||
'tscOutput', | ||
'vendor', | ||
...enforceArray(ignoreDirs), | ||
].sort() | ||
|
||
const ignoreGlobs = [ | ||
'**/*.+(map|min).*', | ||
...enforceArray(ignorePatterns), | ||
].sort() | ||
|
||
const filePatterns = { | ||
includePatterns: { | ||
[Linter.ESLint]: eslintIncludePatterns, | ||
[Linter.Markdownlint]: ['**/*.{md,mdx}'], | ||
[Linter.Stylelint]: ['**/*.{css,scss,less,sass,styl,stylus}'], | ||
}, | ||
ignorePatterns: [ | ||
`**/+(${ignoreDirectories.join('|')})/**`, | ||
...ignoreGlobs, | ||
], | ||
} | ||
|
||
colourLog.config('ESLint Patterns', filePatterns.includePatterns[Linter.ESLint]) | ||
colourLog.config('Markdownlint Patterns', filePatterns.includePatterns[Linter.Markdownlint]) | ||
colourLog.config('Stylelint Patterns', filePatterns.includePatterns[Linter.Stylelint]) | ||
colourLog.config('Ignore', filePatterns.ignorePatterns) | ||
console.log() | ||
|
||
return filePatterns | ||
} | ||
|
||
export default getFilePatterns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters