@@ -11,6 +11,7 @@ import (
11
11
"github.com/zachzurn/go_shopify"
12
12
"github.com/fsnotify/fsnotify"
13
13
"github.com/mgutz/ansi"
14
+ "errors"
14
15
)
15
16
16
17
@@ -121,6 +122,7 @@ func (s *Store) watch(){
121
122
os .Exit (1 )
122
123
}
123
124
125
+ //Checks if a path is a directory or not using stat -- for this use we really could check for no extension but we have to think about it
124
126
isDirectory := func (pth string ) (bool , error ) {
125
127
fi , err := os .Stat (pth )
126
128
if err != nil {
@@ -131,6 +133,7 @@ func (s *Store) watch(){
131
133
}
132
134
133
135
136
+ //Watches a folder
134
137
watch := func (folder string ){
135
138
err = watcher .Add (folder )
136
139
if err != nil {
@@ -139,7 +142,7 @@ func (s *Store) watch(){
139
142
}
140
143
}
141
144
142
-
145
+ //Watches a folder and any folders inside of it
143
146
watchWalk := func (folder string ){
144
147
filepath .Walk (folder ,func (path string , info os.FileInfo , err error ) error {
145
148
@@ -156,6 +159,7 @@ func (s *Store) watch(){
156
159
})
157
160
}
158
161
162
+ //Unwatches a folder
159
163
unwatch := func (folder string ){
160
164
err = watcher .Remove (folder )
161
165
if err != nil {
@@ -164,19 +168,46 @@ func (s *Store) watch(){
164
168
}
165
169
}
166
170
171
+ //Filter out ingored files and returns last event
172
+ getApplicableEvent := func (events []fsnotify.Event ) (fsnotify.Event , error ) {
173
+
174
+ applicableEvents := []fsnotify.Event {}
175
+
176
+ for _ , e := range events {
177
+
178
+ if extensionAllowed (ext (e .Name )) {
179
+ applicableEvents = append (applicableEvents ,e )
180
+ }
181
+
182
+ }
183
+
184
+ if len (applicableEvents ) > 0 {
185
+ return applicableEvents [len (applicableEvents )- 1 ], nil
186
+ }
187
+
188
+ return fsnotify.Event {}, errors .New ("No applicable events found" )
189
+
190
+ }
191
+
167
192
defer watcher .Close ()
168
193
169
194
done := make (chan bool )
170
195
go func () {
171
196
for {
172
197
select {
173
198
case events := <- watcher .Events :
174
-
175
- event := events [len (events )- 1 ]
199
+
200
+ event , err := getApplicableEvent (events )
201
+
202
+ if err != nil {
203
+ break
204
+ }
205
+
176
206
path := event .Name
177
207
208
+
178
209
if event .Op & fsnotify .Create == fsnotify .Create {
179
- if d , _ := isDirectory (event . Name ); d {
210
+ if d , _ := isDirectory (path ); d {
180
211
//Directory was created, let's watch it
181
212
watch (path )
182
213
} else {
@@ -222,7 +253,7 @@ func (s *Store) deleteRemoteAsset(pth string) {
222
253
pth = strings .Replace (pth ,`\` ,`/` ,- 1 )
223
254
assetKey , err := filepath .Rel (s .Folder , pth )
224
255
assetName := filepath .Base (assetKey )
225
- assetExt := filepath . Ext (assetName )
256
+ assetExt := ext (assetName )
226
257
227
258
if err != nil {
228
259
@@ -248,14 +279,16 @@ func (s *Store) uploadLocalAsset(pth string) {
248
279
pth = strings .Replace (pth ,`\` ,`/` ,- 1 )
249
280
assetKey , err := filepath .Rel (s .Folder , pth )
250
281
assetName := filepath .Base (assetKey )
251
- assetExt := filepath .Ext (assetName )
282
+ assetExt := ext (assetName )
283
+
284
+ fmt .Printf ("%v %v" ,StrUploading ,assetName )
252
285
253
286
if err != nil {
254
287
255
288
}
256
289
257
290
if ! extensionAllowed (assetExt ) {
258
- fmt .Printf ("%v Extension %v ignored. Add in shopify.json as 'allowed_extensions'.\n %v Allowed -> %v\n " ,StrWarning ,ansi .Color (assetExt , "134" ),WarningSpacer ,allowedExtensions )
291
+ fmt .Printf ("\r %v Extension %v ignored. Add in shopify.json as 'allowed_extensions'.\n %v Allowed -> %v\n " ,StrWarning ,ansi .Color (assetExt , "134" ),WarningSpacer ,allowedExtensions )
259
292
return ;
260
293
}
261
294
@@ -266,7 +299,7 @@ func (s *Store) uploadLocalAsset(pth string) {
266
299
file , e := ioutil .ReadFile (pth )
267
300
268
301
if e != nil {
269
- fmt .Printf ("%v Couldn't read file. %v\n " ,StrWarning ,pth )
302
+ fmt .Printf ("\r %v Couldn't read file. %v\n " ,StrWarning ,pth )
270
303
return
271
304
}
272
305
@@ -284,11 +317,11 @@ func (s *Store) uploadLocalAsset(pth string) {
284
317
err = asset .Upload (s .ThemeId )
285
318
286
319
if err != nil {
287
- fmt .Printf ("%v Couldn't upload file. %v\n %v %v\n " ,StrWarning ,assetKey ,WarningSpacer ,err )
320
+ fmt .Printf ("\r %v Couldn't upload file. %v\n %v %v\n " ,StrWarning ,assetKey ,WarningSpacer ,err )
288
321
return
289
322
}
290
323
291
- fmt .Printf ("%v %v\n " ,StrUploaded ,assetName )
324
+ fmt .Printf ("\r %v %v\n " ,StrUploaded ,assetName )
292
325
}
293
326
294
327
@@ -345,6 +378,16 @@ func (s *Store) validate(storeKey string){
345
378
346
379
347
380
/* UTILITY */
381
+ func ext (path string ) string {
382
+ s := strings .Split (path , "." )
383
+
384
+ if len (s ) > 0 {
385
+ return "." + s [len (s )- 1 ]
386
+ }
387
+
388
+ return ""
389
+ }
390
+
348
391
func extensionAllowed (ext string ) bool {
349
392
for _ , a := range allowedExtensions {
350
393
if a == ext {
0 commit comments