refactor: use mkdir-if-not-exists

pull/796/head
Yukun Guo 2020-11-30 18:48:52 +08:00 committed by Tienson Qin
parent 14e11c6740
commit a6d04f6b38
2 changed files with 14 additions and 12 deletions

View File

@ -6,6 +6,14 @@
(when (and dir js/window.pfs) (when (and dir js/window.pfs)
(js/window.pfs.mkdir dir))) (js/window.pfs.mkdir dir)))
(defn mkdir-if-not-exists
[dir]
(when (and dir js/window.pfs)
(util/p-handle
(js/window.pfs.stat dir)
(fn [_stat])
(fn [_error] (js/window.pfs.mkdir dir)))))
(defn readdir (defn readdir
[dir] [dir]
(when (and dir js/window.pfs) (when (and dir js/window.pfs)

View File

@ -50,8 +50,7 @@
(let [repo-dir (util/get-repo-dir repo-url) (let [repo-dir (util/get-repo-dir repo-url)
app-dir config/app-name app-dir config/app-name
dir (str repo-dir "/" app-dir)] dir (str repo-dir "/" app-dir)]
(p/let [_ (-> (fs/mkdir dir) (p/let [_ (fs/mkdir-if-not-exists dir)]
(p/catch (fn [_e])))]
(let [default-content config/config-default-content] (let [default-content config/config-default-content]
(p/let [file-exists? (fs/create-if-not-exists repo-dir (str app-dir "/" config/config-file) default-content)] (p/let [file-exists? (fs/create-if-not-exists repo-dir (str app-dir "/" config/config-file) default-content)]
(let [path (str app-dir "/" config/config-file) (let [path (str app-dir "/" config/config-file)
@ -72,8 +71,7 @@
path (str "pages/contents." (config/get-file-extension format)) path (str "pages/contents." (config/get-file-extension format))
file-path (str "/" path) file-path (str "/" path)
default-content (util/default-content-with-title format "contents")] default-content (util/default-content-with-title format "contents")]
(p/let [_ (-> (fs/mkdir (str repo-dir "/pages")) (p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/pages"))
(p/catch (fn [_e])))
file-exists? (fs/create-if-not-exists repo-dir file-path default-content)] file-exists? (fs/create-if-not-exists repo-dir file-path default-content)]
(when-not file-exists? (when-not file-exists?
(db/reset-file! repo-url path default-content) (db/reset-file! repo-url path default-content)
@ -86,8 +84,7 @@
path (str config/app-name "/" config/custom-css-file) path (str config/app-name "/" config/custom-css-file)
file-path (str "/" path) file-path (str "/" path)
default-content ""] default-content ""]
(p/let [_ (-> (fs/mkdir (str repo-dir "/" config/app-name)) (p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/" config/app-name))
(p/catch (fn [_e])))
file-exists? (fs/create-if-not-exists repo-dir file-path default-content)] file-exists? (fs/create-if-not-exists repo-dir file-path default-content)]
(when-not file-exists? (when-not file-exists?
(db/reset-file! repo-url path default-content) (db/reset-file! repo-url path default-content)
@ -99,8 +96,7 @@
(let [repo-dir (util/get-repo-dir repo-url) (let [repo-dir (util/get-repo-dir repo-url)
path (str (config/get-pages-directory) "/how_to_make_dummy_notes.md") path (str (config/get-pages-directory) "/how_to_make_dummy_notes.md")
file-path (str "/" path)] file-path (str "/" path)]
(p/let [_ (-> (fs/mkdir (str repo-dir "/" (config/get-pages-directory))) (p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/" (config/get-pages-directory)))
(p/catch (fn [_e])))
_file-exists? (fs/create-if-not-exists repo-dir file-path content)] _file-exists? (fs/create-if-not-exists repo-dir file-path content)]
(db/reset-file! repo-url path content)))) (db/reset-file! repo-url path content))))
@ -134,8 +130,7 @@
empty-blocks? (empty? (db/get-page-blocks-no-cache repo-url (string/lower-case title)))] empty-blocks? (empty? (db/get-page-blocks-no-cache repo-url (string/lower-case title)))]
(when (or empty-blocks? (when (or empty-blocks?
(not page-exists?)) (not page-exists?))
(p/let [_ (-> (fs/mkdir (str repo-dir "/" config/default-journals-directory)) (p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/" config/default-journals-directory))
(p/catch (fn [_e])))
file-exists? (fs/create-if-not-exists repo-dir file-path content)] file-exists? (fs/create-if-not-exists repo-dir file-path content)]
(when-not file-exists? (when-not file-exists?
(db/reset-file! repo-url path content) (db/reset-file! repo-url path content)
@ -463,8 +458,7 @@
[] []
(if js/window.pfs (if js/window.pfs
(let [repo config/local-repo] (let [repo config/local-repo]
(p/do! (-> (fs/mkdir (str "/" repo)) (p/do! (fs/mkdir-if-not-exists (str "/" repo))
(p/catch (fn [_e] nil)))
(state/set-current-repo! repo) (state/set-current-repo! repo)
(db/start-db-conn! nil repo) (db/start-db-conn! nil repo)
(when-not config/publishing? (when-not config/publishing?