1
0

export_graph.cljs 2.9 KB

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