 
 (defn stop-autobuild [system ids]
   (repl-println "Figwheel: Stoping autobuild")
-  (swap! system component/stop-system (ids-or-all-build-keys system ids)))
+  (swap! system component/stop-system (ids-or-all-build-keys system ids))
+  nil)
 
 (defn start-autobuild [system ids]
   (repl-println "Figwheel: Starting autobuild")
   (when (not-empty ids)
     (patch-system-builds system ids))
-  (swap! system component/start-system (ids-or-all-build-keys system ids)))
+  (swap! system component/start-system (ids-or-all-build-keys system ids))
+  nil)
 
 (defn switch-to-build [system ids]
   (when (not-empty ids)
-    (start-autobuild system ids)))
+    (start-autobuild system ids))
+  nil)
 
 (defn clean-build [system id]
   (let [{:keys [builds]} @system]
     (when-let [{:keys [build-options]} (get builds (name id))]
       (repl-println "Figwheel: Cleaning build -" id)
-      (clean-cljs-build* build-options))))
+      (clean-cljs-build* build-options)))
+  nil)
 
 (defn clean-builds [system ids]
   (let [ids (map key->id (ids-or-all-build-keys system ids))]
     (doseq [id ids]
-      (clean-build system id))))
+      (clean-build system id)))
+  nil)
 
 (defn build-once* [system id]
   (when-let [build-config (get (:builds @system) (name id))]


(defn build-once [system ids]
  (doseq [ky (ids-or-all-build-keys system ids)]
    (build-once* system (key->id ky))))

(defn reset-autobuild [system]
  (swap! system component/stop-system (all-build-keys system))
  (clean-builds system nil)
  (swap! system component/start-system (all-build-keys system)))

(defn reload-config [system]
  (swap! system component/stop-system (all-build-keys system))
  (clean-builds system nil)
  (repl-println "Figwheel: Reloading build config information")
  (swap! system assoc :builds (get-project-builds))
  (swap! system component/start-system (all-build-keys system)))

(defn watchers-running [system]
  (filter (fn [key] (get-in @system [key :file-watcher]))

;; nrepl component only really useful in a development env
;; considering making it idempotent so that it's only launched once

(defn require? [symbol]
  (try (require symbol) true (catch Exception e false)))

(defn start-nrepl-server [figwheel-options autobuild-options]
  (when (:nrepl-port figwheel-options)
    (let [middleware (or
                      (:nrepl-middleware figwheel-options)
                      ["cemerick.piggieback/wrap-cljs-repl"])
          resolve-mw (fn [name]
                       (let [s (symbol name)
                             ns (symbol (namespace s))]
                         (if (and
                              (require? ns)
                              (resolve s))
                           (let [var (resolve s)
                                 val (deref var)]
                             (if (vector? val)
                               (map resolve val)
                               (list var)))
                           (println (format "WARNING: unable to load \"%s\" middleware" name)))))
          middleware (mapcat resolve-mw middleware)]
      (nrepl-serv/start-server
       :port (:nrepl-port figwheel-options)
       :bind (:nrepl-host figwheel-options)
       :handler (apply nrepl-serv/default-handler middleware)))))

(defrecord NreplComponent []
  component/Lifecycle
  (start [this]
    (if (not (:running-nrepl-server this))
      (do
        (println "Figwheel: Starting nREPL server on port:" (:nrepl-port this))
        (assoc this :running-nrepl-server (start-nrepl-server this nil)))
      (do
        (println "Figwheel: nREPL server already running")
        this)))
