Skip to content

Commit

Permalink
Cache the .json file as well (1st step for load old)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbondeson committed Nov 2, 2017
1 parent 464d6cd commit a51c822
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/kii/store.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,34 @@
[kii.bindings.node.fs :as fs]
[kii.bindings.node.path :as path]
[kii.bindings.electron-renderer :refer [user-data-dir]]
))
[cuerdas.core :as str]))

(def bin-file "kiibohd.dfu.bin")
(def cache-dir "firmware-cache")

;; TODO - Parse Filename
(defn cache-firmware
[zip-file]
(let [c (chan)]
(go
(try
(let [out-dir (path/join user-data-dir cache-dir (-> zip-file path/parse :name))
(let [extract (fn [zip path] (p->chan (-> zip (.file path) (.async "nodebuffer"))) )
filename (-> zip-file path/parse :name)
[_ board layout hash] (first (re-seq #"^([A-Za-z0-9_-]+)-([A-Za-z0-9_]+)-([0-9A-Fa-f]{32})" filename))
out-dir (path/join user-data-dir cache-dir filename)
bin-out (path/join out-dir bin-file)
_ (<? (fs/mkdirp out-dir))
file (<? (fs/read-file zip-file))
zip (<? (p->chan (.loadAsync jszip file)))
data (<? (p->chan (-> zip (.file bin-file) (.async "nodebuffer"))))
]
(fs/write-file! bin-out data)
(logf :info "Successfully extracted firmware to local cache: %s" bin-out)
json-name (str/fmt "%s-%s.json" board layout)
bin-data (<? (extract zip bin-file))
json-data (<? (extract zip json-name)) ]
(fs/write-file! bin-out bin-data)
(fs/write-file! (path/join out-dir json-name) json-data)

(logf :info "Successfully extracted firmware and config to local cache: %s" bin-out)

(put! c bin-out)
)
(catch js/Error e
(logf :error e "Error extracting firmware")))
)
c)
)
(logf :error e "Error extracting firmware"))))
c))

0 comments on commit a51c822

Please sign in to comment.