Repository: mantoni/eslint_d.js (js)

Issue:
Specify path to eslint
Hello, thank you for your work on this project!

From the README:

>  If eslint is found in the current working directories node_modules folder, then this version of eslint is going to be used. Otherwise, the version of eslint that ships with eslint_d is used as a fallback.

Is there a way to specify the path to where `eslint` can be found?

We have many repos, all with the same ESLint configuration and rules.

Thus, we have a centralized repo that contains `node_modules/` with `eslint` and the custom rules.

Therefore, each project won't have a `node_modules/`  directory with `eslint`, and we don't want to fallback to the version of `eslint` that ships with `eslint_d`.


Pull request:
Allow overriding eslint path with environment var
Fixes #242 

When `ESLINT_ROOT` is provided, attempt to resolve eslint from that path before checking `process.cwd`

This seemed like the simplest approach to me without re-working all of `ESLINT_D_MISS` behavior

Patch:
diff --git a/README.md b/README.md
index 24c0243..af70763 100644
--- a/README.md
+++ b/README.md
@@ -121,6 +121,9 @@ All arguments are passed to eslint, except for the following commands:
 - `ESLINT_D_MISS` How to behave if local eslint is missing. "fallback" uses the
   bundled eslint (default). "fail" logs an error and exits with code 1.
   "ignore" silently exits with code 0.
+- `ESLINT_D_ROOT` Provide specific directory to search for `node_modules/eslint`.
+  Useful in monorepos where `node_modules` might be in a location other than
+  the project root.
 
 ## Automatic fixing
 
diff --git a/lib/help.js b/lib/help.js
index 4ec8ff6..af65d0d 100644
--- a/lib/help.js
+++ b/lib/help.js
@@ -21,5 +21,8 @@ Environment variables:
   ESLINT_D_MISS   How to behave if local eslint is missing. "fallback" uses the
                   bundled eslint (default). "fail" logs an error and exits with
                   code 1. "ignore" silently exits with code 0.
+  ESLINT_D_ROOT   Provide specific directory to search for node_modules/eslint.
+                  Useful in monorepos where node_modules might be in a location other than
+                  the project root.
 `);
 }
diff --git a/lib/resolver.js b/lib/resolver.js
index 08576c5..7ccefb7 100644
--- a/lib/resolver.js
+++ b/lib/resolver.js
@@ -16,8 +16,16 @@ export function createResolver() {
   const local = getLocal();
   let bundled = false;
   let path;
+
   try {
-    path = require.resolve('eslint/package.json', { paths: [process.cwd()] });
+    path = require.resolve('eslint/package.json', {
+      // Allow specific path to eslint with ESLINT_D_ROOT environment variable
+      // This is useful for monorepos where the location of node_modules can be more complex
+      paths: [
+        ...(process.env.ESLINT_D_ROOT ? [process.env.ESLINT_D_ROOT] : []),
+        process.cwd()
+      ]
+    });
   } catch (err) {
     if (local === 'ignore') {
       return 'ignore';


Test patch:
diff --git a/lib/resolver.test.js b/lib/resolver.test.js
index 64247ba..918c941 100644
--- a/lib/resolver.test.js
+++ b/lib/resolver.test.js
@@ -102,4 +102,21 @@ describe('lib/resolver', () => {
       });
     });
   });
+
+  context('with override', () => {
+    beforeEach(() => {
+      process.env.ESLINT_D_ROOT = resolve('test/fixture/v8.0.x');
+    });
+
+    it('uses path from environment variable when present', () => {
+      const resolver = createResolver();
+
+      refute.isString(resolver);
+      assert.isFalse(resolver['bundled']);
+      assert.equals(
+        resolver['base'],
+        normalize(`${process.env.ESLINT_D_ROOT}/node_modules/eslint`)
+      );
+    });
+  });
 });