New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat/issue-1398: added support for custom configuration #1401
base: master
Are you sure you want to change the base?
Changes from 4 commits
c55b291
2626c43
da2ede0
64c1382
e769fd3
0682aff
eb8dd33
f27d888
6d82f42
20c3f86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import debug from 'debug' | ||
|
||
import { configurationError } from './messages.js' | ||
import { resolveTaskFn } from './resolveTaskFn.js' | ||
import { makeErr, resolveTaskFn } from './resolveTaskFn.js' | ||
import { getInitialState } from './state.js' | ||
|
||
const debugLog = debug('lint-staged:makeCmdTasks') | ||
|
||
|
@@ -31,20 +32,47 @@ export const makeCmdTasks = async ({ commands, cwd, files, gitDir, shell, verbos | |
const resolvedArray = Array.isArray(resolved) ? resolved : [resolved] // Wrap non-array command as array | ||
|
||
for (const command of resolvedArray) { | ||
// If the function linter didn't return string | string[] it won't work | ||
// If the function linter didn't return string | string[] | object it won't work | ||
// Do the validation here instead of `validateConfig` to skip evaluating the function multiple times | ||
if (isFn && typeof command !== 'string') { | ||
if ((isFn && typeof command !== 'string' && typeof command !== 'object') || !command) { | ||
throw new Error( | ||
configurationError( | ||
'[Function]', | ||
'Function task should return a string or an array of strings', | ||
'Function task should return a string or an array of strings or an object', | ||
resolved | ||
) | ||
) | ||
} else if (isFn && typeof command === 'object') { | ||
if ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we flip the condition and do an early return? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
typeof command.title === 'string' && | ||
typeof command.command === 'string' && | ||
typeof command.task === 'function' | ||
) { | ||
const task = async (ctx = getInitialState()) => { | ||
try { | ||
await command.task() | ||
} catch (e) { | ||
throw makeErr(command.title, e, ctx) | ||
} | ||
} | ||
cmdTasks.push({ | ||
title: command.title, | ||
command: command.command, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need both the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we only need title, so removed the command field. |
||
task, | ||
}) | ||
} else { | ||
throw new Error( | ||
configurationError( | ||
'[Function]', | ||
'Function task should return object with title, command and task where title or command should be string and task should be function', | ||
resolved | ||
) | ||
) | ||
} | ||
} else { | ||
const task = resolveTaskFn({ command, cwd, files, gitDir, isFn, shell, verbose }) | ||
cmdTasks.push({ title: command, command, task }) | ||
} | ||
|
||
const task = resolveTaskFn({ command, cwd, files, gitDir, isFn, shell, verbose }) | ||
cmdTasks.push({ title: command, command, task }) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file has started to become a bit complex... I wonder if we could split it somehow, maybe different handling for function and regular config.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done