|
| 1 | +;;; add-node-modules-path.el --- Add node_modules to your exec-path |
| 2 | + |
| 3 | +;; Copyright (C) 2016 Neri Marschik |
| 4 | +;; This package uses the MIT License. |
| 5 | +;; See the LICENSE file. |
| 6 | + |
| 7 | +;; Author: Neri Marschik <[email protected]> |
| 8 | +;; Version: 1.0 |
| 9 | +;; Package-Requires: () |
| 10 | +;; Keywords: javascript, node, node_modules, eslint |
| 11 | +;; URL: https://github.com/codesuki/add-node-modules-path |
| 12 | + |
| 13 | +;;; Commentary: |
| 14 | +;; |
| 15 | +;; This file provides `add-node-modules-path', which searches |
| 16 | +;; the current files parent directories for the `node_modules/.bin/' directory |
| 17 | +;; and adds it to the buffer local `exec-path'. |
| 18 | +;; This allows Emacs to find project based installs of e.g. eslint. |
| 19 | +;; |
| 20 | +;; Usage: |
| 21 | +;; M-x add-node-modules-path |
| 22 | +;; |
| 23 | +;; To automatically run it when opening a new buffer: |
| 24 | +;; (Choose depending on your favorite mode.) |
| 25 | +;; |
| 26 | +;; (eval-after-load 'js-mode |
| 27 | +;; '(add-hook 'js-mode-hook #'add-node-modules-path)) |
| 28 | +;; |
| 29 | +;; (eval-after-load 'js2-mode |
| 30 | +;; '(add-hook 'js2-mode-hook #'add-node-modules-path)) |
| 31 | + |
| 32 | +;;; Code: |
| 33 | + |
| 34 | +;;;###autoload |
| 35 | +(defun add-node-modules-path () |
| 36 | + (let* ((root (locate-dominating-file |
| 37 | + (or (buffer-file-name) default-directory) |
| 38 | + "node_modules")) |
| 39 | + (path (and root |
| 40 | + (expand-file-name "node_modules/.bin/" root)))) |
| 41 | + (if root |
| 42 | + (progn |
| 43 | + (make-local-variable 'exec-path) |
| 44 | + (add-to-list 'exec-path path) |
| 45 | + (message "added node_modules to exec-path")) |
| 46 | + (message "node_modules not found")))) |
| 47 | + |
| 48 | +(provide 'add-node-modules-path) |
| 49 | + |
| 50 | +;;; add-node-modules-path.el ends here |
0 commit comments