create_graph.cljs 3.3 KB

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