From f170b0834405f2a770f19f63d32f7b9cb7d0358e Mon Sep 17 00:00:00 2001 From: filip mertens Date: Thu, 22 Jun 2023 13:04:35 +0200 Subject: [PATCH] filechanged --- .../src/app/files/electronProvider.ts | 4 ++- apps/remixdesktop/src/plugins/fsPlugin.ts | 36 +++++++++++++++---- .../workspace/src/lib/actions/events.ts | 7 ++-- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/apps/remix-ide/src/app/files/electronProvider.ts b/apps/remix-ide/src/app/files/electronProvider.ts index 6c60559be8..8050942f12 100644 --- a/apps/remix-ide/src/app/files/electronProvider.ts +++ b/apps/remix-ide/src/app/files/electronProvider.ts @@ -24,7 +24,9 @@ export class ElectronProvider extends FileProvider { this.event.emit('fileRemoved', path) break case 'change': - this.event.emit('fileChanged', path) + this.get(path, (_error, content) => { + this.event.emit('fileExternallyChanged', path, content, false) + }) break case 'rename': this.event.emit('fileRenamed', path) diff --git a/apps/remixdesktop/src/plugins/fsPlugin.ts b/apps/remixdesktop/src/plugins/fsPlugin.ts index 6fdba2d84c..529a8eccf0 100644 --- a/apps/remixdesktop/src/plugins/fsPlugin.ts +++ b/apps/remixdesktop/src/plugins/fsPlugin.ts @@ -78,6 +78,7 @@ const clientProfile: Profile = { class FSPluginClient extends ElectronBasePluginClient { watcher: chokidar.FSWatcher workingDir: string = '' + trackDownStreamUpdate: Record = {} constructor(webContentsId: number, profile: Profile) { super(webContentsId, profile) @@ -155,6 +156,7 @@ class FSPluginClient extends ElectronBasePluginClient { async writeFile(path: string, content: string, options: any): Promise { //console.log('writeFile', path, content, options) + this.trackDownStreamUpdate[path] = content return (fs as any).writeFile(this.fixPath(path), content, options) } @@ -226,14 +228,34 @@ class FSPluginClient extends ElectronBasePluginClient { '**/node_modules/**', '**/.git/**', ] - }).on('all', (eventName, path, stats) => { + }).on('all', async (eventName, path, stats) => { console.log('change', eventName, path, stats) - // remove workingDir from path - path = path.replace(this.workingDir, '') - try { - this.emit('change', eventName, path) - } catch (e) { - console.log('error emitting change', e) + + let pathWithoutPrefix = path.replace(this.workingDir, '') + if (pathWithoutPrefix.startsWith('/')) pathWithoutPrefix = pathWithoutPrefix.slice(1) + + if (eventName === 'change') { + // remove workingDir from path + const newContent = await fs.readFile(path, 'utf-8') + + console.log('change', pathWithoutPrefix, this.trackDownStreamUpdate) + const currentContent = this.trackDownStreamUpdate[pathWithoutPrefix] + + if (currentContent !== newContent) { + try { + + this.emit('change', eventName, pathWithoutPrefix) + } catch (e) { + console.log('error emitting change', e) + } + } + } else { + try { + + this.emit('change', eventName, pathWithoutPrefix) + } catch (e) { + console.log('error emitting change', e) + } } }) } diff --git a/libs/remix-ui/workspace/src/lib/actions/events.ts b/libs/remix-ui/workspace/src/lib/actions/events.ts index 8da0b74971..ba93240c1b 100644 --- a/libs/remix-ui/workspace/src/lib/actions/events.ts +++ b/libs/remix-ui/workspace/src/lib/actions/events.ts @@ -113,7 +113,7 @@ export const listenOnProviderEvents = (provider) => (reducerDispatch: React.Disp dispatch(loadLocalhostRequest()) }) - provider.event.on('fileExternallyChanged', (path: string, content: string) => { + provider.event.on('fileExternallyChanged', (path: string, content: string, showAlert: boolean = true) => { const config = plugin.registry.get('config').api const editor = plugin.registry.get('editor').api @@ -122,6 +122,7 @@ export const listenOnProviderEvents = (provider) => (reducerDispatch: React.Disp if (config.get('currentFile') === path) { // if it's the current file and the content is different: + if(showAlert){ dispatch(displayNotification( path + ' changed', 'This file has been changed outside of Remix IDE.', @@ -129,7 +130,9 @@ export const listenOnProviderEvents = (provider) => (reducerDispatch: React.Disp () => { editor.setText(path, content) } - )) + ))}else{ + editor.setText(path, content) + } } else { // this isn't the current file, we can silently update the model editor.setText(path, content)