-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
indium-nodejs.el
110 lines (89 loc) · 4.08 KB
/
indium-nodejs.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
;;; indium-nodejs.el --- NodeJS support for indium -*- lexical-binding: t; -*-
;; Copyright (C) 2016-2018 Nicolas Petton
;; Author: Nicolas Petton <[email protected]>
;; Keywords: tools, javascript
;; 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:
;; Handle indium connections to a NodeJS process using the v8 backend.
;;
;; Important note: For this package to work, NodeJS version 7.0 (or any newer
;; version) is required.
;;; Code:
(require 'url)
(require 'url-parse)
(require 'json)
(require 'map)
(require 'seq)
(require 'subr-x)
(declare-function indium-client-connect "indium-client.el")
(declare-function indium-client-disconnect "indium-client.el")
(defun indium-launch-nodejs (conf)
"Start a NodeJS process.
Execute the command specified in CONF, adding the `--inspect'
flag. When the process is ready, open an Indium connection on
it.
If the configuration setting `inspect-brk' is non-nil, break the
execution at the first statement."
(let-alist conf
(unless .program
(user-error "No NodeJS program specified in the .indium.json file"))
(let* ((default-directory .resolvedRoot)
(filter (indium-nodejs--process-filter-function conf))
(command-with-flags (indium-nodejs--command-with-flags
.program
.args
.inspect-brk
.port))
(process (make-process :name "indium-nodejs-process"
:buffer "*node process*"
:filter filter
:command (list shell-file-name
shell-command-switch
command-with-flags))))
(message "Running node command \"%s\"" command-with-flags)
(switch-to-buffer (process-buffer process)))))
(defun indium-nodejs--command-with-flags (program args inspect-brk &optional port)
"Return a command string with flags to start the V8 inspector.
PROGRAM is the executable to run, with ARGS being the passed to the program.
If INSPECT-BRK is nil, use the `--inspect', use the
`--inspect-brk' flag otherwise.
If PORT is non-nil, start the debugging process on that port,
otherwise use Node's default port (9229)."
(let ((inspect-flag (if (eq inspect-brk t) " --inspect-brk" " --inspect"))
(inspect-port-flag (if port (format " --inspect-port=%s" port) "")))
(format "%s%s%s %s" program inspect-flag inspect-port-flag args)))
(defun indium-nodejs--process-filter-function (conf)
"Return a process filter function for CONF.
The function watches the process output to determine when to connect and
disconnect from the process."
(let ((connected))
(lambda (process output)
;; Append output to the process buffer
(with-current-buffer (process-buffer process)
(goto-char (point-max))
(insert output))
(cond
((and (not connected)
(string-match-p "Debugger listening on" output))
;; Node will keep outputting the "Debugger listening on" message after
;; each deconnection, so only try to connect one.
(setq connected t)
(let-alist conf
(indium-client-connect (file-name-directory .projectFile) .name)))
((string-match-p "Waiting for the debugger to disconnect" output)
;; When Node receives a signal to exit, it first waits for all
;; debuggers to disconnect before shutting down. Watch for the
;; "Waiting for the debugger to disconnect" message and then do so.
(setq connected nil)
(indium-client-disconnect))))))
(provide 'indium-nodejs)
;;; indium-nodejs.el ends here