Created
June 29, 2016 13:11
-
-
Save jscheid/2253e849e63d84b0ec5434fe6eb558cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;;; node-local-flycheck -- Try to use linters from node_modules, and node from nvm -*- lexical-binding: t -*- | |
| ;; (C) 2016 Julian Scheid | |
| ;; Author: Julian Scheid <[email protected]> | |
| ;; Version: 0.1.0 | |
| ;; Keywords: convenience flycheck javascript | |
| ;; This file is not part of GNU Emacs. | |
| ;; This program is free software: you can redistribute it and/or modify | |
| ;; it under the terms of the GNU General Public License as published by | |
| ;; the Free Software Foundation, either version 3 of the License, or | |
| ;; (at your option) any later version. | |
| ;; This program is distributed in the hope that it will be useful, | |
| ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| ;; GNU General Public License for more details. | |
| ;; You should have received a copy of the GNU General Public License | |
| ;; along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| ;;; Commentary: | |
| ;; Many node projects have linters installed locally. In this case, | |
| ;; using those is preferred to any globally installed versions. | |
| ;; | |
| ;; Also, node projects often use nvm to specify a given node version. | |
| ;; Again, using the system node version can cause discrepancies. | |
| ;; | |
| ;; Loosely based on http://emacs.stackexchange.com/a/21207 | |
| ;;; Code: | |
| (require 'flycheck) | |
| (require 'f) | |
| (defun node-local-flycheck () | |
| "Try overriding flycheck executables with version from node_module. | |
| Also use node from .nvmrc if possible." | |
| (let* ((root (locate-dominating-file (or (buffer-file-name) default-directory) | |
| "node_modules")) | |
| (checker (flycheck-get-checker-for-buffer)) | |
| (local-executable | |
| (and root | |
| (f-expand (flycheck-checker-default-executable checker) | |
| (f-expand "node_modules/.bin" root))))) | |
| (when (and local-executable | |
| (file-executable-p local-executable)) | |
| ;; Local executable is good, use it | |
| (set (make-local-variable (intern (format "flycheck-%s-executable" checker))) | |
| local-executable) | |
| ;; Find local .nvmrc | |
| (let ((nvmrc-path (f-expand ".nvmrc" root))) | |
| (when (file-readable-p nvmrc-path) | |
| ;; .nvmrc is good, derive node executable from it | |
| (let ((node (f-join (f-full "~/.nvm/versions/node") | |
| (s-trim (f-read nvmrc-path)) | |
| "bin" | |
| "node"))) | |
| (when (file-executable-p node) | |
| ;; Node executable is good, use it | |
| (setq-local flycheck-command-wrapper-function | |
| (lambda (command) (cons node command)))))))))) | |
| (add-hook 'flycheck-mode-hook #'node-local-flycheck) | |
| (provide 'node-local-flycheck) | |
| ;;; node-local-flycheck.el ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment