about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/add-node-modules-path-20180710.2342/add-node-modules-path.el
blob: cd8adc0f8d511eae93a2a3ed3d5ce0649843804d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
;;; add-node-modules-path.el --- Add node_modules to your exec-path

;; Copyright (C) 2016 Neri Marschik
;; This package uses the MIT License.
;; See the LICENSE file.

;; Author: Neri Marschik <marschik_neri@cyberagent.co.jp>
;; Version: 1.0
;; Package-Version: 20180710.2342
;; Package-Requires: ()
;; Keywords: javascript, node, node_modules, eslint
;; URL: https://github.com/codesuki/add-node-modules-path

;;; 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 allows Emacs to find project based installs of e.g. eslint.
;;
;; Usage:
;;     M-x add-node-modules-path
;;
;;     To automatically run it when opening a new buffer:
;;     (Choose depending on your favorite mode.)
;;
;;     (eval-after-load 'js-mode
;;       '(add-hook 'js-mode-hook #'add-node-modules-path))
;;
;;     (eval-after-load 'js2-mode
;;       '(add-hook 'js2-mode-hook #'add-node-modules-path))

;;; Code:

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

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

;;;###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'."
  (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))))
      (when add-node-modules-path-debug
        (message (concat "node_modules/.bin not found for " file))))))

(provide 'add-node-modules-path)

;;; add-node-modules-path.el ends here