Skip to content

Commit

Permalink
feat/issue-1398: updated the condition to return the errors early
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohit Kumar committed Mar 28, 2024
1 parent da2ede0 commit 64c1382
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
23 changes: 13 additions & 10 deletions lib/makeCmdTasks.js
Expand Up @@ -32,7 +32,17 @@ 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 (isFn && command && typeof command === 'object') {
// 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' && typeof command !== 'object') || !command) {
throw new Error(
configurationError(
'[Function]',
'Function task should return a string or an array of strings or an object',
resolved
)
)
} else if (isFn && typeof command === 'object') {
if (
typeof command.title === 'string' &&
typeof command.command === 'string' &&
Expand All @@ -51,22 +61,15 @@ export const makeCmdTasks = async ({ commands, cwd, files, gitDir, shell, verbos
task,
})
} else {
throw new Error(
configurationError('[Function]', 'Function task should be in proper format', resolved)
)
}
} 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',
'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 })
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/makeCmdTasks.spec.js
Expand Up @@ -112,14 +112,14 @@ describe('makeCmdTasks', () => {
expect(res[0].title).toEqual('test')
})

it("should throw when function task doesn't return string | string[]", async () => {
it("should throw when function task doesn't return string | string[] | object", async () => {
await expect(makeCmdTasks({ commands: () => null, gitDir, files: ['test.js'] })).rejects
.toThrowErrorMatchingInlineSnapshot(`
"✖ Validation Error:
Invalid value for '[Function]': null
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"
`)
})

Expand Down

0 comments on commit 64c1382

Please sign in to comment.