@@ -140,10 +140,15 @@ const fileSystemLib = {
140140 } ,
141141 writeFile : function ( ...args ) { // (path, data, options, callback)
142142 let path = args [ 0 ] ;
143+ let newFileCreated = false ;
143144 function callbackInterceptor ( ...interceptedArgs ) {
144145 let err = interceptedArgs . length >= 1 ? interceptedArgs [ 0 ] : null ;
145146 if ( ! err ) {
146- FsWatch . reportChangeEvent ( path ) ;
147+ if ( newFileCreated ) {
148+ FsWatch . reportCreateEvent ( path , false ) ;
149+ } else {
150+ FsWatch . reportChangeEvent ( path ) ;
151+ }
147152 }
148153 if ( args . originalCallback ) {
149154 args . originalCallback ( ...interceptedArgs ) ;
@@ -157,17 +162,23 @@ const fileSystemLib = {
157162
158163 if ( TauriFS . isTauriSubPath ( path ) ) {
159164 return TauriFS . writeFile ( ...args ) ;
160- } else if ( Mounts . isMountSubPath ( path ) ) {
161- return NativeFS . writeFile ( ...args ) ;
162165 }
163- return FilerFSModified . writeFile ( ...args ) ;
166+ fileSystemLib . stat ( path , ( err ) => {
167+ if ( err && err . code === ERR_CODES . ERROR_CODES . ENOENT ) {
168+ newFileCreated = true ;
169+ }
170+ if ( Mounts . isMountSubPath ( path ) ) {
171+ return NativeFS . writeFile ( ...args ) ;
172+ }
173+ return FilerFSModified . writeFile ( ...args ) ;
174+ } ) ;
164175 } ,
165176 mkdir : function ( ...args ) { // (path, mode, callback)
166177 let path = args [ 0 ] ;
167178 function callbackInterceptor ( ...interceptedArgs ) {
168179 let err = interceptedArgs . length >= 1 ? interceptedArgs [ 0 ] : null ;
169180 if ( ! err ) {
170- FsWatch . reportCreateEvent ( path ) ;
181+ FsWatch . reportCreateEvent ( path , true ) ;
171182 }
172183 if ( args . originalCallback ) {
173184 args . originalCallback ( ...interceptedArgs ) ;
@@ -191,8 +202,18 @@ const fileSystemLib = {
191202 function callbackInterceptor ( ...args ) {
192203 let err = args . length >= 1 ? args [ 0 ] : null ;
193204 if ( ! err ) {
194- FsWatch . reportUnlinkEvent ( oldPath ) ;
195- FsWatch . reportCreateEvent ( newPath ) ;
205+ if ( ! TauriFS . isTauriSubPath ( oldPath ) || ! TauriFS . isTauriSubPath ( newPath ) ) {
206+ // we only need to manually handle watch events for non tauri paths as tauri watch is done at node
207+ fileSystemLib . stat ( newPath , ( err , stat ) => {
208+ const isDir = ! err && stat . isDirectory ( ) ;
209+ if ( ! TauriFS . isTauriSubPath ( oldPath ) ) {
210+ FsWatch . reportUnlinkEvent ( oldPath , isDir ) ;
211+ }
212+ if ( ! TauriFS . isTauriSubPath ( newPath ) ) {
213+ FsWatch . reportCreateEvent ( newPath , isDir ) ;
214+ }
215+ } ) ;
216+ }
196217 }
197218 if ( cb ) {
198219 cb ( ...args ) ;
@@ -224,10 +245,11 @@ const fileSystemLib = {
224245 } ) ;
225246 } ,
226247 unlink : function ( path , cb ) {
248+ let isDir ;
227249 function callbackInterceptor ( ...args ) {
228250 let err = args . length >= 1 ? args [ 0 ] : null ;
229251 if ( ! err ) {
230- FsWatch . reportUnlinkEvent ( path ) ;
252+ FsWatch . reportUnlinkEvent ( path , isDir ) ;
231253 }
232254 if ( cb ) {
233255 cb ( ...args ) ;
@@ -239,20 +261,30 @@ const fileSystemLib = {
239261 return ;
240262 } else if ( TauriFS . isTauriSubPath ( path ) ) {
241263 return TauriFS . unlink ( path , callbackInterceptor ) ;
242- } else if ( Mounts . isMountSubPath ( path ) ) {
243- return NativeFS . unlink ( path , callbackInterceptor ) ;
244- }
245- if ( typeof path !== 'string' ) {
246- callbackInterceptor ( new Errors . EINVAL ( 'Invalid arguments.' ) ) ;
247- return ;
248264 }
249- return filerShell . rm ( path , { recursive : true } , callbackInterceptor ) ;
265+
266+ fileSystemLib . stat ( path , ( err , stat ) => {
267+ isDir = ! err && stat . isDirectory ( ) ;
268+ // we do this to emit fs watch events for non-tauri path unlinks.
269+ // We only need to manually handle watch events for non tauri paths as tauri watch is done at node
270+ if ( Mounts . isMountSubPath ( path ) ) {
271+ return NativeFS . unlink ( path , callbackInterceptor ) ;
272+ }
273+ if ( typeof path !== 'string' ) {
274+ callbackInterceptor ( new Errors . EINVAL ( 'Invalid arguments.' ) ) ;
275+ return ;
276+ }
277+ return filerShell . rm ( path , { recursive : true } , callbackInterceptor ) ;
278+ } ) ;
250279 } ,
251280 copy : function ( src , dst , cb ) {
252281 function callbackInterceptor ( ...args ) {
253282 let err = args . length >= 1 ? args [ 0 ] : null ;
254283 if ( ! err ) {
255- FsWatch . reportCreateEvent ( dst ) ;
284+ fileSystemLib . stat ( dst , ( err , stat ) => {
285+ const isDir = ! err && stat . isDirectory ( ) ;
286+ FsWatch . reportCreateEvent ( dst , isDir ) ;
287+ } ) ;
256288 }
257289 if ( cb ) {
258290 cb ( ...args ) ;
@@ -293,15 +325,6 @@ const fileSystemLib = {
293325 }
294326 return FsWatch . unwatchAsync ( eventEmitter ) ;
295327 } ,
296- watch : function ( ...args ) {
297- return FsWatch . watch ( ...args ) ;
298- } ,
299- unwatch : function ( ...args ) {
300- return FsWatch . unwatch ( ...args ) ;
301- } ,
302- unwatchAll : function ( ...args ) {
303- return FsWatch . unwatchAll ( ...args ) ;
304- } ,
305328 moveToTrash : function ( ) {
306329 throw new Errors . ENOSYS ( 'Phoenix fs moveToTrash function not yet supported.' ) ;
307330 } ,
@@ -356,13 +379,7 @@ const fileSystemLib = {
356379 MOUNT_POINT_ROOT : Constants . MOUNT_POINT_ROOT ,
357380 TAURI_ROOT : Constants . TAURI_ROOT ,
358381 ERR_CODES : { } ,
359- WATCH_EVENTS : {
360- ADD_FILE : "add" ,
361- ADD_DIR : "addDir" ,
362- CHANGE : "change" ,
363- UNLINK_FILE : "unlink" ,
364- UNLINK_DIR : "unlinkDir"
365- } ,
382+ WATCH_EVENTS : Constants . WATCH_EVENTS ,
366383 isEncodingSupported : function ( encoding ) {
367384 if ( encoding . toLowerCase ( ) === Constants . BYTE_ARRAY_ENCODING ) {
368385 return true ;
0 commit comments