 
 (defn ^:export genpages [opts]
   (log "Generating site")
-  (swap! config merge (js->clj opts :keywordize-keys true))
-  (let [{:keys [site-dir pages] :as state} @config
-        conf (assoc state :timestamp (str "?" (js/Date.now)))]
+  (let [conf (swap! config merge (js->clj opts :keywordize-keys true))
+        conf (assoc conf :timestamp (str "?" (js/Date.now)))
+        {:keys [site-dir pages]} conf]
     (doseq [f pages]
       (write-file (->> f to-relative (path-join site-dir))
                   (gen-page f conf)))

(defn start! [site-config]
  (swap! config merge site-config)
  (when r/is-client
    (let [conf (when (exists? js/pageConfig)
                 (js->clj js/pageConfig :keywordize-keys true))]
      (swap! config merge conf)
      (init-history)
      (r/render-component (:body @config)
                          (js/document.getElementById (:main-div @config))))))

                               :page-conf {:page-name page-name}
                               :body-html bhtml)))))

(defn fs [] (js/require "fs"))
(defn path [] (js/require "path"))

(defn mkdirs [f]
  (let [items (as-> f _
                (.' (path) normalize _)
                (string/split _ #"/"))]
    (doseq [d (reductions #(str %1 "/" %2) items)]
      (when-not (.' (fs) existsSync d)
        (.' (fs) mkdirSync d)))))

(defn write-file [f content]
  (mkdirs (.' (path) dirname f))
  (.' (fs) writeFileSync f content))

(defn read-file [f]
  (.' (fs) readFileSync f))

(defn path-join [& paths]
  (apply (.' (path) :join) paths))

(defn read-files [files]
  (string/join "\n" (map read-file files)))
