Skip to content

Commit

Permalink
fix: Run commands in the context of gitDir if it is set (#110)
Browse files Browse the repository at this point in the history
In case of running lint-stage from a sub-directory,
execa should be also run with `cwd` option set correctly to the git dir.

Closes #109
  • Loading branch information
okonet authored Dec 14, 2016
1 parent cd35c01 commit a74135d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ cosmiconfig('lint-staged', {
const tasks = generateTasks(config, resolvePaths(files, gitDir))
.map(task => ({
title: `Running tasks for ${ task.pattern }`,
task: () => (new Listr(runScript(task.commands, task.fileList, packageJson)))
task: () => (
new Listr(
runScript(task.commands, task.fileList, packageJson, gitDir)
)
)
}))


Expand Down
5 changes: 3 additions & 2 deletions src/runScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
const findBin = require('./findBin')
const execa = require('execa')

module.exports = function runScript(commands, pathsToLint, packageJson) {
module.exports = function runScript(commands, pathsToLint, packageJson, gitDir) {
const lintersArray = Array.isArray(commands) ? commands : [commands]
const execaOptions = gitDir ? { cwd: gitDir } : {}
return lintersArray.map(linter => ({
title: linter,
task: () => {
try {
const res = findBin(linter, pathsToLint, packageJson)
return new Promise((resolve, reject) => {
execa(res.bin, res.args)
execa(res.bin, res.args, execaOptions)
.then(() => {
resolve(`${ linter } passed!`)
})
Expand Down
21 changes: 19 additions & 2 deletions test/runScript.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,30 @@ describe('runScript', () => {
expect(res[0].task()).toBeAPromise()
expect(spy.calls.length).toEqual(1)
expect(spy.calls[0].arguments).toEqual(
['npm', ['run', '--silent', 'test', '--', 'test.js']]
['npm', ['run', '--silent', 'test', '--', 'test.js'], {}]
)

expect(res[1].task()).toBeAPromise()
expect(spy.calls.length).toEqual(2)
expect(spy.calls[1].arguments).toEqual(
['npm', ['run', '--silent', 'test2', '--', 'test.js']]
['npm', ['run', '--silent', 'test2', '--', 'test.js'], {}]
)
})

it('should pass cwd option to execa if gitDir option is set', () => {
const spy = expect.createSpy()
runScript.__set__('execa', spy)
const res = runScript(['test', 'test2'], 'test.js', packageJSON, '../')
expect(res[0].task()).toBeAPromise()
expect(spy.calls.length).toEqual(1)
expect(spy.calls[0].arguments).toEqual(
['npm', ['run', '--silent', 'test', '--', 'test.js'], { cwd: '../' }]
)

expect(res[1].task()).toBeAPromise()
expect(spy.calls.length).toEqual(2)
expect(spy.calls[1].arguments).toEqual(
['npm', ['run', '--silent', 'test2', '--', 'test.js'], { cwd: '../' }]
)
})
})
Expand Down

0 comments on commit a74135d

Please sign in to comment.