mirror of
https://github.com/logseq/logseq.git
synced 2026-04-24 14:14:55 +00:00
enhance(cli): Add validate option to export-edn command
Useful for sanity checking exports and dev
This commit is contained in:
17
deps/cli/src/logseq/cli/commands/export_edn.cljs
vendored
17
deps/cli/src/logseq/cli/commands/export_edn.cljs
vendored
@@ -19,23 +19,36 @@
|
||||
(= :graph (:export-type options))
|
||||
(assoc :graph-options (dissoc options :file :export-type :graph))))
|
||||
|
||||
(defn- local-export [{{:keys [graph] :as options} :opts}]
|
||||
(defn- validate-export
|
||||
[export-map {:keys [catch-validation-errors?]}]
|
||||
(println "Validating export which can take awhile on large graphs ...")
|
||||
(if-let [error (:error (sqlite-export/validate-export export-map))]
|
||||
(if catch-validation-errors?
|
||||
(js/console.error error)
|
||||
(cli-util/error error))
|
||||
(println "Valid export!")))
|
||||
|
||||
(defn- local-export [{{:keys [graph validate] :as options} :opts}]
|
||||
(when-not graph
|
||||
(cli-util/error "Command missing required option 'graph'"))
|
||||
(if (fs/existsSync (cli-util/get-graph-path graph))
|
||||
(let [conn (apply sqlite-cli/open-db! (cli-util/->open-db-args graph))
|
||||
_ (cli-util/ensure-db-graph-for-command @conn)
|
||||
export-map (sqlite-export/build-export @conn (build-export-options options))]
|
||||
(when validate
|
||||
(validate-export export-map options))
|
||||
(write-export-edn-map export-map options))
|
||||
(cli-util/error "Graph" (pr-str graph) "does not exist")))
|
||||
|
||||
(defn- api-export
|
||||
[{{:keys [api-server-token] :as options} :opts}]
|
||||
[{{:keys [api-server-token validate] :as options} :opts}]
|
||||
(let [opts (build-export-options options)]
|
||||
(-> (p/let [resp (cli-util/api-fetch api-server-token "logseq.cli.export_edn" [(clj->js opts)])]
|
||||
(if (= 200 (.-status resp))
|
||||
(p/let [body (.json resp)
|
||||
export-map (sqlite-util/transit-read (aget body "export-body"))]
|
||||
(when validate
|
||||
(validate-export export-map options))
|
||||
(write-export-edn-map export-map (assoc options :graph (.-graph body))))
|
||||
(cli-util/api-handle-error-response resp)))
|
||||
(p/catch cli-util/command-catch-handler))))
|
||||
|
||||
2
deps/cli/src/logseq/cli/spec.cljs
vendored
2
deps/cli/src/logseq/cli/spec.cljs
vendored
@@ -17,6 +17,8 @@
|
||||
:desc "Include timestamps in export"}
|
||||
:file {:alias :f
|
||||
:desc "File to save export"}
|
||||
:validate {:alias :v
|
||||
:desc "Validates export by importing into a temp graph and validating it"}
|
||||
:catch-validation-errors? {:alias :c
|
||||
:desc "Catch validation errors for dev"}
|
||||
:exclude-namespaces {:alias :e
|
||||
|
||||
4
deps/db/src/logseq/db/sqlite/export.cljs
vendored
4
deps/db/src/logseq/db/sqlite/export.cljs
vendored
@@ -1072,9 +1072,9 @@
|
||||
_ (d/transact! import-conn (concat init-tx block-props-tx misc-tx))
|
||||
validation (db-validate/validate-local-db! @import-conn)]
|
||||
(when-let [errors (seq (:errors validation))]
|
||||
(js/console.error "Exported edn has the following invalid errors when imported into a new graph:")
|
||||
(js/console.error "Exported EDN has the following invalid errors when imported into a new graph:")
|
||||
(pprint/pprint errors)
|
||||
{:error (str "The exported EDN has " (count errors) " error(s). See the javascript console for more details.")}))
|
||||
{:error (str "The exported EDN has " (count errors) " validation error(s)")}))
|
||||
(catch :default e
|
||||
(js/console.error "Unexpected export-edn validation error:" e)
|
||||
{:error (str "The exported EDN is unexpectedly invalid: " (pr-str (ex-message e)))})))
|
||||
Reference in New Issue
Block a user