Merge pull request #38 from ethereum/changeNotifierLib

Use chokidar for notification
pull/454/head
yann300 7 years ago committed by GitHub
commit 97e53d72a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      package.json
  2. 3
      src/router.js
  3. 57
      src/services/sharedFolder.js

@ -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"
}
}

@ -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 () {

@ -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 }))
})
}
}

Loading…
Cancel
Save