Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recursion search 'node_modules/.bin' through directory tree #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions add-node-modules-path.el
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

;;; Commentary:
;;
;; This file provides `add-node-modules-path', which runs `npm bin` and
;; and adds the path to the buffer local `exec-path'.
;; This allows Emacs to find project based installs of e.g. eslint.
;; This file provides `add-node-modules-path', which runs `npm bin`
;; recursively through up directory tree and and adds the path to the
;; buffer local `exec-path'. This allows Emacs to find project based
;; installs of e.g. eslint.
;;
;; Usage:
;; M-x add-node-modules-path
Expand All @@ -38,10 +39,15 @@
:group 'environment)

;;;###autoload
(defcustom add-node-modules-path-command "npm bin"
(defcustom add-node-modules-path-command-bin "npm bin"
"Command to find the bin path."
:type 'string)

;;;###autoload
(defcustom add-node-modules-path-command-root "npm root"
"Command to find the root path."
:type 'string)

;;;###autoload
(defcustom add-node-modules-path-debug nil
"Enable verbose output when non nil."
Expand All @@ -54,22 +60,24 @@
If `npm` command fails, it does nothing."
(interactive)

(let* ((res (s-chomp (shell-command-to-string add-node-modules-path-command)))
(exists (file-exists-p res))
)
(let* ((root-directory (s-chomp (shell-command-to-string add-node-modules-path-command-root)))
(bin-directory (s-chomp (shell-command-to-string add-node-modules-path-command-bin)))
(exists (file-exists-p bin-directory))
(isRoot (string= (directory-file-name (file-name-directory root-directory)) "/")))
(cond
(exists
(make-local-variable 'exec-path)
(add-to-list 'exec-path res)
(when add-node-modules-path-debug
(message "Added to `exec-path`: %s" res))
)
((not isRoot)
(unless (local-variable-p 'exec-path)
(make-local-variable 'exec-path))
(when exists
(add-to-list 'exec-path bin-directory)
(when add-node-modules-path-debug
(message "Added to `exec-path`: %s" bin-directory)))
(cd (concat root-directory "/../.."))
(add-node-modules-path))
(t
(cd (file-name-directory (buffer-file-name)))
(when add-node-modules-path-debug
(message "Failed to run `%s':\n %s" add-node-modules-path-command res))
))
)
)
(message "Found root directory"))))))

(provide 'add-node-modules-path)

Expand Down