-
-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: pass unparsed commands to execa with --shell
- Loading branch information
Showing
3 changed files
with
40 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 |
---|---|---|
|
@@ -17,9 +17,10 @@ const debug = require('debug')('lint-staged:task') | |
*/ | ||
const execLinter = (cmd, args, execaOptions = {}) => { | ||
debug('cmd:', cmd) | ||
debug('args:', args) | ||
if (args) debug('args:', args) | ||
debug('execaOptions:', execaOptions) | ||
return execa(cmd, args, execaOptions) | ||
|
||
return args ? execa(cmd, args, execaOptions) : execa(cmd, execaOptions) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
iiroj
Author
Member
|
||
} | ||
|
||
const successMsg = linter => `${symbols.success} ${linter} passed!` | ||
|
@@ -88,20 +89,31 @@ module.exports = function resolveTaskFn({ gitDir, linter, pathsToLint, shell = f | |
// Support arrays of strings/functions by treating everything as arrays | ||
const linters = Array.isArray(linterString) ? linterString : [linterString] | ||
|
||
const execaOptions = { preferLocal: true, reject: false, shell } | ||
|
||
const tasks = linters.map(command => { | ||
const [cmd, ...args] = stringArgv.parseArgsStringToArgv(command) | ||
// If `linter` is a function, args already include `pathsToLint`. | ||
const argsWithPaths = fnLinter ? args : args.concat(pathsToLint) | ||
let cmd | ||
let args | ||
|
||
if (shell) { | ||
execaOptions.shell = true | ||
// If `shell`, passed command shouldn't be parsed | ||
// If `linter` is a function, command already includes `pathsToLint`. | ||
cmd = fnLinter ? command : `${command} ${pathsToLint.join(' ')}` | ||
} else { | ||
const [parsedCmd, ...parsedArgs] = stringArgv.parseArgsStringToArgv(command) | ||
cmd = parsedCmd | ||
args = fnLinter ? parsedArgs : parsedArgs.concat(pathsToLint) | ||
} | ||
|
||
// Only use gitDir as CWD if we are using the git binary | ||
// e.g `npm` should run tasks in the actual CWD | ||
const execaOptions = { preferLocal: true, reject: false, shell } | ||
if (/^git(\.exe)?/i.test(command) && gitDir !== process.cwd()) { | ||
execaOptions.cwd = gitDir | ||
} | ||
|
||
return ctx => | ||
execLinter(cmd, argsWithPaths, execaOptions).then(result => { | ||
execLinter(cmd, args, execaOptions).then(result => { | ||
if (result.failed || result.killed || result.signal != null) { | ||
throw makeErr(linter, result, ctx) | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import resolveTaskFn from '../src/resolveTaskFn' | ||
|
||
jest.unmock('execa') | ||
|
||
describe('resolveTaskFn', () => { | ||
it('should call execa with shell when configured so', async () => { | ||
const taskFn = resolveTaskFn({ | ||
pathsToLint: ['package.json'], | ||
linter: () => 'node -e "process.exit(1)" || echo $?', | ||
shell: true | ||
}) | ||
|
||
await expect(taskFn()).resolves.toMatchInlineSnapshot(` | ||
Array [ | ||
"√ function linter() { | ||
return 'node -e \\"process.exit(1)\\" || echo $?'; | ||
} passed!", | ||
] | ||
`) | ||
}) | ||
}) |
I don't get it. Can execa accept cmd as function? Because cmd can be javascript function here (see current version of this file)