 (ns cljs.repl.node
   (:require [clojure.string :as string]
             [clojure.java.io :as io]
+            [cljs.util :as util]
             [cljs.analyzer :as ana]
             [cljs.compiler :as comp]
             [cljs.repl :as repl]

      ;; properly due to how we are running it - David
      (node-eval repl-env
        (-> (slurp (io/resource "cljs/bootstrap_node.js"))
          (string/replace "__dirname"
            (str "\"" (str rewrite-path File/separator "bootstrap") "\""))
          (string/replace
            "path.join(\".\", \"..\", src)"
            (str "path.join(\"" rewrite-path "\", src)"))
          (string/replace
            "var CLJS_ROOT = \".\";"
            (str "var CLJS_ROOT = \"" (.getPath root-path) "\";"))))
      ;; load the deps file so we can goog.require cljs.core etc.
      (node-eval repl-env
        (str "require('"
          (.getPath root-path)
          File/separator "node_repl_deps.js')"))
      ;; monkey-patch isProvided_ to avoid useless warnings - David
      (node-eval repl-env
        (str "goog.isProvided_ = function(x) { return false; };"))

          proc (.start bldr)
          env  (ana/empty-env)
          core (io/resource "cljs/core.cljs")
          ;; represent paths as vectors so we can emit JS arrays, this is to
          ;; paper over Windows issues with minimum hassle - David
          path (.getPath (.getCanonicalFile output-dir))
          [fc & cs] (rest (util/path-seq path)) ;; remove leading empty string
          root (.substring path 0 (+ (.indexOf path fc) (count fc)))
          root-path (vec (cons root cs))
          rewrite-path (conj root-path "goog")]
      ;; TODO: temporary hack, should wait till we can read the start string
      ;; from the process - David
      (Thread/sleep 300)
