Skip to content
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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
51 changes: 38 additions & 13 deletions lib/makeCmdTasks.js
Copy link
Member

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -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')

Expand Down Expand Up @@ -31,20 +32,44 @@ 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
// Do the validation here instead of `validateConfig` to skip evaluating the function multiple times
if (isFn && typeof command !== 'string') {
throw new Error(
configurationError(
'[Function]',
'Function task should return a string or an array of strings',
resolved
if (isFn && command && typeof command === 'object') {
if (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we flip the condition and do an early return?

Copy link
Author

Choose a reason for hiding this comment

The 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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need both the command and title?

Copy link
Author

Choose a reason for hiding this comment

The 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 be in proper format', resolved)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we provide a meaningful error message on what specifically was the problem?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

)
)
}
} else {
// If the function linter didn't return string | string[] it won't work
// Do the validation here instead of `validateConfig` to skip evaluating the function multiple times
if (isFn && typeof command !== 'string') {
throw new Error(
configurationError(
'[Function]',
'Function task should return a string or an array of strings',
resolved
)
)
}
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 })
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/resolveTaskFn.js
Expand Up @@ -108,7 +108,7 @@ const interruptExecutionOnError = (ctx, execaChildProcess) => {
* @param {Object} ctx
* @returns {Error}
*/
const makeErr = (command, result, ctx) => {
export const makeErr = (command, result, ctx) => {
ctx.errors.add(TaskError)

// https://nodejs.org/api/events.html#error-events
Expand Down