Skip to content

Commit

Permalink
refactor: use mkdir-if-not-exists
Browse files Browse the repository at this point in the history
  • Loading branch information
gyk authored and tiensonqin committed Dec 1, 2020
1 parent 14e11c6 commit a6d04f6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/main/frontend/fs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
(when (and dir js/window.pfs)
(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
[dir]
(when (and dir js/window.pfs)
Expand Down
18 changes: 6 additions & 12 deletions src/main/frontend/handler/repo.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@
(let [repo-dir (util/get-repo-dir repo-url)
app-dir config/app-name
dir (str repo-dir "/" app-dir)]
(p/let [_ (-> (fs/mkdir dir)
(p/catch (fn [_e])))]
(p/let [_ (fs/mkdir-if-not-exists dir)]
(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)]
(let [path (str app-dir "/" config/config-file)
Expand All @@ -72,8 +71,7 @@
path (str "pages/contents." (config/get-file-extension format))
file-path (str "/" path)
default-content (util/default-content-with-title format "contents")]
(p/let [_ (-> (fs/mkdir (str repo-dir "/pages"))
(p/catch (fn [_e])))
(p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/pages"))
file-exists? (fs/create-if-not-exists repo-dir file-path default-content)]
(when-not file-exists?
(db/reset-file! repo-url path default-content)
Expand All @@ -86,8 +84,7 @@
path (str config/app-name "/" config/custom-css-file)
file-path (str "/" path)
default-content ""]
(p/let [_ (-> (fs/mkdir (str repo-dir "/" config/app-name))
(p/catch (fn [_e])))
(p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/" config/app-name))
file-exists? (fs/create-if-not-exists repo-dir file-path default-content)]
(when-not file-exists?
(db/reset-file! repo-url path default-content)
Expand All @@ -99,8 +96,7 @@
(let [repo-dir (util/get-repo-dir repo-url)
path (str (config/get-pages-directory) "/how_to_make_dummy_notes.md")
file-path (str "/" path)]
(p/let [_ (-> (fs/mkdir (str repo-dir "/" (config/get-pages-directory)))
(p/catch (fn [_e])))
(p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/" (config/get-pages-directory)))
_file-exists? (fs/create-if-not-exists repo-dir file-path content)]
(db/reset-file! repo-url path content))))

Expand Down Expand Up @@ -134,8 +130,7 @@
empty-blocks? (empty? (db/get-page-blocks-no-cache repo-url (string/lower-case title)))]
(when (or empty-blocks?
(not page-exists?))
(p/let [_ (-> (fs/mkdir (str repo-dir "/" config/default-journals-directory))
(p/catch (fn [_e])))
(p/let [_ (fs/mkdir-if-not-exists (str repo-dir "/" config/default-journals-directory))
file-exists? (fs/create-if-not-exists repo-dir file-path content)]
(when-not file-exists?
(db/reset-file! repo-url path content)
Expand Down Expand Up @@ -463,8 +458,7 @@
[]
(if js/window.pfs
(let [repo config/local-repo]
(p/do! (-> (fs/mkdir (str "/" repo))
(p/catch (fn [_e] nil)))
(p/do! (fs/mkdir-if-not-exists (str "/" repo))
(state/set-current-repo! repo)
(db/start-db-conn! nil repo)
(when-not config/publishing?
Expand Down

0 comments on commit a6d04f6

Please sign in to comment.