From e2058a239dd31dbf0d6c35da67779665b3440b5b Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 19 Mar 2018 15:49:03 +0100 Subject: [PATCH] use chokidar for notification --- package.json | 11 +++---- src/router.js | 3 +- src/services/sharedFolder.js | 57 +++++++++++++++++++++--------------- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index 0570b2fabf..77eaf7f96c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "remixd", - "version": "0.1.8-alpha.1", + "version": "0.1.8-alpha.2", "description": "remix server: allow accessing file system from remix.ethereum.org and start a dev environment (see help section)", "main": "./src/index.js", "bin" : { "remixd" : "./bin/remixd" }, @@ -25,14 +25,15 @@ }, "homepage": "https://github.com/ethereum/remixd#readme", "dependencies": { + "chokidar": "^2.0.2", "commander": "^2.9.0", "fs-extra": "^3.0.1", "isbinaryfile": "^3.0.2", "lerna": "^2.9.0", - "watch": "^1.0.2", - "websocket": "^1.0.24", - "stdout": "0.0.3", "serve": "^6.3.1", - "web3": "ethereum/web3.js#1.0" + "stdout": "0.0.3", + "watch": "^1.0.2", + "web3": "ethereum/web3.js#1.0", + "websocket": "^1.0.24" } } diff --git a/src/router.js b/src/router.js index 986831b829..46518273c3 100644 --- a/src/router.js +++ b/src/router.js @@ -8,7 +8,8 @@ class Router { this.websocket.start((message) => { this.call(message.id, message.service, message.fn, message.args) }) - servicesList['sharedfolder'].setupNotifications(this.websocket, sharedFolder) + servicesList['sharedfolder'].setWebSocket(this.websocket) + servicesList['sharedfolder'].setupNotifications(sharedFolder) servicesList['sharedfolder'].sharedFolder(sharedFolder) console.log('Shared folder : ' + sharedFolder) return function () { diff --git a/src/services/sharedFolder.js b/src/services/sharedFolder.js index 0772e52148..9965c4d1f7 100644 --- a/src/services/sharedFolder.js +++ b/src/services/sharedFolder.js @@ -1,11 +1,16 @@ var utils = require('../utils') var isbinaryfile = require('isbinaryfile') var fs = require('fs-extra') -var watch = require('watch') +var chokidar = require('chokidar') module.exports = { - monitors: [], trackDownStreamUpdate: {}, + websocket: null, + alreadyNotified: {}, + + setWebSocket: function (websocket) { + this.websocket = websocket + }, sharedFolder: function (sharedFolder) { this.sharedFolder = sharedFolder @@ -22,6 +27,10 @@ module.exports = { resolveDirectory: function (args, cb) { try { var path = utils.absolutePath(args.path, this.sharedFolder) + if (this.websocket && !this.alreadyNotified[path]) { + this.alreadyNotified[path] = 1 + this.setupNotifications(path) + } cb(null, utils.resolveDirectory(path, this.sharedFolder)) } catch (e) { cb(e.message) @@ -87,30 +96,32 @@ module.exports = { }) }, - setupNotifications: function (websocket, path) { + setupNotifications: function (path) { if (!isRealPath(path)) return - watch.createMonitor(path, (monitor) => { - this.monitors.push(monitor) - monitor.on('created', (f, stat) => { - isbinaryfile(f, (error, isBinary) => { - if (error) console.log(error) - if (stat.isDirectory()) { - this.setupNotifications(websocket, f) - } - if (websocket.connection) websocket.send(message('created', { path: utils.relativePath(f, this.sharedFolder), isReadOnly: isBinary, isFolder: stat.isDirectory() })) - }) - }) - monitor.on('changed', (f, curr, prev) => { - if (this.trackDownStreamUpdate[f]) { - delete this.trackDownStreamUpdate[f] - return - } - if (websocket.connection) websocket.send(message('changed', utils.relativePath(f, this.sharedFolder))) - }) - monitor.on('removed', (f, stat) => { - if (websocket.connection) websocket.send(message('removed', { path: utils.relativePath(f, this.sharedFolder), isFolder: stat.isDirectory() })) + var watcher = chokidar.watch(path, {depth: 0, ignorePermissionErrors: true}) + console.log('setup ntifications for ' + path) + watcher.on('add', (f, stat) => { + isbinaryfile(f, (error, isBinary) => { + if (error) console.log(error) + if (this.websocket.connection) this.websocket.send(message('created', { path: utils.relativePath(f, this.sharedFolder), isReadOnly: isBinary, isFolder: false })) }) }) + watcher.on('addDir', (f, stat) => { + if (this.websocket.connection) this.websocket.send(message('created', { path: utils.relativePath(f, this.sharedFolder), isReadOnly: false, isFolder: true })) + }) + watcher.on('change', (f, curr, prev) => { + if (this.trackDownStreamUpdate[f]) { + delete this.trackDownStreamUpdate[f] + return + } + if (this.websocket.connection) this.websocket.send(message('changed', utils.relativePath(f, this.sharedFolder))) + }) + watcher.on('unlink', (f) => { + if (this.websocket.connection) this.websocket.send(message('removed', { path: utils.relativePath(f, this.sharedFolder), isFolder: false })) + }) + watcher.on('unlinkDir', (f) => { + if (this.websocket.connection) this.websocket.send(message('removed', { path: utils.relativePath(f, this.sharedFolder), isFolder: true })) + }) } }