Skip to content

Commit

Permalink
return the path to the detected file (or null)
Browse files Browse the repository at this point in the history
bahaviour should be the same, because truthiness, but this may
make the package moer useful
  • Loading branch information
chee committed Aug 12, 2017
1 parent 7d10759 commit a8b71e2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -11,7 +11,7 @@ use like:
```js
import detectEslintConfig from 'detect-eslint-config'

const hasEslintConfig = detectEslintConfig('dog/face')
const hasEslintConfig = !!detectEslintConfig('dog/face')

if (hasEslintConfig) {
console.log('we have found an eslint config for this project')
Expand Down
28 changes: 16 additions & 12 deletions dist/index.js
@@ -1,10 +1,8 @@
var ref = require('path');
var dirname = ref.dirname;
var resolve = ref.resolve;
var path = require('path')

var ref$1 = require('fs');
var existsSync = ref$1.existsSync;
var lstatSync = ref$1.lstatSync;
var ref = require('fs');
var existsSync = ref.existsSync;
var statSync = ref.statSync;

var PACKAGE_JSON = 'package.json'

Expand All @@ -14,22 +12,28 @@ var ESLINT_FILES =

var ESLINT_NODE = 'eslintConfig'

var isDirectory = function (file) { return lstatSync(file).isDirectory(); }
var isRoot = function (directory) { return directory === resolve(directory, '..'); }
var isDirectory = function (file) { return statSync(file).isDirectory(); }
var isRoot = function (directory) { return directory === path.resolve(directory, '..'); }
var createResolver = function (directory) { return function (file) { return path.resolve(directory, file); }; }
var dirname = path.dirname;

module.exports = function detectEslintConfig (file) {
var directory = isDirectory(file) ? file : dirname(file)

var resolve = createResolver(directory)

if (isRoot(directory)) { return false }

var packageJson = resolve(directory, PACKAGE_JSON)
var packageJson = resolve(PACKAGE_JSON)

var eslintFileDetected = !!ESLINT_FILES.find(function (eslintFile) { return existsSync(resolve(directory, eslintFile)); }
var detectedFile = ESLINT_FILES.find(function (eslintFile) { return existsSync(resolve(eslintFile)); }
)

if (existsSync(packageJson)) {
return eslintFileDetected || !!require(packageJson)[ESLINT_NODE]
return (detectedFile && resolve(detectedFile)) ||
(require(packageJson)[ESLINT_NODE] && packageJson)
}

return eslintFileDetected || detectEslintConfig(resolve(directory, '..'))
return (detectedFile && resolve(detectedFile)) ||
detectEslintConfig(resolve('..'))
}
26 changes: 16 additions & 10 deletions source/index.js
@@ -1,6 +1,6 @@
const {dirname, resolve} = require('path')
const path = require('path')

const {existsSync, lstatSync} = require('fs')
const {existsSync, statSync} = require('fs')

const PACKAGE_JSON = 'package.json'

Expand All @@ -10,23 +10,29 @@ const ESLINT_FILES =

const ESLINT_NODE = 'eslintConfig'

const isDirectory = file => lstatSync(file).isDirectory()
const isRoot = directory => directory === resolve(directory, '..')
const isDirectory = file => statSync(file).isDirectory()
const isRoot = directory => directory === path.resolve(directory, '..')
const createResolver = directory => file => path.resolve(directory, file)
const {dirname} = path

module.exports = function detectEslintConfig (file) {
const directory = isDirectory(file) ? file : dirname(file)

if (isRoot(directory)) return false
const resolve = createResolver(directory)

const packageJson = resolve(directory, PACKAGE_JSON)
if (isRoot(directory)) return null

const eslintFileDetected = !!ESLINT_FILES.find(eslintFile =>
existsSync(resolve(directory, eslintFile))
const packageJson = resolve(PACKAGE_JSON)

const detectedFile = ESLINT_FILES.find(eslintFile =>
existsSync(resolve(eslintFile))
)

if (existsSync(packageJson)) {
return eslintFileDetected || !!require(packageJson)[ESLINT_NODE]
return (detectedFile && resolve(detectedFile)) ||
(require(packageJson)[ESLINT_NODE] && packageJson)
}

return eslintFileDetected || detectEslintConfig(resolve(directory, '..'))
return (detectedFile && resolve(detectedFile)) ||
detectEslintConfig(resolve('..'))
}
24 changes: 19 additions & 5 deletions tests/index.js
Expand Up @@ -4,35 +4,49 @@ const detectConfig = require('../dist')
it('correctly detects eslint config in a package.json', tape => {
const hasConfig = detectConfig(`${__dirname}/packagejson/beak.js`)
tape.plan(1)
tape.equal(hasConfig, true)
tape.equal(!!hasConfig, true)
tape.end()
})

it('correctly detects eslint config in a project root', tape => {
const hasConfig = detectConfig(`${__dirname}/root/index.js`)
tape.plan(1)
tape.equal(hasConfig, true)
tape.equal(!!hasConfig, true)
tape.end()
})

it('correctly detects eslint config in a subdirectory', tape => {
const hasConfig =
detectConfig(`${__dirname}/subdirectory/help-me/charlie/index.js`)
tape.plan(1)
tape.equal(hasConfig, true)
tape.equal(!!hasConfig, true)
tape.end()
})

it('returns false if no eslint config is detected', tape => {
const hasConfig = detectConfig(`${__dirname}/none/pineapples.js`)
tape.plan(1)
tape.equal(hasConfig, false)
tape.equal(!!hasConfig, false)
tape.end()
})

it('works if you specify a directory', tape => {
const hasConfig = detectConfig(`${__dirname}/root/`)
tape.plan(1)
tape.equal(hasConfig, true)
tape.equal(!!hasConfig, true)
tape.end()
})

it('returns the path to the file', tape => {
const hasConfig = detectConfig(`${__dirname}/root/index.js`)
tape.plan(1)
tape.equal(hasConfig.endsWith('root/.eslintrc.json'), true)
tape.end()
})

it('returns the path to the package.json', tape => {
const hasConfig = detectConfig(`${__dirname}/packagejson/beak.js`)
tape.plan(1)
tape.equal(hasConfig.endsWith('packagejson/package.json'), true)
tape.end()
})

0 comments on commit a8b71e2

Please sign in to comment.