Skip to content

Commit

Permalink
feat(chrome native fs): respect .gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Dec 3, 2020
1 parent 4c738d3 commit 7a8d2dc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dev-release-app": "run-s cljs:dev-release-app gulp:build",
"clean": "gulp clean",
"test": "run-s cljs:test cljs:run-test",
"report": "run-s cljs:report",
"gulp:watch": "gulp watch",
"gulp:build": "NODE_ENV=production gulp build",
"cljs:watch": "clojure -M:cljs watch app publishing",
Expand All @@ -43,6 +44,7 @@
"codemirror": "^5.58.1",
"diff": "^4.0.2",
"dropbox": "^5.2.0",
"ignore": "^5.1.8",
"jszip": "^3.5.0",
"localforage": "^1.7.3",
"mousetrap": "^1.6.5",
Expand Down
10 changes: 9 additions & 1 deletion src/main/frontend/handler/file.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
[clojure.string :as string]
[frontend.history :as history]
[frontend.handler.project :as project-handler]
[lambdaisland.glogi :as log]))
[lambdaisland.glogi :as log]
["ignore" :as Ignore]))

(defn load-file
[repo-url path]
Expand Down Expand Up @@ -225,3 +226,10 @@
(let [path (:file/path file)
content (db/get-file path)]
(alter-file repo path content {:re-render-root? true}))))

(defn ignore-files
[pattern paths]
(-> (Ignore)
(.add pattern)
(.filter (bean/->js paths))
(bean/->clj)))
39 changes: 33 additions & 6 deletions src/main/frontend/handler/web/nfs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[frontend.util :as util]
["/frontend/utils" :as utils]
[frontend.handler.repo :as repo-handler]
[frontend.handler.file :as file-handler]
[frontend.idb :as idb]
[frontend.state :as state]
[clojure.string :as string]
Expand All @@ -17,6 +18,22 @@
[frontend.config :as config]
[lambdaisland.glogi :as log]))

(defn remove-ignore-files
[files]
(let [files (remove (fn [f]
(string/starts-with? (:file/path f) ".git/"))
files)]
(if-let [ignore-file (some #(when (= (:file/name %) ".gitignore")
%) files)]
(if-let [file (:file/file ignore-file)]
(p/let [content (.text file)]
(when content
(let [paths (set (file-handler/ignore-files content (map :file/path files)))]
(when (seq paths)
(filter (fn [f] (contains? paths (:file/path f))) files)))))
(p/resolved files))
(p/resolved files))))

(defn- ->db-files
[dir-name result]
(let [result (flatten (bean/->clj result))]
Expand All @@ -25,7 +42,8 @@
get-attr #(gobj/get file %)
path (-> (get-attr "webkitRelativePath")
(string/replace-first (str dir-name "/") ""))]
{:file/path path
{:file/name (get-attr "name")
:file/path path
:file/last-modified-at (get-attr "lastModified")
:file/size (get-attr "size")
:file/type (get-attr "type")
Expand Down Expand Up @@ -61,7 +79,8 @@
_ (fs/add-nfs-file-handle! root-handle-path root-handle)
_ (set-files! @path-handles)
result (nth result 1)
files (->db-files dir-name result)
files (-> (->db-files dir-name result)
remove-ignore-files)
markup-files (filter-markup-and-built-in-files files)]
(-> (p/all (map (fn [file]
(p/let [content (.text (:file/file file))]
Expand Down Expand Up @@ -160,7 +179,8 @@
(fn [path handle]
(swap! path-handles assoc path handle)))
_ (set-files! @path-handles)
new-files (->db-files dir-name files-result)
new-files (-> (->db-files dir-name files-result)
remove-ignore-files)
get-file-f (fn [path files] (some #(when (= (:file/path %) path) %) files))
{:keys [added modified deleted] :as diffs} (compute-diffs old-files new-files)
;; Use the same labels as isomorphic-git
Expand Down Expand Up @@ -204,9 +224,16 @@
(defn- refresh!
[repo]
(when repo
(p/let [verified? (check-directory-permission! repo)]
(when verified?
(reload-dir! repo)))))
(reload-dir! repo)
;; check permission is too slow
;; (->
;; (p/let [verified? (check-directory-permission! repo)]
;; (prn {:verified? verified?})
;; (when verified?
;; (reload-dir! repo)))
;; (p/catch (fn [error]
;; (log/error :nfs/refresh-error error))))
))

(defn supported?
[]
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2131,7 +2131,7 @@ ignore@^4.0.3:
resolved "https://registry.yarnpkg.com/ignore/-/ignore-4.0.6.tgz#750e3db5862087b4737ebac8207ffd1ef27b25fc"
integrity sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg==

ignore@^5.1.4:
ignore@^5.1.4, ignore@^5.1.8:
version "5.1.8"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57"
integrity sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw==
Expand Down

0 comments on commit 7a8d2dc

Please sign in to comment.