export_graph.cljs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. (defn -main [args]
  34. (let [{options :opts args' :args} (cli/parse-args args {:spec spec})
  35. graph-dir (first args')
  36. _ (when (or (nil? graph-dir) (:help options))
  37. (println (str "Usage: $0 GRAPH-NAME [& ARGS] [OPTIONS]\nOptions:\n"
  38. (cli/format-opts {:spec spec})))
  39. (js/process.exit 1))
  40. conn (apply sqlite-cli/open-db! (sqlite-cli/->open-db-args graph-dir))
  41. export-options (dissoc options :file)
  42. export-map (sqlite-export/build-export @conn {:export-type :graph :graph-options export-options})]
  43. (if (:file options)
  44. (do
  45. (println "Exported" (count (:properties export-map)) "properties,"
  46. (count (:properties export-map)) "classes and"
  47. (count (:pages-and-blocks export-map)) "pages")
  48. (fs/writeFileSync (resolve-path (:file options))
  49. (with-out-str (pprint/pprint export-map))))
  50. (pprint/pprint export-map))))
  51. (when (= nbb/*file* (nbb/invoked-file))
  52. (-main *command-line-args*))