Skip to content

Commit

Permalink
Use npm command to find bin path (#16)
Browse files Browse the repository at this point in the history
Co-authored-by: Neri Marschik <[email protected]>
  • Loading branch information
kayhide and codesuki authored Mar 9, 2022
1 parent 7d9be65 commit d8bd55d
Showing 1 changed file with 25 additions and 36 deletions.
61 changes: 25 additions & 36 deletions add-node-modules-path.el
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@

;;; Commentary:
;;
;; This file provides `add-node-modules-path', which searches
;; the current files parent directories for the `node_modules/.bin/' directory
;; and adds it to the buffer local `exec-path'.
;; 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.
;;
;; Usage:
Expand All @@ -36,49 +35,39 @@
:prefix "add-node-modules-path-"
:group 'environment)

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

;;;###autoload
(defcustom add-node-modules-path-debug nil
"Enable verbose output when non nil."
:type 'boolean
:group 'add-node-modules-path)

;;;###autoload
(defcustom add-node-modules-max-depth 20
"Max depth to look for node_modules."
:type 'integer
:group 'add-node-modules-path)

;;;###autoload
(defun add-node-modules-path ()
"Search the current buffer's parent directories for `node_modules/.bin`.
Traverse the directory structure up, until reaching the user's home directory,
or hitting add-node-modules-max-depth.
Any path found is added to the `exec-path'."
"Run `npm bin` command and add the path to the `exec-path`.
If `npm` command fails, it does nothing."
(interactive)
(let* ((default-dir (expand-file-name default-directory))
(file (or (buffer-file-name) default-dir))
(home (expand-file-name "~"))
(iterations add-node-modules-max-depth)
(root (directory-file-name (or (and (buffer-file-name) (file-name-directory (buffer-file-name))) default-dir)))
(roots '()))
(while (and root (> iterations 0))
(setq iterations (1- iterations))
(let ((bindir (expand-file-name "node_modules/.bin/" root)))
(when (file-directory-p bindir)
(add-to-list 'roots bindir)))
(if (string= root home)
(setq root nil)
(setq root (directory-file-name (file-name-directory root)))))
(if roots
(progn
(make-local-variable 'exec-path)
(while roots
(add-to-list 'exec-path (car roots))
(when add-node-modules-path-debug
(message (concat "added " (car roots) " to exec-path")))
(setq roots (cdr roots))))

(let* ((res (s-chomp (shell-command-to-string add-node-modules-path-command)))
(exists (file-exists-p res))
)
(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))
)
(t
(when add-node-modules-path-debug
(message (concat "node_modules/.bin not found for " file))))))
(message "Failed to run `%s':\n %s" add-node-modules-path-command res))
))
)
)

(provide 'add-node-modules-path)

Expand Down

0 comments on commit d8bd55d

Please sign in to comment.