diff --git a/README.md b/README.md index dbc3132..525f13c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/clj/prerenderer/core.clj b/src/clj/prerenderer/core.clj index f4ebd07..e800a16 100644 --- a/src/clj/prerenderer/core.clj +++ b/src/clj/prerenderer/core.clj @@ -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))) @@ -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 @@ -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))