create_graph.cljs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.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. conn (outliner-cli/init-conn dir db-name {:classpath (cp/get-classpath) :import-type :cli/create-graph})
  51. {:keys [init-tx block-props-tx misc-tx] :as _txs}
  52. (if (:import options)
  53. (sqlite-export/build-import sqlite-build-edn @conn {})
  54. (outliner-cli/build-blocks-tx sqlite-build-edn))]
  55. (println "Generating" (count (filter :block/name init-tx)) "pages and"
  56. (count (filter :block/title init-tx)) "blocks ...")
  57. ;; (cljs.pprint/pprint _txs)
  58. (d/transact! conn init-tx)
  59. (when (seq block-props-tx) (d/transact! conn block-props-tx))
  60. (when (seq misc-tx) (d/transact! conn misc-tx))
  61. (println "Created graph" (str db-name "!"))
  62. (when (:validate options)
  63. (validate-db/validate-db @conn db-name {:group-errors true :closed-maps true :humanize true}))))
  64. (when (= nbb/*file* (nbb/invoked-file))
  65. (-main *command-line-args*))