create_graph.cljs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. (ns create-graph
  2. "An example script that creates a DB graph given a sqlite.build EDN file"
  3. (:require [logseq.outliner.cli :as outliner-cli]
  4. [clojure.string :as string]
  5. [clojure.edn :as edn]
  6. [datascript.core :as d]
  7. ["path" :as node-path]
  8. ["os" :as os]
  9. ["fs" :as fs]
  10. [nbb.classpath :as cp]
  11. [nbb.core :as nbb]))
  12. (defn- resolve-path
  13. "If relative path, resolve with $ORIGINAL_PWD"
  14. [path]
  15. (if (node-path/isAbsolute path)
  16. path
  17. (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
  18. (defn -main [args]
  19. (when (not= 2 (count args))
  20. (println "Usage: $0 GRAPH-DIR EDN-PATH")
  21. (js/process.exit 1))
  22. (let [[graph-dir edn-path] args
  23. [dir db-name] (if (string/includes? graph-dir "/")
  24. ((juxt node-path/dirname node-path/basename) graph-dir)
  25. [(node-path/join (os/homedir) "logseq" "graphs") graph-dir])
  26. sqlite-build-edn (merge {:auto-create-ontology? true}
  27. (-> (resolve-path edn-path) fs/readFileSync str edn/read-string))
  28. conn (outliner-cli/init-conn dir db-name {:classpath (cp/get-classpath)})
  29. {:keys [init-tx block-props-tx]} (outliner-cli/build-blocks-tx sqlite-build-edn)]
  30. (println "Generating" (count (filter :block/name init-tx)) "pages and"
  31. (count (filter :block/content init-tx)) "blocks ...")
  32. (d/transact! conn init-tx)
  33. (d/transact! conn block-props-tx)
  34. (println "Created graph" (str db-name "!"))))
  35. (when (= nbb/*file* (:file (meta #'-main)))
  36. (-main *command-line-args*))