Skip to content

Commit

Permalink
fix(gitDir): Run npm run scripts in the current working directory i…
Browse files Browse the repository at this point in the history
…nstead of git directory.

When running `npm run ...` scripts it should run in a (sub-) directory where
package.json is located. If `.git` directory and `package.json` dirs doesn't match
it will fail. This commit fixes it.

Closes #122
  • Loading branch information
okonet committed Jan 30, 2017
1 parent 9a602c4 commit 9c45d8e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/runScript.js
Expand Up @@ -8,9 +8,10 @@ module.exports = function runScript(commands, pathsToLint, packageJson, options)
return lintersArray.map(linter => ({
title: linter,
task: () => {
const execaOptions = options && options.gitDir ? { cwd: options.gitDir } : {}
try {
const res = findBin(linter, pathsToLint, packageJson, options)
const execaOptions =
res.bin !== 'npm' && options && options.gitDir ? { cwd: options.gitDir } : {}
return new Promise((resolve, reject) => {
execa(res.bin, res.args, execaOptions)
.then(() => {
Expand Down
41 changes: 30 additions & 11 deletions test/runScript.spec.js
Expand Up @@ -34,18 +34,18 @@ describe('runScript', () => {
expect(runScript('test', 'test.js', packageJSON)).toBeA('array')
})

it('should return not empty array', () => {
it('should throw for non-existend script', () => {
expect(runScript('test3', 'test.js', packageJSON)[0].task).toThrow()
})

it('should work with a single command', () => {
const res = runScript('test', 'test.js', packageJSON)
expect(res.length).toBe(1)
expect(res[0].title).toBe('test')
expect(res[0].task).toBeA('function')
expect(res[0].task()).toBeAPromise()
})

it('should throw for non-existend script', () => {
expect(runScript('test3', 'test.js', packageJSON)[0].task).toThrow()
})

it('should support array of scripts as a first argument', () => {
const spy = expect.createSpy()
runScript.__set__('execa', spy)
Expand All @@ -67,26 +67,45 @@ describe('runScript', () => {
)
})

it('should pass cwd option to execa if gitDir option is set', () => {
it('should support non npm scripts', () => {
const spy = expect.createSpy()
runScript.__set__('execa', spy)
const res = runScript(['node --arg=true ./myscript.js', 'git add'], 'test.js', packageJSON)
expect(res.length).toBe(2)
expect(res[0].title).toBe('node --arg=true ./myscript.js')
expect(res[1].title).toBe('git add')

expect(res[0].task()).toBeAPromise()
expect(spy.calls.length).toEqual(1)
expect(spy.calls[0].arguments[0]).toContain('node')
expect(spy.calls[0].arguments[1]).toEqual(['--arg=true', './myscript.js', '--', 'test.js'])

expect(res[1].task()).toBeAPromise()
expect(spy.calls.length).toEqual(2)
expect(spy.calls[1].arguments[0]).toContain('git')
expect(spy.calls[1].arguments[1]).toEqual(['add', '--', 'test.js'])
})

it('should pass cwd to execa if gitDir option is set for non-npm tasks', () => {
const spy = expect.createSpy()
runScript.__set__('execa', spy)
const res = runScript(
['test', 'test2'],
['test', 'git add'],
'test.js',
packageJSON,
{ gitDir: '../' }
)
expect(res[0].task()).toBeAPromise()
expect(spy.calls.length).toEqual(1)
expect(spy.calls[0].arguments).toEqual(
['npm', ['run', '--silent', 'test', '--', 'test.js'], { cwd: '../' }]
['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'], { cwd: '../' }]
)
expect(spy.calls[1].arguments[0]).toMatch(/git$/)
expect(spy.calls[1].arguments[1]).toEqual(['add', '--', 'test.js'])
expect(spy.calls[1].arguments[2]).toEqual({ cwd: '../' })
})

it('should use --silent in non-verbose mode', () => {
Expand Down

0 comments on commit 9c45d8e

Please sign in to comment.