db_import.cljs 5.0 KB

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