Skip to content

Commit d8bd55d

Browse files
kayhidecodesuki
andauthored
Use npm command to find bin path (#16)
Co-authored-by: Neri Marschik <codesuki@users.noreply.github.com>
1 parent 7d9be65 commit d8bd55d

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

add-node-modules-path.el

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212

1313
;;; Commentary:
1414
;;
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'.
15+
;; This file provides `add-node-modules-path', which runs `npm bin` and
16+
;; and adds the path to the buffer local `exec-path'.
1817
;; This allows Emacs to find project based installs of e.g. eslint.
1918
;;
2019
;; Usage:
@@ -36,49 +35,39 @@
3635
:prefix "add-node-modules-path-"
3736
:group 'environment)
3837

38+
;;;###autoload
39+
(defcustom add-node-modules-path-command "npm bin"
40+
"Command to find the bin path."
41+
:type 'string)
42+
3943
;;;###autoload
4044
(defcustom add-node-modules-path-debug nil
4145
"Enable verbose output when non nil."
4246
:type 'boolean
4347
:group 'add-node-modules-path)
4448

45-
;;;###autoload
46-
(defcustom add-node-modules-max-depth 20
47-
"Max depth to look for node_modules."
48-
:type 'integer
49-
:group 'add-node-modules-path)
50-
5149
;;;###autoload
5250
(defun add-node-modules-path ()
53-
"Search the current buffer's parent directories for `node_modules/.bin`.
54-
Traverse the directory structure up, until reaching the user's home directory,
55-
or hitting add-node-modules-max-depth.
56-
Any path found is added to the `exec-path'."
51+
"Run `npm bin` command and add the path to the `exec-path`.
52+
If `npm` command fails, it does nothing."
5753
(interactive)
58-
(let* ((default-dir (expand-file-name default-directory))
59-
(file (or (buffer-file-name) default-dir))
60-
(home (expand-file-name "~"))
61-
(iterations add-node-modules-max-depth)
62-
(root (directory-file-name (or (and (buffer-file-name) (file-name-directory (buffer-file-name))) default-dir)))
63-
(roots '()))
64-
(while (and root (> iterations 0))
65-
(setq iterations (1- iterations))
66-
(let ((bindir (expand-file-name "node_modules/.bin/" root)))
67-
(when (file-directory-p bindir)
68-
(add-to-list 'roots bindir)))
69-
(if (string= root home)
70-
(setq root nil)
71-
(setq root (directory-file-name (file-name-directory root)))))
72-
(if roots
73-
(progn
74-
(make-local-variable 'exec-path)
75-
(while roots
76-
(add-to-list 'exec-path (car roots))
77-
(when add-node-modules-path-debug
78-
(message (concat "added " (car roots) " to exec-path")))
79-
(setq roots (cdr roots))))
54+
55+
(let* ((res (s-chomp (shell-command-to-string add-node-modules-path-command)))
56+
(exists (file-exists-p res))
57+
)
58+
(cond
59+
(exists
60+
(make-local-variable 'exec-path)
61+
(add-to-list 'exec-path res)
62+
(when add-node-modules-path-debug
63+
(message "Added to `exec-path`: %s" res))
64+
)
65+
(t
8066
(when add-node-modules-path-debug
81-
(message (concat "node_modules/.bin not found for " file))))))
67+
(message "Failed to run `%s':\n %s" add-node-modules-path-command res))
68+
))
69+
)
70+
)
8271

8372
(provide 'add-node-modules-path)
8473

0 commit comments

Comments
 (0)