Skip to content

Commit

Permalink
Support commands with arguments in the lint-staged config and binarie…
Browse files Browse the repository at this point in the history
…s from $PATH. Closes #47
  • Loading branch information
okonet committed Sep 8, 2016
1 parent 02564f7 commit 682a4b9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
- Switched to listr. Simplified code and more beautiful output.
- Switched to execa. Should fix #30
- Use ES2015. Dropped support for Node < 4.x
- Support commands with arguments in the lint-staged config. Closes #47
- Support binaries from $PATH. Closes #47

# 2.0.2

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
"postinstall": "echo \"lint-staged installed! Do not forget to configure it. See https://github.com/okonet/lint-staged/blob/master/README.md\" && exit 0",
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix",
"eslint": "eslint --quiet --fix",
"git:add": "git add",
"pre-commit": "node index.js",
"release": "npmpub",
"test": "mocha --compilers js:babel-core/register ./test/*.spec.js",
Expand All @@ -20,8 +18,9 @@
},
"lint-staged": {
"*.js": [
"eslint",
"git:add"
"eslint --fix",
"git add",
"eslint"
]
},
"pre-commit": [
Expand Down Expand Up @@ -59,7 +58,8 @@
"npm-which": "^3.0.1",
"object-assign": "^4.1.0",
"ora": "^0.2.3",
"staged-git-files": "0.0.4"
"staged-git-files": "0.0.4",
"which": "^1.2.11"
},
"devDependencies": {
"babel-core": "^6.10.4",
Expand Down
40 changes: 29 additions & 11 deletions src/findBin.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
'use strict'

const npmWhich = require('npm-which')(process.cwd())
const which = require('which')

module.exports = function findBin (binName, paths, config) {
const bin = 'npm'
const files = ['--color', '--'].concat(paths)
const args = ['run', '-s', binName].concat(files)
module.exports = function findBin (cmd, paths, config) {
const defaultArgs = ['--'].concat(paths)
/*
* If package.json has script with binName defined
* If package.json has script with cmd defined
* we want it to be executed first
*/
if (config.scripts[binName] !== undefined) {
if (config.scripts[cmd] !== undefined) {
// Support for scripts from package.json
return {
bin,
args
bin: 'npm',
args: ['run', '--silent', cmd].concat(defaultArgs)
}
}

/*
* If binName wasn't found in package.json scripts
* If cmd wasn't found in package.json scripts
* we'll try to locate the binary in node_modules/.bin
* and if this fails in $PATH.
*
* This is useful for shorter configs like:
*
* "lint-staged": {
Expand All @@ -33,8 +34,25 @@ module.exports = function findBin (binName, paths, config) {
* "eslint": "eslint"
* }
*/

const parts = cmd.split(' ')
let bin = parts[0]
let args = parts.splice(1)

try {
/* Firstly, try to resolve the bin in local node_modules/.bin */
bin = npmWhich.sync(bin)
} catch (e) {
/* If this fails, try to resolve binary in $PATH */
try {
bin = which.sync(bin)
} catch (e) {
throw new Error(`${bin} could not be found. Try \`npm install ${bin}\`.`)
}
}

return {
bin: npmWhich.sync(binName),
args: files
bin,
args: args.concat(defaultArgs)
}
}
22 changes: 19 additions & 3 deletions test/findBin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,36 @@ describe('findBin', () => {
findBin.__set__('npmWhich', npmWichMockGood)
const { bin, args } = findBin('eslint', 'test.js', packageJSONMock)
expect(bin).toEqual('npm')
expect(args).toEqual(['run', '-s', 'eslint', '--color', '--', 'test.js'])
expect(args).toEqual(['run', '--silent', 'eslint', '--', 'test.js'])
})

it('should return bin from node_modules/.bin if there is no command in package.json', () => {
findBin.__set__('npmWhich', npmWichMockGood)
const { bin, args } = findBin('eslint', 'test.js test2.js', packageJSON)
expect(bin).toEqual('eslint')
expect(args).toEqual(['--color', '--', 'test.js test2.js'])
expect(args).toEqual(['--', 'test.js test2.js'])
})

it('should parse cmd and add arguments to args', () => {
findBin.__set__('npmWhich', npmWichMockGood)
const { bin, args } = findBin('eslint --fix', 'test.js test2.js', packageJSON)
expect(bin).toEqual('eslint')
expect(args).toEqual(['--fix', '--', 'test.js test2.js'])
})

it('should return bin from $PATH if there is no command in package.json and no bin in node_modules', () => {
findBin.__set__('npmWhich', npmWichMockBad)
findBin.__set__('which', npmWichMockGood)
const { bin, args } = findBin('git add', 'test.js test2.js', packageJSON)
expect(bin).toEqual('git')
expect(args).toEqual(['add', '--', 'test.js test2.js'])
})

it('should return error if bin not found and there is no entry in scripts section', () => {
findBin.__set__('npmWhich', npmWichMockBad)
findBin.__set__('which', npmWichMockBad)
expect(() => {
findBin('eslint', 'test.js', packageJSON)
}).toThrow('not found: eslint')
}).toThrow('eslint could not be found. Try `npm install eslint`.')
})
})

0 comments on commit 682a4b9

Please sign in to comment.