db_import.cljs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. (println "Ex-data:" (pr-str (dissoc (:ex-data m) :error)))
  42. (println "Stacktrace:")
  43. (if-let [stack (some-> (get-in m [:ex-data :error]) ex-data :sci.impl/callstack deref)]
  44. (println (string/join
  45. "\n"
  46. (map
  47. #(str (:file %)
  48. (when (:line %) (str ":" (:line %)))
  49. (when (:sci.impl/f-meta %)
  50. (str " calls #'" (get-in % [:sci.impl/f-meta :ns]) "/" (get-in % [:sci.impl/f-meta :name]))))
  51. (reverse stack))))
  52. (println (some-> (get-in m [:ex-data :error]) .-stack)))
  53. (when (= :error (:level m))
  54. (js/process.exit 1)))
  55. (def default-export-options
  56. {;; common options
  57. :rpath-key ::rpath
  58. :notify-user notify-user
  59. :<read-file <read-file
  60. ;; :set-ui-state prn
  61. ;; config file options
  62. ;; TODO: Add actual default
  63. :default-config {}})
  64. (defn- import-file-graph-to-db
  65. "Import a file graph dir just like UI does. However, unlike the UI the
  66. exporter receives file maps containing keys :path and ::rpath since :path
  67. are full paths"
  68. [file-graph-dir db-graph-dir conn options]
  69. (let [*files (build-graph-files file-graph-dir)
  70. config-file (first (filter #(string/ends-with? (:path %) "logseq/config.edn") *files))
  71. _ (assert config-file "No 'logseq/config.edn' found for file graph dir")
  72. options (merge options
  73. default-export-options
  74. ;; asset file options
  75. {:<copy-asset (fn copy-asset [file]
  76. (<copy-asset-file file db-graph-dir file-graph-dir))})]
  77. (gp-exporter/export-file-graph conn conn config-file *files options)))
  78. (defn- resolve-path
  79. "If relative path, resolve with $ORIGINAL_PWD"
  80. [path]
  81. (if (node-path/isAbsolute path)
  82. path
  83. (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
  84. (defn- import-files-to-db
  85. "Import specific doc files for dev purposes"
  86. [file conn {:keys [files] :as options}]
  87. (let [doc-options (gp-exporter/build-doc-options {:macros {}} (merge options default-export-options))
  88. files' (mapv #(hash-map :path %)
  89. (into [file] (map resolve-path files)))]
  90. (gp-exporter/export-doc-files conn files' <read-file doc-options)))
  91. (def spec
  92. "Options spec"
  93. {:help {:alias :h
  94. :desc "Print help"}
  95. :verbose {:alias :v
  96. :desc "Verbose mode"}
  97. :tag-classes {:alias :t
  98. :coerce []
  99. :desc "List of tags to convert to classes"}
  100. :files {:alias :f
  101. :coerce []
  102. :desc "Additional files to import"}
  103. :property-classes {:alias :p
  104. :coerce []
  105. :desc "List of properties whose values convert to classes"}
  106. :property-parent-classes
  107. {:alias :P
  108. :coerce []
  109. :desc "List of properties whose values convert to a parent class"}})
  110. (defn -main [args]
  111. (let [[file-graph db-graph-dir] args
  112. options (cli/parse-opts args {:spec spec})
  113. _ (when (or (< (count args) 2) (:help options))
  114. (println (str "Usage: $0 FILE-GRAPH DB-GRAPH [OPTIONS]\nOptions:\n"
  115. (cli/format-opts {:spec spec})))
  116. (js/process.exit 1))
  117. [dir db-name] (if (string/includes? db-graph-dir "/")
  118. (let [graph-dir' (resolve-path db-graph-dir)]
  119. ((juxt node-path/dirname node-path/basename) graph-dir'))
  120. [(node-path/join (os/homedir) "logseq" "graphs") db-graph-dir])
  121. file-graph' (resolve-path file-graph)
  122. conn (outliner-cli/init-conn dir db-name {:classpath (cp/get-classpath)})
  123. directory? (.isDirectory (fs/statSync file-graph'))]
  124. (p/do!
  125. (if directory?
  126. (import-file-graph-to-db file-graph' (node-path/join dir db-name) conn (merge options {:graph-name db-name}))
  127. (import-files-to-db file-graph' conn (merge options {:graph-name db-name})))
  128. (when (:verbose options) (println "Transacted" (count (d/datoms @conn :eavt)) "datoms"))
  129. (println "Created graph" (str db-name "!")))))
  130. (when (= nbb/*file* (:file (meta #'-main)))
  131. (-main *command-line-args*))