db_import.cljs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. [clojure.edn :as edn]
  7. [datascript.core :as d]
  8. ["path" :as node-path]
  9. ["os" :as os]
  10. ["fs" :as fs]
  11. ["fs/promises" :as fsp]
  12. [nbb.core :as nbb]
  13. [babashka.cli :as cli]
  14. [logseq.graph-parser.exporter :as gp-exporter]
  15. [logseq.common.graph :as common-graph]
  16. [logseq.common.config :as common-config]
  17. [logseq.tasks.db-graph.create-graph :as create-graph]
  18. [promesa.core :as p]))
  19. (defn- remove-hidden-files [dir config files]
  20. (if (seq (:hidden config))
  21. (->> files
  22. (map #(assoc % ::rel-path (node-path/relative dir (:rpath %))))
  23. ((fn [files] (common-config/remove-hidden-files files config ::rel-path)))
  24. (map #(dissoc % ::rel-path)))
  25. files))
  26. (defn- build-graph-files
  27. "Given a graph directory, return absolute, allowed file paths and their contents in preparation
  28. for parsing"
  29. [dir* config]
  30. (let [dir (node-path/resolve dir*)]
  31. (->> (common-graph/get-files dir)
  32. (mapv #(hash-map :rpath %))
  33. (remove-hidden-files dir config))))
  34. (defn- read-config
  35. "Reads repo-specific config from logseq/config.edn"
  36. [dir]
  37. (let [config-file (str dir "/" common-config/app-name "/config.edn")]
  38. (if (fs/existsSync config-file)
  39. (-> config-file fs/readFileSync str edn/read-string)
  40. {})))
  41. (defn- import-file-graph-to-db [file-graph-dir conn user-options]
  42. (let [config (read-config file-graph-dir)
  43. import-options (gp-exporter/setup-import-options
  44. @conn
  45. config
  46. user-options
  47. {:notify-user prn})
  48. ;; TODO: Remove logseq/ filter when higher-level import fn is available
  49. files (remove #(re-find #"logseq/" (:rpath %)) (build-graph-files file-graph-dir config))]
  50. ;; (prn :files (count files) files)
  51. (gp-exporter/import-from-doc-files!
  52. conn files #(p/let [s (fsp/readFile (:rpath %))] (str s)) import-options)))
  53. (def spec
  54. "Options spec"
  55. {:help {:alias :h
  56. :desc "Print help"}
  57. :verbose {:alias :v
  58. :desc "Verbose mode"}
  59. :tag-classes {:alias :t
  60. :coerce []
  61. :desc "List of tags to convert to classes"}
  62. :property-classes {:alias :p
  63. :coerce []
  64. :desc "List of properties whose values convert to classes"}})
  65. (defn -main [args]
  66. (let [[file-graph db-graph-dir] args
  67. options (cli/parse-opts args {:spec spec})
  68. _ (when (or (< (count args) 2) (:help options))
  69. (println (str "Usage: $0 FILE-GRAPH DB-GRAPH [OPTIONS]\nOptions:\n"
  70. (cli/format-opts {:spec spec})))
  71. (js/process.exit 1))
  72. [dir db-name] (if (string/includes? db-graph-dir "/")
  73. (let [graph-dir'
  74. (node-path/join (or js/process.env.ORIGINAL_PWD ".") db-graph-dir)]
  75. ((juxt node-path/dirname node-path/basename) graph-dir'))
  76. [(node-path/join (os/homedir) "logseq" "graphs") db-graph-dir])
  77. file-graph' (node-path/join (or js/process.env.ORIGINAL_PWD ".") file-graph)
  78. conn (create-graph/init-conn dir db-name)]
  79. (p/do!
  80. (import-file-graph-to-db file-graph' conn (merge options {:graph-name db-name}))
  81. (when (:verbose options) (println "Transacted" (count (d/datoms @conn :eavt)) "datoms"))
  82. (println "Created graph" (str db-name "!")))))
  83. (when (= nbb/*file* (:file (meta #'-main)))
  84. (-main *command-line-args*))