create_graph.cljs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. (ns create-graph
  2. "A script that creates a DB graph given a sqlite.build EDN file"
  3. (:require ["fs" :as fs]
  4. ["os" :as os]
  5. ["path" :as node-path]
  6. #_:clj-kondo/ignore
  7. [babashka.cli :as cli]
  8. [clojure.edn :as edn]
  9. [clojure.string :as string]
  10. [datascript.core :as d]
  11. [logseq.outliner.cli :as outliner-cli]
  12. [nbb.classpath :as cp]
  13. [nbb.core :as nbb]
  14. [validate-db]))
  15. (defn- resolve-path
  16. "If relative path, resolve with $ORIGINAL_PWD"
  17. [path]
  18. (if (node-path/isAbsolute path)
  19. path
  20. (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
  21. (def spec
  22. "Options spec"
  23. {:help {:alias :h
  24. :desc "Print help"}
  25. :validate {:alias :v
  26. :desc "Validate db after creation"}})
  27. (defn -main [args]
  28. (let [{options :opts args' :args} (cli/parse-args args {:spec spec})
  29. [graph-dir edn-path] args'
  30. _ (when (or (nil? graph-dir) (nil? edn-path) (:help options))
  31. (println (str "Usage: $0 GRAPH-NAME EDN-PATH [OPTIONS]\nOptions:\n"
  32. (cli/format-opts {:spec spec})))
  33. (js/process.exit 1))
  34. [dir db-name] (if (string/includes? graph-dir "/")
  35. ((juxt node-path/dirname node-path/basename) graph-dir)
  36. [(node-path/join (os/homedir) "logseq" "graphs") graph-dir])
  37. sqlite-build-edn (merge {:auto-create-ontology? true}
  38. (-> (resolve-path edn-path) fs/readFileSync str edn/read-string))
  39. conn (outliner-cli/init-conn dir db-name {:classpath (cp/get-classpath) :import-type :cli/create-graph})
  40. {:keys [init-tx block-props-tx] :as _txs} (outliner-cli/build-blocks-tx sqlite-build-edn)]
  41. (println "Generating" (count (filter :block/name init-tx)) "pages and"
  42. (count (filter :block/title init-tx)) "blocks ...")
  43. ;; (cljs.pprint/pprint _txs)
  44. (d/transact! conn init-tx)
  45. (d/transact! conn block-props-tx)
  46. (println "Created graph" (str db-name "!"))
  47. (when (:validate options)
  48. (validate-db/validate-db @conn db-name {:group-errors true :closed-maps true :humanize true}))))
  49. (when (= nbb/*file* (nbb/invoked-file))
  50. (-main *command-line-args*))