curator.clj 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env bb
  2. (require '[clojure.java.io :as io]
  3. '[clojure.string :as string]
  4. '[clojure.pprint :refer [pprint]])
  5. (def exceptions
  6. #{"index.html"
  7. "styles.css"
  8. "js"
  9. "clapping.gif"
  10. "go.png"
  11. "clapping.png"
  12. "vendor"})
  13. (let [foldername "./resources/public"
  14. folder (io/file foldername)
  15. existing-filename "./library.edn"
  16. existing (try
  17. (-> existing-filename
  18. (slurp)
  19. (read-string))
  20. (catch Exception _ []))
  21. existing-files (->> existing
  22. (map :image)
  23. (set))
  24. highest-index (or (some->> existing
  25. (map :id)
  26. (not-empty)
  27. (apply max)
  28. (inc))
  29. 1)]
  30. (if (.isDirectory folder)
  31. (->> (.list folder)
  32. (remove exceptions)
  33. (remove existing-files)
  34. (map-indexed (fn [ind file]
  35. {:id (+ highest-index ind)
  36. :name (-> file
  37. (string/replace #"\.[^\.]*$" "")
  38. (string/replace #"[-\._]" " "))
  39. :image file}))
  40. (concat existing)
  41. (vec)
  42. ((fn [new-data]
  43. (with-open [writer (io/writer existing-filename)]
  44. (pprint new-data writer))
  45. (println "wrote to" existing-filename))))
  46. (println "Folder not found. Make sure to run this script from its containing folder.")))