Skip to content

Commit

Permalink
Add the :working-directory option to the options map to specify the w…
Browse files Browse the repository at this point in the history
…orking directory for Node.js
  • Loading branch information
eploko committed Dec 2, 2015
1 parent 91fdf9a commit e0a0775
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ request to `localhost:3000`. You can define both of this by passing `:default-aj

You want them to point to where the Clojure server is running. In many cases for example, the port will be random.

Also, you may want to specify the working directory for the Node.js process like this:

```clojure
(reset! js-engine (prerenderer/run {:path "js/server-side.js"
:working-directory "target"})))
```

After that, prerendering happens by simply doing:

```clojure
Expand Down Expand Up @@ -302,6 +309,7 @@ to your uberjar profile. You can get some background about this issue in
## Changelog

### v0.2.0
- Added an option to specify the working directory for the JavaScript engine.
- Added Function to stop JavaScript engine.
- Renamed run to start! to match stop!
- Added option :noop-when-stopped that will make prerenderer just issue a warning when the JavaScript engine is not
Expand Down
31 changes: 19 additions & 12 deletions src/clj/prerenderer/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@
(defn ensure-javascript-exists
([js-engine] (ensure-javascript-exists js-engine false))
([js-engine notify-about-file-appearing]
(if (not (.exists (io/as-file (:path js-engine))))
(let [message (str "File " (:path js-engine) " for prerendering is not present. Did you compile your JavaScript? 'lein cljsbuild auto' maybe?")]
(if (:wait js-engine)
(do
(println message "Waiting until it appears...")
(Thread/sleep 100)
(recur js-engine true))
(throw (Exception. message))))
(when notify-about-file-appearing
(println "File" (:path js-engine) "appeared. Pfiuuu!")))))
(let [path (:path js-engine)
working-directory (:working-directory js-engine ".")
full-path (.getAbsolutePath (java.io.File. working-directory path))]
(if-not (.exists (io/as-file full-path))
(let [message (str "File " full-path " for prerendering is not present. Did you compile your JavaScript? 'lein cljsbuild auto' maybe?")]
(if (:wait js-engine)
(do
(println message "Waiting until it appears...")
(Thread/sleep 100)
(recur js-engine true))
(throw (Exception. message))))
(when notify-about-file-appearing
(println "File" full-path "appeared. Pfiuuu!"))))))

(defn read-port-file [js-engine]
(slurp (:port-file js-engine)))
Expand Down Expand Up @@ -59,7 +62,8 @@
"--port-file" (:port-file js-engine)
"--default-ajax-host" (:default-ajax-host js-engine)
"--default-ajax-port" (str (:default-ajax-port js-engine))])
.inheritIO))
.inheritIO
(.directory (io/file (:working-directory js-engine)))))

(defn start! [options]
(let [js-engine (merge {:path nil
Expand All @@ -70,10 +74,13 @@
.deleteOnExit))
:start-timeout 5000
:wait false
:noop-when-stopped false}
:noop-when-stopped false
:working-directory "."}
options)]
(if (nil? (:path js-engine))
(throw (Exception. "Path should be specified when starting a pre-rendering engine.")))
(if-not (.exists (io/as-file (:working-directory js-engine)))
(throw (Exception. "Working directory should exist when starting a pre-rendering engine.")))
(ensure-javascript-exists js-engine)
(blank-port-file! js-engine)
(let [process (.start (create-process-builder js-engine))
Expand Down

0 comments on commit e0a0775

Please sign in to comment.