db_import.cljs 7.0 KB

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