|
12 | 12 |
|
13 | 13 | ;;; Commentary: |
14 | 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'. |
| 15 | +;; This file provides `add-node-modules-path', which runs `npm bin` and |
| 16 | +;; and adds the path to the buffer local `exec-path'. |
18 | 17 | ;; This allows Emacs to find project based installs of e.g. eslint. |
19 | 18 | ;; |
20 | 19 | ;; Usage: |
|
36 | 35 | :prefix "add-node-modules-path-" |
37 | 36 | :group 'environment) |
38 | 37 |
|
| 38 | +;;;###autoload |
| 39 | +(defcustom add-node-modules-path-command "npm bin" |
| 40 | + "Command to find the bin path." |
| 41 | + :type 'string) |
| 42 | + |
39 | 43 | ;;;###autoload |
40 | 44 | (defcustom add-node-modules-path-debug nil |
41 | 45 | "Enable verbose output when non nil." |
42 | 46 | :type 'boolean |
43 | 47 | :group 'add-node-modules-path) |
44 | 48 |
|
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 | | - |
51 | 49 | ;;;###autoload |
52 | 50 | (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." |
57 | 53 | (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 |
80 | 66 | (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 | + ) |
82 | 71 |
|
83 | 72 | (provide 'add-node-modules-path) |
84 | 73 |
|
|
0 commit comments