          (recur newout ndeps))
        output))))
 
+(defn conn-response-handler
+  [res]
+  (println (str "status: " (. res -statusCode)))
+  (println (str "headers: " (js->clj (. res -headers)))))
+
+(defn conn-error-handler
+  [e]
+  (if (= (. e -errno) (. js/process -ECONNREFUSED))
+    (println (str "ECONNREFUSED: connection refused to "
+                  post-host
+                  ":"
+                  post-port))
+
+    (println "blargl " (js->clj e))))
+
 (defn post-deps
   [deps]
   (let [json (json-generate deps)
-        [product_id token] (get-product-and-token)
-        request (.request https (post-options product_id token json))]
+        options (post-options json)
+        request (.request https options conn-response-handler)]
+
     (.write request json)
     (.end request)))
 

  [_ npm]
  (.ls (. js/npm -commands) [] true npm-ls-callback))


(set! *main-cli-fn* #(.load npm load-npm-callback))

;;(set! *main-cli-fn* print-deps)

;; (def http
;;   (node/require "http"))

;; (defn handler
;;   [_ res]
;;   (.writeHead res 200 (str {"Content-Type" "text/plain"}))
;;   (.end res "Hello World!\n"))

;; (defn start
;;   [& _]
;;   (let [server (.createServer http handler)]
;;     (.listen server 8001 "127.0.0.1")
;;     (println "Server running at http://127.0.0.1:8001/")))

;; var http = require('http');
;; var fs = require('fs');

;; var server = http.createServer(function (req, res) {
;;   console.log("blargl");
;;   res.writeHead(200, { "Content-Type": "text/plain" });
;;   res.end(" " + fs.readdirSync(process.cwd()));
;; });


;; var npm = require('npm');

;; npm.load(function(err, npm) {
;;     npm.commands.ls([], true, function(err, data, lite) {
;;         console.log(data); //or lite for simplified output
;;     });
;; });

;; server.listen(process.env.PORT || 8001);
 (ns sourceninja-node.core
  (:require  [clojure.set :as set]
             [cljs.nodejs :as node]))

(def env
  (. js/process -env))

(def npm
  (node/require "npm"))

(def https
  (if (. env -SOURCENINJA_HTTP)
    (node/require "http")
    (node/require "https")))

(def sn-post-port
  (if-let [port (. env -SOURCENINJA_PORT)]
    (js/parseInt port)
    443))

(def sn-post-host
  (if-let [hostname (. env -SOURCENINJA_HOSTNAME)]
    hostname
    "app.sourceninja.com"))

(def sn-product-id
  (. env -SOURCENINJA_PRODUCT_ID))

(def sn-product-token
  (. env -SOURCENINJA_PRODUCT_TOKEN))

(def sn-post-url
  (str "/products/" sn-product-id "/imports?import_type=node&token=" sn-product-token))

(defn make-js-map
  "makes a javascript map from a clojure one"
  [cljmap]
  (let [out (js-obj)]
    (doall (map #(aset out (name (first %)) (second %)) cljmap))
    out))

(defn create-headers
  "create a header hash for the given content string"
  [content]
  {"Content-Type" "application/json"
   "Content-Length" (. content -length)})

(defn post-options
  [json]
  (make-js-map
   {:hostname sn-post-host
    :port sn-post-port
    :path sn-post-url
    :method "POST"
    :headers (make-js-map (create-headers json))}))

(defn json-generate
  [data]
  "not good for general purpose, can't find a good general clj->js function, only works for maps"
  (str (JSON/stringify (map make-js-map data)) "\n"))

(defn extract-name-and-version
  "Take the intermediate output and create a set of
   hashes. Each hash contains a name and a version"
  [input]
  (for [[name data] input]
    {"name" name
     "version" (get data "version")}))

(defn flatten-deps
  "Consume the graph that we get from NPM and spit out a hash of hashes.
