Skip to content

Commit

Permalink
feat: if the bin is in PATH, then use that rather than the full path
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Sep 20, 2017
1 parent b58c58b commit 11e690a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"rollup-plugin-node-resolve": "^3.0.0",
"rollup-plugin-uglify": "^2.0.1",
"rollup-watch": "^4.3.1",
"which": "^1.3.0",
"yargs-parser": "^7.0.0"
},
"repository": {
Expand Down
12 changes: 8 additions & 4 deletions src/config/lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
const {resolveBin} = require('../utils')

const kcdScripts = resolveBin('kcd-scripts')

module.exports = {
linters: {
'**/*.+(js|json|less|css|ts)': [
'kcd-scripts format',
'kcd-scripts lint',
'kcd-scripts test --findRelatedTests',
`${kcdScripts} format`,
`${kcdScripts} lint`,
`${kcdScripts} test --findRelatedTests`,
'git add',
],
'.all-contributorsrc': [
// lint-staged passes arguments to the scripts.
// to avoid passing these arguments, we do the echo thing
'kcd-scripts contributors generate',
`${kcdScripts} contributors generate`,
'git add README.md',
],
},
Expand Down
31 changes: 25 additions & 6 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,39 @@ const path = require('path')
const arrify = require('arrify')
const has = require('lodash.has')
const readPkgUp = require('read-pkg-up')
const which = require('which')

const {pkg, path: pkgPath} = readPkgUp.sync({
cwd: fs.realpathSync(process.cwd()),
})
const appDirectory = path.dirname(pkgPath)

// eslint-disable-next-line complexity
function resolveBin(modName, {executable = modName} = {}) {
const modPkgPath = require.resolve(`${modName}/package.json`)
const modPkgDir = path.dirname(modPkgPath)
const {bin} = require(modPkgPath)
if (typeof bin === 'string') {
return path.join(modPkgDir, bin)
let pathFromWhich
try {
pathFromWhich = which.sync(executable)
} catch (_error) {
// ignore _error
}
try {
const modPkgPath = require.resolve(`${modName}/package.json`)
const modPkgDir = path.dirname(modPkgPath)
const {bin} = require(modPkgPath)
if (typeof bin === 'string') {
return path.join(modPkgDir, bin)
}
const fullPathToBin = path.join(modPkgDir, bin[executable])
if (fullPathToBin === pathFromWhich) {
return executable
}
return fullPathToBin
} catch (error) {
if (pathFromWhich) {
return executable
}
throw error
}
return path.join(modPkgDir, bin[executable])
}

const fromRoot = (...p) => path.join(appDirectory, ...p)
Expand Down

0 comments on commit 11e690a

Please sign in to comment.