Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Deraen committed Oct 20, 2016
0 parents commit 5757af6
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.nrepl-*
node_modules
target
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Cljsjs.npm

# NOT REALLY WORKING YET

Automatically package npm modules as jars with `deps.cljs` files.
5 changes: 5 additions & 0 deletions build.boot
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(set-env!
:dependencies '[[cheshire "5.6.3"]]
:source-paths #{"src"})

(require '[cljsjs.npm.build :refer [package]])
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "cljsjs.npm",
"version": "1.0.0",
"description": "",
"dependencies": {
"material-ui": "^0.15.4",
"react": "^15.3.1",
"react-addons-create-fragment": "^15.3.1",
"react-addons-transition-group": "^15.3.1",
"react-dom": "^15.3.1",
"react-tap-event-plugin": "^1.0.0"
}
}
90 changes: 90 additions & 0 deletions src/cljsjs/npm/build.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
(ns cljsjs.npm.build
(:require [cheshire.core :as json]
[clojure.java.io :as io]
[clojure.string :as string]
[boot.util :as util]
[boot.core :as boot]
[boot.task.built-in :as built-in]))

(defn read-json [file]
(with-open [r (io/reader file)]
(json/parse-stream r true)))

(defn parse-version [version-pattern]
(let [[_ version] (re-find #"[\^](.*)" version-pattern)]
version))

(defn copy-main [out {:keys [name main]}]
(io/copy (io/file "node_modules" name main)
(doto (io/file out "cljsjs.npm" name main)
(io/make-parents))))

(defn with-files
"Runs middleware with filtered fileset and merges the result back into complete fileset."
[p middleware]
(fn [next-handler]
(fn [fileset]
(let [merge-fileset-handler (fn [fileset']
(next-handler (boot/commit! (assoc fileset :tree (merge (:tree fileset) (:tree fileset'))))))
handler (middleware merge-fileset-handler)
fileset (assoc fileset :tree (reduce-kv
(fn [tree path x]
(if (p x)
(assoc tree path x)
tree))
(empty (:tree fileset))
(:tree fileset)))]
(handler fileset)))))

(defn package'
"A Task, but not."
[{:keys [name main]}]
(let [out (boot/tmp-dir!)]
(fn middleware [next-handler]
(fn handler [fileset]
(util/info "Package %s\n" name)
(let [files (->> (file-seq (io/file "node_modules" name))
(remove #(re-find #"^node_modules/.*/node_modules" (.getPath %)))
(remove #(re-find #"^node_modules/.*/dist" (.getPath %)))
(filter #(.endsWith (.getName %) ".js"))
(map (fn [file]
(let [path (.getPath file)
[_ package-path] (re-find #"^node_modules/[^/]*/(.*)" path)
aname (if (= main package-path)
name
(str name "/" (string/replace package-path #"\.js$" "")))
aname (string/replace aname #"/" "\\$")]
(io/copy file (doto (io/file out "cljsjs.npm" (string/replace path #"^node_modules/" "")) (io/make-parents)))
{:file (str "cljsjs.npm/" (string/replace path #"^node_modules/" ""))
:provides [aname]
:module-type :commonjs}))))]
(doto (io/file out "deps.cljs")
(io/make-parents)
(spit (pr-str {:foreign-libs (vec files)}))))
(-> fileset
(boot/add-resource out)
boot/commit!
next-handler)))))

(boot/deftask package
[]
(let [package-index (read-json (io/file "./package.json"))
packages (map name (keys (:dependencies package-index)))]
(fn middleware [next-handler]
(let [next-handler (reduce (fn [next-handler package-name]
(let [package-json (read-json (io/file "node_modules" package-name "package.json"))
middleware (with-files (constantly false)
(comp (package' package-json)
(built-in/pom :project (symbol "cljsjs.npm" package-name)
:version (:version package-json))
(built-in/jar)
(built-in/show :fileset true)))]
(middleware next-handler)))
next-handler
(reverse packages))]
(fn handler [fileset]
(next-handler fileset))))))

(comment
(read-json (io/file "node_modules" "react" "package.json"))
(boot.core/boot (comp (package) (boot.task.built-in/target))))

0 comments on commit 5757af6

Please sign in to comment.