Node Process Spawn help

236 views
Skip to first unread message

Nick Featch

unread,
Mar 11, 2014, 6:22:48 PM3/11/14
to clojur...@googlegroups.com
Hi we have a Clojurescript exec process working

(def child-process (nd/require "child_process"))

(defn exec [cmd opt fn] (.exec child-process cmd opt fn))

(let [opt (clojuremap->jsobj {:cwd "path"})]
(exec "ls -la" opt (fn [err stdout stderr] ....)))

But can't figure out how to swap exec for spawn. Has any one done this?

Joaquín Oltra

unread,
Mar 12, 2014, 4:42:27 PM3/12/14
to clojur...@googlegroups.com
Take a good look at the node.js docs, and show how you are swapping exec for spawn. They are different functions and they take different arguments. One difference for example is that the second argument in spawn is an array with the args and in exec is options for the execution.

http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

Another big difference is that exec gives you the results in the callback and spawn returns a ChildProcess object which is an event emitter, so it is not a matter of just swapping which function you are using.

Look at the examples in the docs and translate them to cljs, it should be easy and there are plenty of them.

Cheers

Nick Featch

unread,
Mar 13, 2014, 1:03:27 PM3/13/14
to clojur...@googlegroups.com
Hi thanks for your reply. Sorry my post did suggest I was simply just swapping .exec for .spawn and hopping it would work ha ha. I've studied the docs and tried to apply the differences as you can see below.

(def child-process (nd/require "child_process"))

(defn spawn [cmd arg opt] (.spawn child-process cmd arg opt))

(let [opt (utl/cmap->jobj {:cwd project-path})
proc (env/spawn "silk" (array "reload") opt)]
(.on (.-stdout proc) 'data' (fn [data]
(.log js/console data))))

The process runs (I can see it creating the file and directories I expect) but no logging is preformed. Can you pass a clojurescript anonymous function to a JavaScipt method?




Joaquín Oltra Hernández

unread,
Mar 14, 2014, 5:04:51 AM3/14/14
to clojur...@googlegroups.com
Yes, they are the same and can be used interchangeably.

I've tried it and it works without any problem.

ClojureScript:cljs.user> (def spawn (.-spawn (js/require "child_process")))
#<function (file /*, args, options*/) {
...
ClojureScript:cljs.user> (let [p (spawn "ls" #js["-l"])]
                           (.on (.-stdout p) "data" println))
#<[object Object]>
#<total 1064
-rw-r--r--  1 jkn  staff    1817 Feb  5 13:27 README.md
drwxr-xr-x  3 jkn  staff     102 Feb  6 10:53 checkouts
-rw-r--r--  1 jkn  staff  536522 Feb  5 13:27 cljs_node_lighttable.js
-rw-r--r--  1 jkn  staff     830 Feb  5 13:27 project.clj
drwxr-xr-x  3 jkn  staff     102 Feb  5 13:27 src
drwxr-xr-x  4 jkn  staff     136 Feb 10 15:04 target
>







--
Note that posts from new members are moderated - please be patient with your first post.
---
You received this message because you are subscribed to a topic in the Google Groups "ClojureScript" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojurescript/TObqicBxmKQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojurescrip...@googlegroups.com.
To post to this group, send email to clojur...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojurescript.

Nick Featch

unread,
Mar 15, 2014, 11:30:03 AM3/15/14
to clojur...@googlegroups.com, joa...@chimeces.com
Oh wow you've solved it, thank you. The way I'd imported is flawed, your way works.

Joaquín Oltra Hernández

unread,
Mar 15, 2014, 12:42:02 PM3/15/14
to clojur...@googlegroups.com

Glad I could help :)

Reply all
Reply to author
Forward
0 new messages