Merge pull request #38 from ethereum/changeNotifierLib

Use chokidar for notification
pull/3094/head
yann300 7 years ago committed by GitHub
commit ca0b2de539
  1. 11
      package.json
  2. 3
      src/router.js
  3. 57
      src/services/sharedFolder.js

@ -1,6 +1,6 @@
{ {
"name": "remixd", "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)", "description": "remix server: allow accessing file system from remix.ethereum.org and start a dev environment (see help section)",
"main": "./src/index.js", "main": "./src/index.js",
"bin" : { "remixd" : "./bin/remixd" }, "bin" : { "remixd" : "./bin/remixd" },
@ -25,14 +25,15 @@
}, },
"homepage": "https://github.com/ethereum/remixd#readme", "homepage": "https://github.com/ethereum/remixd#readme",
"dependencies": { "dependencies": {
"chokidar": "^2.0.2",
"commander": "^2.9.0", "commander": "^2.9.0",
"fs-extra": "^3.0.1", "fs-extra": "^3.0.1",
"isbinaryfile": "^3.0.2", "isbinaryfile": "^3.0.2",
"lerna": "^2.9.0", "lerna": "^2.9.0",
"watch": "^1.0.2",
"websocket": "^1.0.24",
"stdout": "0.0.3",
"serve": "^6.3.1", "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"
} }
} }

@ -8,7 +8,8 @@ class Router {
this.websocket.start((message) => { this.websocket.start((message) => {
this.call(message.id, message.service, message.fn, message.args) 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) servicesList['sharedfolder'].sharedFolder(sharedFolder)
console.log('Shared folder : ' + sharedFolder) console.log('Shared folder : ' + sharedFolder)
return function () { return function () {

@ -1,11 +1,16 @@
var utils = require('../utils') var utils = require('../utils')
var isbinaryfile = require('isbinaryfile') var isbinaryfile = require('isbinaryfile')
var fs = require('fs-extra') var fs = require('fs-extra')
var watch = require('watch') var chokidar = require('chokidar')
module.exports = { module.exports = {
monitors: [],
trackDownStreamUpdate: {}, trackDownStreamUpdate: {},
websocket: null,
alreadyNotified: {},
setWebSocket: function (websocket) {
this.websocket = websocket
},
sharedFolder: function (sharedFolder) { sharedFolder: function (sharedFolder) {
this.sharedFolder = sharedFolder this.sharedFolder = sharedFolder
@ -22,6 +27,10 @@ module.exports = {
resolveDirectory: function (args, cb) { resolveDirectory: function (args, cb) {
try { try {
var path = utils.absolutePath(args.path, this.sharedFolder) 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)) cb(null, utils.resolveDirectory(path, this.sharedFolder))
} catch (e) { } catch (e) {
cb(e.message) cb(e.message)
@ -87,30 +96,32 @@ module.exports = {
}) })
}, },
setupNotifications: function (websocket, path) { setupNotifications: function (path) {
if (!isRealPath(path)) return if (!isRealPath(path)) return
watch.createMonitor(path, (monitor) => { var watcher = chokidar.watch(path, {depth: 0, ignorePermissionErrors: true})
this.monitors.push(monitor) console.log('setup ntifications for ' + path)
monitor.on('created', (f, stat) => { watcher.on('add', (f, stat) => {
isbinaryfile(f, (error, isBinary) => { isbinaryfile(f, (error, isBinary) => {
if (error) console.log(error) if (error) console.log(error)
if (stat.isDirectory()) { if (this.websocket.connection) this.websocket.send(message('created', { path: utils.relativePath(f, this.sharedFolder), isReadOnly: isBinary, isFolder: false }))
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() }))
}) })
}) })
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 }))
})
} }
} }

Loading…
Cancel
Save