Skip to content

Commit 93657bc

Browse files
sinistraldnolen
authored andcommitted
NodeJS REPL accepts a :path opt to set NODE_PATH.
This makes it possible to specify a NodeJS module search path so that modules don't need to be installed globally.
1 parent 00309e8 commit 93657bc

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/main/clojure/cljs/repl/node.clj

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@
9696
(.printStackTrace e *err*))))
9797
(recur buf)))))
9898

99+
(defn- build-process
100+
[opts repl-env input-src]
101+
(let [xs (cond-> [(get opts :node-command "node")]
102+
(:debug-port repl-env) (conj (str "--debug=" (:debug-port repl-env))))
103+
proc (-> (ProcessBuilder. (into-array xs)) (.redirectInput input-src))]
104+
(when-let [path-fs (:path repl-env)]
105+
(.put (.environment proc)
106+
"NODE_PATH"
107+
(string/join File/pathSeparator
108+
(map #(.getAbsolutePath (io/as-file %)) path-fs))))
109+
proc))
110+
99111
(defn setup
100112
([repl-env] (setup repl-env nil))
101113
([repl-env opts]
@@ -106,11 +118,7 @@
106118
(string/replace (slurp (io/resource "cljs/repl/node_repl.js"))
107119
"var PORT = 5001;"
108120
(str "var PORT = " (:port repl-env) ";")))
109-
xs (cond-> [(get opts :node-command "node")]
110-
(:debug-port repl-env) (conj (str "--debug=" (:debug-port repl-env))))
111-
proc (-> (ProcessBuilder. (into-array xs))
112-
(.redirectInput of)
113-
.start)
121+
proc (.start (build-process opts repl-env of))
114122
_ (do (.start (Thread. (bound-fn [] (pipe proc (.getInputStream proc) *out*))))
115123
(.start (Thread. (bound-fn [] (pipe proc (.getErrorStream proc) *err*)))))
116124
env (ana/empty-env)
@@ -189,7 +197,7 @@
189197
(js/CLOSURE_IMPORT_SCRIPT
190198
(aget (.. js/goog -dependencies_ -nameToPath) name))))))))))
191199

192-
(defrecord NodeEnv [host port socket proc]
200+
(defrecord NodeEnv [host port path socket proc]
193201
repl/IReplEnvOptions
194202
(-repl-options [this]
195203
{:output-dir ".cljs_node_repl"
@@ -209,18 +217,19 @@
209217
(close-socket @socket)))
210218

211219
(defn repl-env* [options]
212-
(let [{:keys [host port debug-port]}
220+
(let [{:keys [host port path debug-port]}
213221
(merge
214222
{:host "localhost"
215223
:port (+ 49000 (rand-int 10000))}
216224
options)]
217-
(assoc (NodeEnv. host port (atom nil) (atom nil))
225+
(assoc (NodeEnv. host port path (atom nil) (atom nil))
218226
:debug-port debug-port)))
219227

220228
(defn repl-env
221-
"Construct a Node.js evalution environment. Can supply :host and :port."
229+
"Construct a Node.js evalution environment. Can supply :host, :port
230+
and :path (a vector used as the NODE_PATH)."
222231
[& {:as options}]
223232
(repl-env* options))
224233

225234
(defn -main []
226-
(repl/repl (repl-env)))
235+
(repl/repl (repl-env)))

0 commit comments

Comments
 (0)