Skip to content

Commit

Permalink
Get path to main with require.resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
ttmarek committed Apr 15, 2017
1 parent d8d0bff commit e2a8ccb
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -85,6 +85,7 @@
"has": "^1.0.1",
"lodash.cond": "^4.3.0",
"minimatch": "^3.0.3",
"pkg-up": "^1.0.0",
"read-pkg-up": "^2.0.0"
},
"nyc": {
Expand Down
12 changes: 3 additions & 9 deletions src/rules/no-import-module-exports.js
@@ -1,16 +1,10 @@
import minimatch from 'minimatch'
import path from 'path'
import readPkgUp from 'read-pkg-up'
import pkgUp from 'pkg-up'

function getEntryPoint(context) {
try {
const pkg = readPkgUp.sync({cwd: context.getFilename(), normalize: false})
return pkg && pkg.pkg
? path.join(process.cwd(), pkg.pkg.main)
: null
} catch (e) {
return null
}
const pkgPath = pkgUp.sync(context.getFilename())
return require.resolve(path.dirname(pkgPath))
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion tests/files/package.json
@@ -1,6 +1,6 @@
{
"dummy": true,
"main": "tests/files/lib/main.js",
"main": "src-root/src-bar.js",
"devDependencies": {
"glob": "1.0.0",
"eslint": "2.x"
Expand Down
4 changes: 2 additions & 2 deletions tests/src/rules/no-import-module-exports.js
Expand Up @@ -47,7 +47,7 @@ ruleTester.run('no-import-module-exports', rule, {
`,
// When the file matches the entry point defined in package.json
// See tests/files/package.json
filename: path.join(process.cwd(), 'tests/files/lib/main.js'),
filename: path.join(process.cwd(), 'tests/files/src-root/src-bar.js'),
}),
test({
code: `
Expand Down Expand Up @@ -95,7 +95,7 @@ ruleTester.run('no-import-module-exports', rule, {
`,
// When the file does not match the entry point defined in package.json
// See tests/files/package.json
filename: path.join(process.cwd(), 'tests/files/not/lib/main.js'),
filename: path.join(process.cwd(), 'tests/files/not/src-root/src-bar.js'),
errors: [error],
}),
test({
Expand Down

0 comments on commit e2a8ccb

Please sign in to comment.