Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(ns build
(:require [clojure.tools.build.api :as b]
[shadow.cljs.devtools.api :as shadow.api]
[shadow.cljs.devtools.server :as shadow.server]
[cheshire.core :as json]
[clojure.java.io :as io]
[clojure.string :as string])
(:import (org.apache.commons.compress.archivers ArchiveStreamFactory)
(java.util.zip GZIPInputStream CheckedInputStream Adler32)
(java.security MessageDigest DigestInputStream)
(java.util Formatter)))
(defn ^MessageDigest message-digest-instance
[integrity]
(MessageDigest/getInstance (first (string/split integrity #"-" 2))))
(defn md-integrity
[^MessageDigest md]
(let [formatter (Formatter.)
hash (.digest md)]
(doseq [i hash]
(.format formatter "%02x" (into-array [i])))
(str formatter)))
(set! *warn-on-reflection* true)
(defn x []
(let [{:strs [packages]} (json/parse-stream (io/reader "package-lock.json"))
packages (remove #(get % "dev")
(dissoc packages ""))]
(doseq [[target {:strs [resolved integrity]}] packages
:let [targetf (io/file target)
md (message-digest-instance integrity)
in (DigestInputStream. (io/input-stream resolved) md)
tar (.createArchiveInputStream (ArchiveStreamFactory.) "tar"
(GZIPInputStream. in))]]
(loop []
(when-let [tar-entry (some-> tar .getNextEntry)]
(let [entry-target (io/file targetf (string/replace (.getName tar-entry)
#"^package/"
""))]
(io/make-parents entry-target)
(io/copy tar entry-target)
(recur))))
(prn [integrity (md-integrity md)]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment