Skip to content

Instantly share code, notes, and snippets.

@eigenhombre
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save eigenhombre/2c6ee5257790406bef3a to your computer and use it in GitHub Desktop.

Select an option

Save eigenhombre/2c6ee5257790406bef3a to your computer and use it in GitHub Desktop.
Using ClojureScript + Node for scripting

This took me awhile to get right. I wanted to see if Node + cljs might be suitable for scripting tasks where loading the entire Clojure infrastructure would be painfully slow.

I used lein new mies-node to make the project. This works with my installed Node.js (0.10) but stopped working when I upgraded to Node 0.12, so I rolled back.

Since Node 0.10 child_process doesn’t support spawnSync like Node 0.12 does, I had to use the spawn-sync package, installed via NPM.

The resulting script runs in 330 msec from start to finish.

(ns trycmd.core
(:require [cljs.nodejs :as nodejs]))
(nodejs/enable-util-print!)
;; https://www.npmjs.com/package/spawn-sync. This is in Node 0.12 but
;; not 0.10.
(def ss (nodejs/require "spawn-sync"))
(defn run-sync [cmd & args]
(->> (ss cmd (clj->js args))
.-output
str))
(defn -main []
(println (run-sync "ls" "/tmp")))
(set! *main-cli-fn* -main)
$ time node run.js
,KSOutOfProcessFetcher.0.r55jifrBu08ZlGAfPLYXKgYad4c=
KSOutOfProcessFetcher.506.r55jifrBu08ZlGAfPLYXKgYad4c=
com.apple.launchd.P3pVbw5tRh
com.apple.launchd.pVDw30Lg8j
com.apple.launchd.rrlVBkqbNO
gpg-7CgQgB
gpg-KJqchT
gpg-Kshcof
gpg-LueuqB
gpg-xVuJRn
gpg-z5vv93
lein-trampoline-0EgF7A7znyHME
lein-trampoline-3nhcXlOZ2enB2
mongodb-27017.sock
mysql.sock
,
real 0m0.342s
user 0m0.285s
sys 0m0.057s
@deanrad
Copy link

deanrad commented Feb 28, 2015

I think I'm missing something- what command did you run to make that output ? Is there a way not to need Lein, like node file.cljs ?

@eigenhombre
Copy link
Author

@chicagogrooves - yes, sorry, I breezed over that part. output updated to show how it was run. (The file run.js is created by the mies-node template and loads Google Closure, a Node helper library, and the .js file compiled from your .cljs code.)

@deanrad
Copy link

deanrad commented Mar 2, 2015

lein new mies-node
Generating a project called mies-node based on the 'default' template.

This doesn't seem right to me - shouldn't it be based on 'mies-node' ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment