export_graph.cljs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. (ns export-graph
  2. "A script that exports a graph to a sqlite.build EDN file"
  3. (:require ["fs" :as fs]
  4. ["path" :as node-path]
  5. [babashka.cli :as cli]
  6. [clojure.pprint :as pprint]
  7. [logseq.db.common.sqlite-cli :as sqlite-cli]
  8. [logseq.db.sqlite.export :as sqlite-export]
  9. [nbb.core :as nbb]))
  10. (defn- resolve-path
  11. "If relative path, resolve with $ORIGINAL_PWD"
  12. [path]
  13. (if (node-path/isAbsolute path)
  14. path
  15. (node-path/join (or js/process.env.ORIGINAL_PWD ".") path)))
  16. (def spec
  17. "Options spec"
  18. {:help {:alias :h
  19. :desc "Print help"}
  20. :include-timestamps? {:alias :T
  21. :desc "Include timestamps in export"}
  22. :file {:alias :f
  23. :desc "Saves edn to file"}
  24. :catch-validation-errors? {:alias :c
  25. :desc "Catch validation errors for dev"}
  26. :exclude-namespaces {:alias :e
  27. :coerce #{}
  28. :desc "Namespaces to exclude from properties and classes"}
  29. :exclude-built-in-pages? {:alias :b
  30. :desc "Exclude built-in pages"}
  31. :exclude-files? {:alias :F
  32. :desc "Exclude :file/path files"}
  33. :export-type {:alias :t
  34. :coerce :keyword
  35. :desc "Export type"
  36. :default :graph}})
  37. (defn -main [args]
  38. (let [{options :opts args' :args} (cli/parse-args args {:spec spec})
  39. graph-dir (first args')
  40. _ (when (or (nil? graph-dir) (:help options))
  41. (println (str "Usage: $0 GRAPH-NAME [& ARGS] [OPTIONS]\nOptions:\n"
  42. (cli/format-opts {:spec spec})))
  43. (js/process.exit 1))
  44. conn (apply sqlite-cli/open-db! (sqlite-cli/->open-db-args graph-dir))
  45. export-map (sqlite-export/build-export @conn
  46. (cond-> {:export-type (:export-type options)}
  47. (= :graph (:export-type options))
  48. (assoc :graph-options (dissoc options :file :export-type))))]
  49. (if (:file options)
  50. (do
  51. (println "Exported" (count (:properties export-map)) "properties,"
  52. (count (:properties export-map)) "classes and"
  53. (count (:pages-and-blocks export-map)) "pages")
  54. (fs/writeFileSync (resolve-path (:file options))
  55. (with-out-str (pprint/pprint export-map))))
  56. (pprint/pprint export-map))))
  57. (when (= nbb/*file* (nbb/invoked-file))
  58. (-main *command-line-args*))