diff --git a/src/main/logseq/cli/config.cljs b/src/main/logseq/cli/config.cljs index 90db4fb1b2..7b73207ac4 100644 --- a/src/main/logseq/cli/config.cljs +++ b/src/main/logseq/cli/config.cljs @@ -4,9 +4,9 @@ [clojure.string :as string] [goog.object :as gobj] ["fs" :as fs] - ["os" :as os] ["path" :as node-path] - [logseq.cli.output-mode :as output-mode])) + [logseq.cli.output-mode :as output-mode] + [logseq.cli.root-dir :as root-dir])) (defn- parse-int [value] @@ -30,19 +30,15 @@ :else nil)) -(defn- default-root-dir - [] - "~/logseq") - (defn- default-config-path ([] - (default-config-path (default-root-dir))) + (default-config-path (root-dir/default-root-dir))) ([root-dir] (node-path/join root-dir "cli.edn"))) (defn server-list-path [root-dir] - (node-path/join (or root-dir (node-path/join (.homedir os) "logseq")) "server-list")) + (node-path/join (or root-dir (root-dir/default-root-dir)) "server-list")) (def ^:private removed-config-keys #{:auth-token :retries :e2ee-password}) @@ -67,7 +63,7 @@ (defn update-config! [{:keys [config-path root-dir]} updates] - (let [path (or config-path (default-config-path (or root-dir (default-root-dir)))) + (let [path (or config-path (default-config-path (root-dir/normalize-root-dir root-dir))) current (or (read-config-file path) {}) filtered-current (sanitize-file-config current) filtered-updates (sanitize-file-config updates) @@ -115,21 +111,23 @@ :logout-timeout-ms 120000 :list-title-max-display-width list-title-max-display-width-default :output-format nil - :root-dir (default-root-dir) + :root-dir (root-dir/default-root-dir) :ws-url "wss://api.logseq.io/sync/%s" :http-base "https://api.logseq.io"} env (env-config) - root-dir (or (:root-dir opts) - (:root-dir env) - (:root-dir defaults)) + root-dir (root-dir/normalize-root-dir + (or (:root-dir opts) + (:root-dir env) + (:root-dir defaults))) config-path (or (:config-path opts) (:config-path env) (default-config-path root-dir)) file-config (or (read-config-file config-path) {}) - root-dir (or (:root-dir opts) - (:root-dir env) - (:root-dir file-config) - root-dir) + root-dir (root-dir/normalize-root-dir + (or (:root-dir opts) + (:root-dir env) + (:root-dir file-config) + root-dir)) output-format (or (output-mode/parse (:output-format opts)) (output-mode/parse (:output opts)) (output-mode/parse (:output-format env)) diff --git a/src/test/logseq/cli/config_test.cljs b/src/test/logseq/cli/config_test.cljs index ace5c1a0e5..c2a9c78590 100644 --- a/src/test/logseq/cli/config_test.cljs +++ b/src/test/logseq/cli/config_test.cljs @@ -48,7 +48,7 @@ result (with-env env #(config/resolve-config opts))] (is (= cfg-path (:config-path result))) (is (= "cli-repo" (:graph result))) - (is (= "cli-root" (:root-dir result))) + (is (= (node-path/resolve "cli-root") (:root-dir result))) (is (= 333 (:timeout-ms result))) (is (= 888 (:login-timeout-ms result))) (is (= 999 (:logout-timeout-ms result))) @@ -65,7 +65,7 @@ "LOGSEQ_CLI_ROOT_DIR" "env-root"} result (with-env env #(config/resolve-config {:config-path cfg-path}))] (is (= "env-repo" (:graph result))) - (is (= "env-root" (:root-dir result))))) + (is (= (node-path/resolve "env-root") (:root-dir result))))) (deftest test-output-format-env-overrides-file (let [dir (node-helper/create-tmp-dir) @@ -110,7 +110,7 @@ "LOGSEQ_CLI_CONFIG" nil} #(config/resolve-config {:config-path cfg-path}))] (is (= cfg-path (:config-path result))) - (is (= "~/logseq" (:root-dir result))) + (is (= (node-path/join (.homedir os) "logseq") (:root-dir result))) (is (= "wss://api.logseq.io/sync/%s" (:ws-url result))) (is (= "https://api.logseq.io" (:http-base result))) (is (= 10000 (:timeout-ms result))) @@ -127,8 +127,8 @@ "LOGSEQ_CLI_OUTPUT" nil "LOGSEQ_CLI_CONFIG" nil} #(config/resolve-config {:root-dir "~/custom-logseq"}))] - (is (= "~/custom-logseq" (:root-dir result))) - (is (= (node-path/join "~/custom-logseq" "cli.edn") + (is (= (node-path/join (.homedir os) "custom-logseq") (:root-dir result))) + (is (= (node-path/join (.homedir os) "custom-logseq" "cli.edn") (:config-path result))))) (deftest test-explicit-config-path-does-not-change-root-dir-derived-defaults @@ -137,7 +137,7 @@ result (config/resolve-config {:config-path cfg-path :root-dir "~/custom-logseq"})] (is (= cfg-path (:config-path result))) - (is (= "~/custom-logseq" (:root-dir result))))) + (is (= (node-path/join (.homedir os) "custom-logseq") (:root-dir result))))) (deftest test-server-list-path-follows-root-dir (let [root-dir (node-path/join (node-helper/create-tmp-dir "cli-root") "nested-root") @@ -202,3 +202,11 @@ parsed (reader/read-string contents)] (is (= "old" (:graph parsed))) (is (not (contains? parsed :auth-token))))) + +(deftest test-update-config-expands-tilde-in-root-dir + (let [dir (node-helper/create-tmp-dir "cli-tilde") + cfg-path (node-path/join dir "cli.edn") + _ (config/update-config! {:root-dir dir} {:ws-url "wss://example.com"}) + contents (.toString (fs/readFileSync cfg-path) "utf8") + parsed (reader/read-string contents)] + (is (= "wss://example.com" (:ws-url parsed)))))