Skip to content

Commit

Permalink
Get the file patterns to lint/ignore 🗄️
Browse files Browse the repository at this point in the history
  • Loading branch information
01taylop committed Oct 27, 2024
1 parent 2a8bcc4 commit 2dc6607
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 42 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lint-pilot",
"description": "Lint Pilot: Your co-pilot for maintaining high code quality with seamless ESLint, Stylelint, and MarkdownLint integration.",
"description": "Lint Pilot: Your co-pilot for maintaining high code quality with seamless ESLint, Stylelint, and Markdownlint integration.",
"repository": {
"type": "git",
"url": "git+https://github.com/01taylop/lint-pilot.git"
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Options:
-h, --help display help for command
Commands:
lint [options] run all linters: ESLint, Stylelint, and MarkdownLint (default)
lint [options] run all linters: ESLint, Stylelint, and Markdownlint (default)
help [command] display help for command
`)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import colourLog from '@Utils/colourLog'
import { Linter } from '@Types'
import { Linter } from '@Types/lint'
import colourLog from '@Utils/colour-log'

import getFilePatterns from '../filePatterns'
import { getFilePatterns } from '../filePatterns'

describe('filePatterns', () => {
describe('getFilePatterns', () => {

jest.spyOn(colourLog, 'config').mockImplementation(() => {})
jest.spyOn(console, 'log').mockImplementation(() => {})
beforeEach(() => {
jest.spyOn(colourLog, 'config').mockImplementation(() => true)
jest.spyOn(console, 'log').mockImplementation(() => true)
})

it('returns the correct file patterns for eslint', () => {
it('returns the default file patterns for ESLint', () => {
const filePatterns = getFilePatterns({})

const expectedPatterns = [
Expand All @@ -19,7 +21,7 @@ describe('filePatterns', () => {
expect(colourLog.config).toHaveBeenCalledWith('ESLint Patterns', expectedPatterns)
})

it('returns the correct file patterns for eslint with an additional include pattern', () => {
it('returns the file patterns for ESLint with an additional include pattern', () => {
const filePatterns = getFilePatterns({
eslintInclude: 'foo'
})
Expand All @@ -33,7 +35,7 @@ describe('filePatterns', () => {
expect(colourLog.config).toHaveBeenCalledWith('ESLint Patterns', expectedPatterns)
})

it('returns the correct file patterns for eslint with several additional include patterns', () => {
it('returns the file patterns for ESLint with several additional include patterns', () => {
const filePatterns = getFilePatterns({
eslintInclude: ['foo', 'bar'],
})
Expand All @@ -48,7 +50,7 @@ describe('filePatterns', () => {
expect(colourLog.config).toHaveBeenCalledWith('ESLint Patterns', expectedPatterns)
})

it('returns the correct file patterns for markdownlint', () => {
it('returns the default file patterns for Markdownlint', () => {
const filePatterns = getFilePatterns({})

const expectedPatterns = [
Expand All @@ -59,7 +61,7 @@ describe('filePatterns', () => {
expect(colourLog.config).toHaveBeenCalledWith('Markdownlint Patterns', expectedPatterns)
})

it('returns the correct file patterns for stylelint', () => {
it('returns the default file patterns for Stylelint', () => {
const filePatterns = getFilePatterns({})

const expectedPatterns = [
Expand All @@ -70,7 +72,7 @@ describe('filePatterns', () => {
expect(colourLog.config).toHaveBeenCalledWith('Stylelint Patterns', expectedPatterns)
})

it('returns the correct ignore file patterns', () => {
it('returns the default ignore file patterns', () => {
const filePatterns = getFilePatterns({})

const expectedPatterns = [
Expand All @@ -82,7 +84,7 @@ describe('filePatterns', () => {
expect(colourLog.config).toHaveBeenCalledWith('Ignore', expectedPatterns)
})

it('returns the correct ignore file patterns with an additional directory ignored', () => {
it('returns the ignore file patterns with an additional directory ignored', () => {
const filePatterns = getFilePatterns({
ignoreDirs: 'foo'
})
Expand All @@ -96,7 +98,7 @@ describe('filePatterns', () => {
expect(colourLog.config).toHaveBeenCalledWith('Ignore', expectedPatterns)
})

it('returns the correct ignore file patterns with several additional directories ignored', () => {
it('returns the ignore file patterns with several additional directories ignored', () => {
const filePatterns = getFilePatterns({
ignoreDirs: ['foo', 'bar'],
})
Expand All @@ -110,7 +112,7 @@ describe('filePatterns', () => {
expect(colourLog.config).toHaveBeenCalledWith('Ignore', expectedPatterns)
})

it('returns the correct ignore file patterns with an additional pattern ignored', () => {
it('returns the ignore file patterns with an additional pattern ignored', () => {
const filePatterns = getFilePatterns({
ignorePatterns: 'foo'
})
Expand All @@ -125,7 +127,7 @@ describe('filePatterns', () => {
expect(colourLog.config).toHaveBeenCalledWith('Ignore', expectedPatterns)
})

it('returns the correct ignore file patterns with several additional patterns ignored', () => {
it('returns the ignore file patterns with several additional patterns ignored', () => {
const filePatterns = getFilePatterns({
ignorePatterns: ['foo', 'bar'],
})
Expand Down
27 changes: 11 additions & 16 deletions src/filePatterns.ts → src/commands/lint/filePatterns.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { type FilePatterns, Linter } from '@Types'
import colourLog from '@Utils/colourLog'
import type { LintOptions } from '@Types/commands'
import { type FilePatterns, Linter } from '@Types/lint'
import colourLog from '@Utils/colour-log'

type StringOrArray = string | Array<string>
type GetFilePatterns = Pick<LintOptions, 'eslintInclude' | 'ignoreDirs' | 'ignorePatterns'>

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 getFilePatterns = ({ eslintInclude = [], ignoreDirs = [], ignorePatterns = [] }: GetFilePatterns): FilePatterns => {
const eslintIncludePatterns = [
'**/*.{cjs,js,jsx,mjs,ts,tsx}',
...enforceArray(eslintInclude),
...Array.of(eslintInclude).flat(),
]

const ignoreDirectories = [
Expand All @@ -24,12 +17,12 @@ const getFilePatterns = ({ eslintInclude, ignoreDirs, ignorePatterns }: GetFileP
'tmp',
'tscOutput',
'vendor',
...enforceArray(ignoreDirs),
...Array.of(ignoreDirs).flat(),
].sort()

const ignoreGlobs = [
'**/*.+(map|min).*',
...enforceArray(ignorePatterns),
...Array.of(ignorePatterns).flat(),
].sort()

const filePatterns = {
Expand All @@ -53,4 +46,6 @@ const getFilePatterns = ({ eslintInclude, ignoreDirs, ignorePatterns }: GetFileP
return filePatterns
}

export default getFilePatterns
export {
getFilePatterns,
}
12 changes: 10 additions & 2 deletions src/commands/lint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { clearCacheDirectory } from '@Utils/cache'
import colourLog from '@Utils/colour-log'
import { clearTerminal } from '@Utils/terminal'

const lint = ({ clearCache, debug, emoji, title, ...options }: LintOptions) => {
import { getFilePatterns } from './filePatterns'

const lint = ({ clearCache, debug, emoji, eslintInclude, ignoreDirs, ignorePatterns, title, ...options }: LintOptions) => {
global.debug = debug

clearTerminal()
Expand All @@ -13,7 +15,13 @@ const lint = ({ clearCache, debug, emoji, title, ...options }: LintOptions) => {
clearCacheDirectory()
}

console.log('Run Lint', options)
const filePatterns = getFilePatterns({
eslintInclude,
ignoreDirs,
ignorePatterns,
})

console.log('Run Lint', options, filePatterns)
}

export default lint
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const createProgram = () => {

program
.command('lint', { isDefault: true })
.description('run all linters: ESLint, Stylelint, and MarkdownLint (default)')
.description('run all linters: ESLint, Stylelint, and Markdownlint (default)')

.option('-e, --emoji <string>', 'customise the emoji displayed when running lint-pilot', '✈️')
.option('-t, --title <string>', 'customise the title displayed when running lint-pilot', 'Lint Pilot')
Expand All @@ -37,12 +37,12 @@ const createProgram = () => {
.option('--cache', 'cache linting results', false)
.option('--clearCache', 'clear the cache', false)

.option('--ignore-dirs <directories...>', 'directories to ignore globally')
.option('--ignore-patterns <patterns...>', 'file patterns to ignore globally')
.option('--eslint-include <patterns...>', 'file patterns to include for ESLint')
.option('--ignore-dirs <directories...>', 'define directories to ignore')
.option('--ignore-patterns <patterns...>', 'define file patterns to ignore')
.option('--eslint-include <patterns...>', 'define additional file patterns for ESLint')

.option('--debug', 'output additional debug information including the list of files being linted', false)
.option('--eslint-use-legacy-config', 'set to true to use the legacy ESLint configuration', false)
.option('--debug', 'output additional debug information', false)
.option('--eslint-use-legacy-config', 'use legacy ESLint config', false)

.action(options => lint(options))

Expand Down
20 changes: 20 additions & 0 deletions src/types/lint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
enum Linter {
ESLint = 'ESLint',
Markdownlint = 'Markdownlint',
Stylelint = 'Stylelint',
}

interface FilePatterns {
includePatterns: {
[key in Linter]: Array<string>
}
ignorePatterns: Array<string>
}

export type {
FilePatterns,
}

export {
Linter,
}
22 changes: 22 additions & 0 deletions src/utils/__tests__/colour-log.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,34 @@ import colourLog from '../colour-log'
jest.mock('chalk', () => ({
blue: jest.fn().mockImplementation(text => text),
cyan: jest.fn().mockImplementation(text => text),
dim: jest.fn().mockImplementation(text => text),
magenta: jest.fn().mockImplementation(text => text),
}))

describe('colourLog', () => {

const mockedConsoleLog = jest.spyOn(console, 'log').mockImplementation(() => {})

describe('config', () => {

it('logs the key in magenta and a single config item in dim', () => {
colourLog.config('setting', ['foo'])

expect(chalk.magenta).toHaveBeenCalledOnceWith('setting: ')
expect(chalk.dim).toHaveBeenCalledOnceWith('foo')
expect(mockedConsoleLog).toHaveBeenCalledOnceWith('setting: ', 'foo')
})

it('logs the key in magenta and a config array in dim', () => {
colourLog.config('setting', ['foo', 'bar', 'baz'])

expect(chalk.magenta).toHaveBeenCalledOnceWith('setting: ')
expect(chalk.dim).toHaveBeenCalledOnceWith('[foo, bar, baz]')
expect(mockedConsoleLog).toHaveBeenCalledOnceWith('setting: ', '[foo, bar, baz]')
})

})

describe('info', () => {

it('logs the text in blue', () => {
Expand Down
9 changes: 9 additions & 0 deletions src/utils/colour-log.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import chalk from 'chalk'

const colourLog = {
config: (key: string, configArray: Array<string>) => {
const configString = configArray.length > 1
? `[${configArray.join(', ')}]`
: configArray[0]

console.log(chalk.magenta(`${key}: `), chalk.dim(configString))
},

info: (text: string) => console.log(chalk.blue(text)),

title: (title: string) => console.log(chalk.cyan(title)),
}

Expand Down

0 comments on commit 2dc6607

Please sign in to comment.