-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperso-node.el
66 lines (50 loc) · 2.38 KB
/
perso-node.el
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
;;; perso-node.el --- NodeJS and related tools stuff -*- lexical-binding: t; -*-
;; Copyright (C) 2016 Jerome Truong
;; Author: Jerome Truong <nymo@tack>
;; Keywords: node, npm
;; 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:
;;
;;; Code:
(defun perso/node/start (buffer-name)
"Start node in its own window each time, instead of new/separate *Async Shell Command* buffers"
(interactive "sBuffer Name: ")
(when (or (null buffer-name)
(zerop (length (jqt/trim-string buffer-name))))
(setq buffer-name "node"))
(let* ((root (perso/node/find-root))
(buffer-name (concat (file-name-nondirectory (directory-file-name (file-truename root))) "/" buffer-name)))
(async-shell-command (concat "cd " root "; npm start") buffer-name)))
(defun perso/node/run (&optional script)
"Runs `npm run SCRIPT' from the project root."
(interactive "sScript: ")
(let ((script (if (zerop (length (jqt/trim-string script))) "build" script)))
(async-shell-command (concat "cd " (perso/node/find-root) "; npm run " script))))
(defmacro perso/node/run/script (script)
"Creates a function which calls `(perso/node/run SCRIPT)'."
`(defun ,(intern (concat "perso/node/run/" script)) ()
"Runs `npm run SCRIPT' from the project root."
(interactive)
(async-shell-command (concat "cd " (perso/node/find-root) "; npm run " ,script))))
(perso/node/run/script "build")
(perso/node/run/script "build:less")
(defun perso/node/find-root (&optional dir)
"Finds root package.json by traversing upwards from current
directory."
(interactive)
(let* ((cwd (or dir default-directory))
(files (directory-files cwd)))
(if (member "package.json" files)
cwd
(perso/node/find-root(concat cwd "../")))))
(provide 'perso-node)
;;; perso-node.el ends here