create_graph.cljs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. (ns create-graph
  2. "A script that creates or updates a DB graph given a sqlite.build EDN file.
  3. If the given graph already exists, the EDN file updates the graph."
  4. (:require ["fs" :as fs]
  5. ["os" :as os]
  6. ["path" :as node-path]
  7. [babashka.cli :as cli]
  8. [clojure.edn :as edn]
  9. [clojure.string :as string]
  10. [datascript.core :as d]
  11. [logseq.db.sqlite.export :as sqlite-export]
  12. [logseq.outliner.cli :as outliner-cli]
  13. [nbb.classpath :as cp]
  14. [nbb.core :as nbb]
  15. [validate-db]))
  16. (defn- resolve-path
  17. "If relative path, resolve with $ORIGINAL_PWD"
  18. [path]
  19. (if (node-path/isAbsolute path)
  20. path
  21. (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
  22. (defn- get-dir-and-db-name
  23. "Gets dir and db name for use with open-db! Works for relative and absolute paths and
  24. defaults to ~/logseq/graphs/ when no '/' present in name"
  25. [graph-dir]
  26. (if (string/includes? graph-dir "/")
  27. (let [resolve-path' #(if (node-path/isAbsolute %) %
  28. ;; $ORIGINAL_PWD used by bb tasks to correct current dir
  29. (node-path/join (or js/process.env.ORIGINAL_PWD ".") %))]
  30. ((juxt node-path/dirname node-path/basename) (resolve-path' graph-dir)))
  31. [(node-path/join (os/homedir) "logseq" "graphs") graph-dir]))
  32. (def spec
  33. "Options spec"
  34. {:help {:alias :h
  35. :desc "Print help"}
  36. :validate {:alias :v
  37. :desc "Validate db after creation"}
  38. :import {:alias :i
  39. :desc "Import edn file using sqlite-export"}})
  40. (defn -main [args]
  41. (let [{options :opts args' :args} (cli/parse-args args {:spec spec})
  42. [graph-dir edn-path] args'
  43. _ (when (or (nil? graph-dir) (nil? edn-path) (:help options))
  44. (println (str "Usage: $0 GRAPH-NAME EDN-PATH [OPTIONS]\nOptions:\n"
  45. (cli/format-opts {:spec spec})))
  46. (js/process.exit 1))
  47. [dir db-name] (get-dir-and-db-name graph-dir)
  48. sqlite-build-edn (merge (if (:import options) {} {:auto-create-ontology? true})
  49. (-> (resolve-path edn-path) fs/readFileSync str edn/read-string))
  50. graph-exists? (fs/existsSync (node-path/join dir db-name))
  51. conn (outliner-cli/init-conn dir db-name {:classpath (cp/get-classpath) :import-type :cli/create-graph})
  52. {:keys [init-tx block-props-tx misc-tx] :as _txs}
  53. (if (:import options)
  54. (sqlite-export/build-import sqlite-build-edn @conn {})
  55. (outliner-cli/build-blocks-tx sqlite-build-edn))]
  56. (println "Generating" (count (filter :block/name init-tx)) "pages and"
  57. (count (filter :block/title init-tx)) "blocks ...")
  58. ;; (fs/writeFileSync "txs.edn" (with-out-str (cljs.pprint/pprint _txs)))
  59. ;; (cljs.pprint/pprint _txs)
  60. (d/transact! conn init-tx)
  61. (when (seq block-props-tx) (d/transact! conn block-props-tx))
  62. (when (seq misc-tx) (d/transact! conn misc-tx))
  63. (println (if graph-exists? "Updated graph" "Created graph") (str db-name "!"))
  64. (when (:validate options)
  65. (validate-db/validate-db @conn db-name {:group-errors true :closed-maps true :humanize true}))))
  66. (when (= nbb/*file* (nbb/invoked-file))
  67. (-main *command-line-args*))