db_import.cljs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. (gp-exporter/export-doc-files conn files' <read-file doc-options)))
  92. (def spec
  93. "Options spec"
  94. {:help {:alias :h
  95. :desc "Print help"}
  96. :verbose {:alias :v
  97. :desc "Verbose mode"}
  98. :tag-classes {:alias :t
  99. :coerce []
  100. :desc "List of tags to convert to classes"}
  101. :files {:alias :f
  102. :coerce []
  103. :desc "Additional files to import"}
  104. :property-classes {:alias :p
  105. :coerce []
  106. :desc "List of properties whose values convert to classes"}
  107. :property-parent-classes
  108. {:alias :P
  109. :coerce []
  110. :desc "List of properties whose values convert to a parent class"}})
  111. (defn -main [args]
  112. (let [[file-graph db-graph-dir] args
  113. options (cli/parse-opts args {:spec spec})
  114. _ (when (or (< (count args) 2) (:help options))
  115. (println (str "Usage: $0 FILE-GRAPH DB-GRAPH [OPTIONS]\nOptions:\n"
  116. (cli/format-opts {:spec spec})))
  117. (js/process.exit 1))
  118. [dir db-name] (if (string/includes? db-graph-dir "/")
  119. (let [graph-dir' (resolve-path db-graph-dir)]
  120. ((juxt node-path/dirname node-path/basename) graph-dir'))
  121. [(node-path/join (os/homedir) "logseq" "graphs") db-graph-dir])
  122. file-graph' (resolve-path file-graph)
  123. conn (outliner-cli/init-conn dir db-name {:classpath (cp/get-classpath)})
  124. directory? (.isDirectory (fs/statSync file-graph'))]
  125. (p/do!
  126. (if directory?
  127. (import-file-graph-to-db file-graph' (node-path/join dir db-name) conn (merge options {:graph-name db-name}))
  128. (import-files-to-db file-graph' conn (merge options {:graph-name db-name})))
  129. (when (:verbose options) (println "Transacted" (count (d/datoms @conn :eavt)) "datoms"))
  130. (println "Created graph" (str db-name "!")))))
  131. (when (= nbb/*file* (:file (meta #'-main)))
  132. (-main *command-line-args*))