Skip to content

Commit cff06ad

Browse files
critocritocodesuki
authored andcommitted
Traverse directory structure up to add parent node_modules paths. (#9)
To support monorepos and nested packages.
1 parent d9266c0 commit cff06ad

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

add-node-modules-path.el

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,28 @@
3838
;;;###autoload
3939
(defun add-node-modules-path ()
4040
"Search the current buffer's parent directories for `node_modules/.bin`.
41-
If it's found, then add it to the `exec-path'."
41+
Traverse the directory structure up, until reaching the user's home directory.
42+
Any path found is added to the `exec-path'."
4243
(interactive)
43-
(let* ((root (locate-dominating-file
44-
(or (buffer-file-name) default-directory)
45-
"node_modules"))
46-
(path (and root
47-
(expand-file-name "node_modules/.bin/" root))))
48-
(if root
44+
(let* ((file (or (buffer-file-name) default-directory))
45+
(home (expand-file-name "~"))
46+
(root (expand-file-name (locate-dominating-file file "node_modules")))
47+
(roots '()))
48+
(while (and root (not (string= root home)))
49+
(let ((bindir (expand-file-name "node_modules/.bin/" root)))
50+
(when (file-directory-p bindir)
51+
(add-to-list 'roots bindir)))
52+
(setq root (directory-file-name (file-name-directory root))))
53+
(if roots
4954
(progn
5055
(make-local-variable 'exec-path)
51-
(add-to-list 'exec-path path)
52-
(when add-node-modules-path-debug
53-
(message (concat "added " path " to exec-path"))))
56+
(while roots
57+
(add-to-list 'exec-path (car roots))
58+
(when add-node-modules-path-debug
59+
(message (concat "added " (car roots) " to exec-path")))
60+
(setq roots (cdr roots))))
5461
(when add-node-modules-path-debug
55-
(message (concat "node_modules not found in " root))))))
62+
(message (concat "node_modules/.bin not found for " file))))))
5663

5764
(provide 'add-node-modules-path)
5865

0 commit comments

Comments
 (0)