db_import.cljs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. [nbb.classpath :as cp]
  13. [babashka.cli :as cli]
  14. [logseq.graph-parser.exporter :as gp-exporter]
  15. [logseq.common.graph :as common-graph]
  16. #_:clj-kondo/ignore
  17. [logseq.outliner.cli :as outliner-cli]
  18. [promesa.core :as p]))
  19. (defn- build-graph-files
  20. "Given a file graph directory, return all files including assets and adds relative paths
  21. on ::rpath since paths are absolute by default and exporter needs relative paths for
  22. some operations"
  23. [dir*]
  24. (let [dir (node-path/resolve dir*)]
  25. (->> (common-graph/get-files dir)
  26. (concat (when (fs/existsSync (node-path/join dir* "assets"))
  27. (common-graph/readdir (node-path/join dir* "assets"))))
  28. (mapv #(hash-map :path %
  29. ::rpath (node-path/relative dir* %))))))
  30. (defn- <read-file
  31. [file]
  32. (p/let [s (fsp/readFile (:path file))]
  33. (str s)))
  34. (defn- <copy-asset-file [file db-graph-dir file-graph-dir]
  35. (p/let [parent-dir (node-path/dirname
  36. (node-path/join db-graph-dir (node-path/relative file-graph-dir (:path file))))
  37. _ (fsp/mkdir parent-dir #js {:recursive true})]
  38. (fsp/copyFile (:path file) (node-path/join parent-dir (node-path/basename (:path file))))))
  39. (defn- notify-user [m]
  40. (println (:msg m))
  41. (when (:ex-data m)
  42. (println "Ex-data:" (pr-str (dissoc (:ex-data m) :error)))
  43. (println "Stacktrace:")
  44. (if-let [stack (some-> (get-in m [:ex-data :error]) ex-data :sci.impl/callstack deref)]
  45. (println (string/join
  46. "\n"
  47. (map
  48. #(str (:file %)
  49. (when (:line %) (str ":" (:line %)))
  50. (when (:sci.impl/f-meta %)
  51. (str " calls #'" (get-in % [:sci.impl/f-meta :ns]) "/" (get-in % [:sci.impl/f-meta :name]))))
  52. (reverse stack))))
  53. (println (some-> (get-in m [:ex-data :error]) .-stack))))
  54. (when (= :error (:level m))
  55. (js/process.exit 1)))
  56. (def default-export-options
  57. {;; common options
  58. :rpath-key ::rpath
  59. :notify-user notify-user
  60. :<read-file <read-file
  61. ;; :set-ui-state prn
  62. ;; config file options
  63. ;; TODO: Add actual default
  64. :default-config {}})
  65. (defn- import-file-graph-to-db
  66. "Import a file graph dir just like UI does. However, unlike the UI the
  67. exporter receives file maps containing keys :path and ::rpath since :path
  68. are full paths"
  69. [file-graph-dir db-graph-dir conn options]
  70. (let [*files (build-graph-files file-graph-dir)
  71. config-file (first (filter #(string/ends-with? (:path %) "logseq/config.edn") *files))
  72. _ (assert config-file "No 'logseq/config.edn' found for file graph dir")
  73. options (merge options
  74. default-export-options
  75. ;; asset file options
  76. {:<copy-asset (fn copy-asset [file]
  77. (<copy-asset-file file db-graph-dir file-graph-dir))})]
  78. (gp-exporter/export-file-graph conn conn config-file *files options)))
  79. (defn- resolve-path
  80. "If relative path, resolve with $ORIGINAL_PWD"
  81. [path]
  82. (if (node-path/isAbsolute path)
  83. path
  84. (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
  85. (defn- import-files-to-db
  86. "Import specific doc files for dev purposes"
  87. [file conn {:keys [files] :as options}]
  88. (let [doc-options (gp-exporter/build-doc-options {:macros {}} (merge options default-export-options))
  89. files' (mapv #(hash-map :path %)
  90. (into [file] (map resolve-path files)))]
  91. (p/let [_ (gp-exporter/export-doc-files conn files' <read-file doc-options)]
  92. {:import-state (:import-state doc-options)})))
  93. (def spec
  94. "Options spec"
  95. {:help {:alias :h
  96. :desc "Print help"}
  97. :verbose {:alias :v
  98. :desc "Verbose mode"}
  99. :tag-classes {:alias :t
  100. :coerce []
  101. :desc "List of tags to convert to classes"}
  102. :files {:alias :f
  103. :coerce []
  104. :desc "Additional files to import"}
  105. :property-classes {:alias :p
  106. :coerce []
  107. :desc "List of properties whose values convert to classes"}
  108. :property-parent-classes
  109. {:alias :P
  110. :coerce []
  111. :desc "List of properties whose values convert to a parent class"}})
  112. (defn -main [args]
  113. (let [[file-graph db-graph-dir] args
  114. options (cli/parse-opts args {:spec spec})
  115. _ (when (or (< (count args) 2) (:help options))
  116. (println (str "Usage: $0 FILE-GRAPH DB-GRAPH [OPTIONS]\nOptions:\n"
  117. (cli/format-opts {:spec spec})))
  118. (js/process.exit 1))
  119. [dir db-name] (if (string/includes? db-graph-dir "/")
  120. (let [graph-dir' (resolve-path db-graph-dir)]
  121. ((juxt node-path/dirname node-path/basename) graph-dir'))
  122. [(node-path/join (os/homedir) "logseq" "graphs") db-graph-dir])
  123. file-graph' (resolve-path file-graph)
  124. conn (outliner-cli/init-conn dir db-name {:classpath (cp/get-classpath)})
  125. directory? (.isDirectory (fs/statSync file-graph'))
  126. ;; coerce option collection into strings
  127. user-options (if (:tag-classes options) (update options :tag-classes (partial mapv str)) options)
  128. options' (merge {:user-options (dissoc user-options :verbose :files :help)
  129. :graph-name db-name}
  130. (select-keys options [:files :verbose]))]
  131. (p/let [{:keys [import-state]}
  132. (if directory?
  133. (import-file-graph-to-db file-graph' (node-path/join dir db-name) conn options')
  134. (import-files-to-db file-graph' conn options'))]
  135. (when-let [ignored-props (seq @(:ignored-properties import-state))]
  136. (println "Ignored properties:" (pr-str ignored-props)))
  137. (when (:verbose options') (println "Transacted" (count (d/datoms @conn :eavt)) "datoms"))
  138. (println "Created graph" (str db-name "!")))))
  139. (when (= nbb/*file* (:file (meta #'-main)))
  140. (-main *command-line-args*))