db_import.cljs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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- <copy-asset-file [file db-graph-dir file-graph-dir]
  44. (p/let [parent-dir (node-path/dirname
  45. (node-path/join db-graph-dir (node-path/relative file-graph-dir (:rpath file))))
  46. _ (fsp/mkdir parent-dir #js {:recursive true})]
  47. (fsp/copyFile (:rpath file) (node-path/join parent-dir (node-path/basename (:rpath file))))))
  48. (defn- import-file-graph-to-db [file-graph-dir db-graph-dir conn options]
  49. (p/let [*files (build-graph-files file-graph-dir)
  50. config-file (first (filter #(string/ends-with? (:rpath %) "logseq/config.edn") *files))
  51. _ (assert config-file "No 'logseq/config.edn' found for file graph dir")
  52. ;; TODO: Add :default-config option
  53. config (gp-exporter/import-config-file! conn config-file <read-file {:notify-user prn})
  54. files (remove-hidden-files file-graph-dir config *files)
  55. import-options (build-import-options conn config options)
  56. logseq-file? #(string/includes? (:rpath %) "logseq/")
  57. asset-files (when (fs/existsSync (node-path/join file-graph-dir "assets"))
  58. (map #(hash-map :rpath %) (common-graph/readdir (node-path/join file-graph-dir "assets"))))
  59. doc-files (remove logseq-file? files)
  60. logseq-files (filter logseq-file? files)]
  61. (println "Importing" (count files) "files ...")
  62. (p/do!
  63. (gp-exporter/import-logseq-files conn logseq-files <read-file {:notify-user prn})
  64. (gp-exporter/import-from-asset-files! asset-files #(<copy-asset-file % db-graph-dir file-graph-dir) {:notify-user prn})
  65. (gp-exporter/import-from-doc-files! conn doc-files <read-file import-options)
  66. (gp-exporter/import-class-properties conn conn))))
  67. (defn- resolve-path
  68. "If relative path, resolve with $ORIGINAL_PWD"
  69. [path]
  70. (if (node-path/isAbsolute path)
  71. path
  72. (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
  73. (defn- import-files-to-db [file conn {:keys [files] :as options}]
  74. (let [import-options (build-import-options conn {:macros {}} options)
  75. files' (mapv #(hash-map :rpath %)
  76. (into [file] (map resolve-path files)))]
  77. (gp-exporter/import-from-doc-files! conn files' <read-file import-options)))
  78. (def spec
  79. "Options spec"
  80. {:help {:alias :h
  81. :desc "Print help"}
  82. :verbose {:alias :v
  83. :desc "Verbose mode"}
  84. :tag-classes {:alias :t
  85. :coerce []
  86. :desc "List of tags to convert to classes"}
  87. :files {:alias :f
  88. :coerce []
  89. :desc "Additional files to import"}
  90. :property-classes {:alias :p
  91. :coerce []
  92. :desc "List of properties whose values convert to classes"}})
  93. (defn -main [args]
  94. (let [[file-graph db-graph-dir] args
  95. options (cli/parse-opts args {:spec spec})
  96. _ (when (or (< (count args) 2) (:help options))
  97. (println (str "Usage: $0 FILE-GRAPH DB-GRAPH [OPTIONS]\nOptions:\n"
  98. (cli/format-opts {:spec spec})))
  99. (js/process.exit 1))
  100. [dir db-name] (if (string/includes? db-graph-dir "/")
  101. (let [graph-dir' (resolve-path db-graph-dir)]
  102. ((juxt node-path/dirname node-path/basename) graph-dir'))
  103. [(node-path/join (os/homedir) "logseq" "graphs") db-graph-dir])
  104. file-graph' (resolve-path file-graph)
  105. conn (create-graph/init-conn dir db-name)
  106. directory? (.isDirectory (fs/statSync file-graph'))]
  107. (p/do!
  108. (if directory?
  109. (import-file-graph-to-db file-graph' (node-path/join dir db-name) conn (merge options {:graph-name db-name}))
  110. (import-files-to-db file-graph' conn (merge options {:graph-name db-name})))
  111. (when (:verbose options) (println "Transacted" (count (d/datoms @conn :eavt)) "datoms"))
  112. (println "Created graph" (str db-name "!")))))
  113. (when (= nbb/*file* (:file (meta #'-main)))
  114. (-main *command-line-args*))