publishing.cljs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. (ns publishing
  2. "Basic script for publishing from CLI"
  3. (:require ["fs" :as fs]
  4. ["path" :as node-path]
  5. [clojure.edn :as edn]
  6. [datascript.core :as d]
  7. [logseq.db.common.sqlite-cli :as sqlite-cli]
  8. [logseq.db.sqlite.util :as sqlite-util]
  9. [logseq.graph-parser.cli :as gp-cli]
  10. [logseq.publishing :as publishing]
  11. [nbb.core :as nbb]))
  12. (defn- get-db [graph-dir]
  13. (let [{:keys [conn]} (gp-cli/parse-graph graph-dir {:verbose false})] @conn))
  14. (defn- publish-file-graph [static-dir graph-dir output-path options]
  15. (let [repo-config (-> (node-path/join graph-dir "logseq" "config.edn") fs/readFileSync str edn/read-string)]
  16. (publishing/export (get-db graph-dir)
  17. static-dir
  18. graph-dir
  19. output-path
  20. (merge options {:repo (node-path/basename graph-dir)
  21. :repo-config repo-config
  22. :ui/theme "dark"
  23. :ui/radix-color :purple}))))
  24. (defn- publish-db-graph [static-dir graph-dir output-path opts]
  25. (let [db-name (node-path/basename graph-dir)
  26. conn (sqlite-cli/open-db! (node-path/dirname graph-dir) db-name)
  27. repo-config (-> (d/q '[:find ?content
  28. :where [?b :file/path "logseq/config.edn"] [?b :file/content ?content]]
  29. @conn)
  30. ffirst
  31. edn/read-string)]
  32. (publishing/export @conn
  33. static-dir
  34. graph-dir
  35. output-path
  36. (merge opts {:repo (str sqlite-util/db-version-prefix db-name)
  37. :repo-config repo-config
  38. :db-graph? true
  39. :ui/theme "dark"
  40. :ui/radix-color :cyan}))))
  41. (defn- resolve-path
  42. "If relative path, resolve with $ORIGINAL_PWD"
  43. [path]
  44. (if (node-path/isAbsolute path)
  45. path
  46. (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
  47. (defn -main
  48. [args]
  49. (when (< (count args) 3)
  50. (println "Usage: $0 STATIC-DIR GRAPH-DIR OUT-DIR")
  51. (js/process.exit 1))
  52. (let [[static-dir graph-dir output-path] (map resolve-path args)
  53. options {:dev? (contains? (set args) "--dev")}]
  54. (if (sqlite-cli/db-graph-directory? graph-dir)
  55. (publish-db-graph static-dir graph-dir output-path options)
  56. (publish-file-graph static-dir graph-dir output-path options))))
  57. (when (= nbb/*file* (nbb/invoked-file))
  58. (-main *command-line-args*))