db_import.cljs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. (ns db-import
  2. "Imports given file(s) to a db graph. This script is primarily for
  3. developing the import feature and for engineers who want to customize
  4. the import process"
  5. (:require [clojure.string :as string]
  6. [datascript.core :as d]
  7. ["path" :as node-path]
  8. ["os" :as os]
  9. ["fs" :as fs]
  10. ["fs/promises" :as fsp]
  11. [nbb.core :as nbb]
  12. [babashka.cli :as cli]
  13. [logseq.graph-parser.exporter :as gp-exporter]
  14. [logseq.common.graph :as common-graph]
  15. [logseq.common.config :as common-config]
  16. [logseq.tasks.db-graph.create-graph :as create-graph]
  17. [promesa.core :as p]))
  18. (defn- remove-hidden-files [dir config files]
  19. (if (seq (:hidden config))
  20. (->> files
  21. (map #(assoc % ::rel-path (node-path/relative dir (:rpath %))))
  22. ((fn [files] (common-config/remove-hidden-files files config ::rel-path)))
  23. (map #(dissoc % ::rel-path)))
  24. files))
  25. (defn- build-graph-files
  26. "Given a graph directory, return absolute, allowed file paths and their contents in preparation
  27. for parsing"
  28. [dir*]
  29. (let [dir (node-path/resolve dir*)]
  30. (->> (common-graph/get-files dir)
  31. (mapv #(hash-map :rpath %)))))
  32. (defn- <read-file
  33. [file]
  34. (p/let [s (fsp/readFile (:rpath file))]
  35. (str s)))
  36. (defn- build-import-options [conn config options]
  37. (-> (gp-exporter/setup-import-options
  38. @conn
  39. {}
  40. (select-keys options [:tag-classes :property-classes])
  41. {:notify-user prn :macros (:macros config)})
  42. (assoc-in [:extract-options :verbose] (:verbose options))))
  43. (defn- import-file-graph-to-db [file-graph-dir conn options]
  44. (p/let [*files (build-graph-files file-graph-dir)
  45. config-file (first (filter #(string/ends-with? (:rpath %) "logseq/config.edn") *files))
  46. _ (assert config-file "No 'logseq/config.edn' found for file graph dir")
  47. ;; TODO: Add :default-config option
  48. config (gp-exporter/import-config-file! conn config-file <read-file {:notify-user prn})
  49. files (remove-hidden-files file-graph-dir config *files)
  50. import-options (build-import-options conn config options)
  51. logseq-file? #(string/includes? (:rpath %) "logseq/")
  52. doc-files (remove logseq-file? files)
  53. logseq-files (filter logseq-file? files)]
  54. (println "Importing" (count files) "files ...")
  55. (p/do!
  56. (gp-exporter/import-logseq-files conn logseq-files <read-file {:notify-user prn})
  57. (gp-exporter/import-from-doc-files! conn doc-files <read-file import-options))))
  58. (defn- resolve-path
  59. "If relative path, resolve with $ORIGINAL_PWD"
  60. [path]
  61. (if (node-path/isAbsolute path)
  62. path
  63. (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
  64. (defn- import-files-to-db [file conn {:keys [files] :as options}]
  65. (let [import-options (build-import-options conn {:macros {}} options)
  66. files' (mapv #(hash-map :rpath %)
  67. (into [file] (map resolve-path files)))]
  68. (gp-exporter/import-from-doc-files! conn files' <read-file import-options)))
  69. (def spec
  70. "Options spec"
  71. {:help {:alias :h
  72. :desc "Print help"}
  73. :verbose {:alias :v
  74. :desc "Verbose mode"}
  75. :tag-classes {:alias :t
  76. :coerce []
  77. :desc "List of tags to convert to classes"}
  78. :files {:alias :f
  79. :coerce []
  80. :desc "Additional files to import"}
  81. :property-classes {:alias :p
  82. :coerce []
  83. :desc "List of properties whose values convert to classes"}})
  84. (defn -main [args]
  85. (let [[file-graph db-graph-dir] args
  86. options (cli/parse-opts args {:spec spec})
  87. _ (when (or (< (count args) 2) (:help options))
  88. (println (str "Usage: $0 FILE-GRAPH DB-GRAPH [OPTIONS]\nOptions:\n"
  89. (cli/format-opts {:spec spec})))
  90. (js/process.exit 1))
  91. [dir db-name] (if (string/includes? db-graph-dir "/")
  92. (let [graph-dir' (resolve-path db-graph-dir)]
  93. ((juxt node-path/dirname node-path/basename) graph-dir'))
  94. [(node-path/join (os/homedir) "logseq" "graphs") db-graph-dir])
  95. file-graph' (resolve-path file-graph)
  96. conn (create-graph/init-conn dir db-name)
  97. directory? (.isDirectory (fs/statSync file-graph'))]
  98. (p/do!
  99. (if directory?
  100. (import-file-graph-to-db file-graph' conn (merge options {:graph-name db-name}))
  101. (import-files-to-db file-graph' conn (merge options {:graph-name db-name})))
  102. (when (:verbose options) (println "Transacted" (count (d/datoms @conn :eavt)) "datoms"))
  103. (println "Created graph" (str db-name "!")))))
  104. (when (= nbb/*file* (:file (meta #'-main)))
  105. (-main *command-line-args*))