Commit 6ccf9992 authored by ioedeveloper's avatar ioedeveloper

Changed sharedFolder into ES6 class

parent db4e5cdf
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
var Router = require('../src/router') var Router = require('../src/router')
var servicesList = require('../src/servicesList') var servicesList = require('../src/servicesList')
var program = require('commander') var program = require('commander')
const { buildWebsocketClient } = require('@remixproject/plugin-ws')
program program
.usage('-s <shared folder>') .usage('-s <shared folder>')
...@@ -27,6 +28,7 @@ if (program.sharedFolder) { ...@@ -27,6 +28,7 @@ if (program.sharedFolder) {
servicesList['sharedfolder'].setWebSocket(webSocket) servicesList['sharedfolder'].setWebSocket(webSocket)
servicesList['sharedfolder'].setupNotifications(program.sharedFolder) servicesList['sharedfolder'].setupNotifications(program.sharedFolder)
servicesList['sharedfolder'].sharedFolder(program.sharedFolder, program.readOnly || false) servicesList['sharedfolder'].sharedFolder(program.sharedFolder, program.readOnly || false)
buildWebsocketClient(webSocket.connection, new servicesList['sharedfolder']())
}) })
killCallBack.push(sharedFolderrouter.start()) killCallBack.push(sharedFolderrouter.start())
} }
......
This diff is collapsed.
...@@ -29,15 +29,12 @@ ...@@ -29,15 +29,12 @@
}, },
"homepage": "https://github.com/ethereum/remixd#readme", "homepage": "https://github.com/ethereum/remixd#readme",
"dependencies": { "dependencies": {
"@remixproject/plugin": "^0.1.18",
"@remixproject/plugin-ws": "^0.2.0-alpha.1",
"chokidar": "^2.0.2", "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",
"nopy": "^0.2.6",
"serve": "7.0.0",
"stdout": "0.0.3",
"watch": "^1.0.2",
"websocket": "^1.0.24" "websocket": "^1.0.24"
}, },
"python": { "python": {
......
...@@ -2,31 +2,32 @@ var utils = require('../utils') ...@@ -2,31 +2,32 @@ var utils = require('../utils')
var isbinaryfile = require('isbinaryfile') var isbinaryfile = require('isbinaryfile')
var fs = require('fs-extra') var fs = require('fs-extra')
var chokidar = require('chokidar') var chokidar = require('chokidar')
const { PluginClient } = require('@remixproject/plugin')
module.exports = { class SharedFolder extends PluginClient {
trackDownStreamUpdate: {}, trackDownStreamUpdate = {}
websocket: null, websocket = null
alreadyNotified: {}, alreadyNotified = {}
setWebSocket: function (websocket) { setWebSocket (websocket) {
this.websocket = websocket this.websocket = websocket
}, }
sharedFolder: function (currentSharedFolder, readOnly) { sharedFolder (currentSharedFolder, readOnly) {
this.currentSharedFolder = currentSharedFolder this.currentSharedFolder = currentSharedFolder
this.readOnly = readOnly this.readOnly = readOnly
if (this.websocket.connection) this.websocket.send(message('rootFolderChanged', {})) if (this.websocket.connection) this.websocket.send(message('rootFolderChanged', {}))
}, }
list: function (args, cb) { list (args, cb) {
try { try {
cb(null, utils.walkSync(this.currentSharedFolder, {}, this.currentSharedFolder)) cb(null, utils.walkSync(this.currentSharedFolder, {}, this.currentSharedFolder))
} catch (e) { } catch (e) {
cb(e.message) cb(e.message)
} }
}, }
resolveDirectory: function (args, cb) { resolveDirectory (args, cb) {
try { try {
var path = utils.absolutePath(args.path, this.currentSharedFolder) var path = utils.absolutePath(args.path, this.currentSharedFolder)
if (this.websocket && !this.alreadyNotified[path]) { if (this.websocket && !this.alreadyNotified[path]) {
...@@ -37,13 +38,13 @@ module.exports = { ...@@ -37,13 +38,13 @@ module.exports = {
} catch (e) { } catch (e) {
cb(e.message) cb(e.message)
} }
}, }
folderIsReadOnly: function (args, cb) { folderIsReadOnly (args, cb) {
return cb(null, this.readOnly) return cb(null, this.readOnly)
}, }
get: function (args, cb) { get (args, cb) {
var path = utils.absolutePath(args.path, this.currentSharedFolder) var path = utils.absolutePath(args.path, this.currentSharedFolder)
if (!fs.existsSync(path)) { if (!fs.existsSync(path)) {
return cb('File not found ' + path) return cb('File not found ' + path)
...@@ -60,15 +61,15 @@ module.exports = { ...@@ -60,15 +61,15 @@ module.exports = {
}) })
} }
}) })
}, }
exists: function (args, cb) { exists (args, cb) {
const path = utils.absolutePath(args.path, this.currentSharedFolder) const path = utils.absolutePath(args.path, this.currentSharedFolder)
cb(null, fs.existsSync(path)) cb(null, fs.existsSync(path))
}, }
set: function (args, cb) { set (args, cb) {
if (this.readOnly) return cb('Cannot write file: read-only mode selected') if (this.readOnly) return cb('Cannot write file: read-only mode selected')
const isFolder = args.path.endsWith('/') const isFolder = args.path.endsWith('/')
var path = utils.absolutePath(args.path, this.currentSharedFolder) var path = utils.absolutePath(args.path, this.currentSharedFolder)
...@@ -88,9 +89,9 @@ module.exports = { ...@@ -88,9 +89,9 @@ module.exports = {
}) })
}).catch(e => cb(e)) }).catch(e => cb(e))
} }
}, }
rename: function (args, cb) { rename (args, cb) {
if (this.readOnly) return cb('Cannot rename file: read-only mode selected') if (this.readOnly) return cb('Cannot rename file: read-only mode selected')
var oldpath = utils.absolutePath(args.oldPath, this.currentSharedFolder) var oldpath = utils.absolutePath(args.oldPath, this.currentSharedFolder)
if (!fs.existsSync(oldpath)) { if (!fs.existsSync(oldpath)) {
...@@ -102,9 +103,9 @@ module.exports = { ...@@ -102,9 +103,9 @@ module.exports = {
if (error) console.log(error) if (error) console.log(error)
cb(error, data) cb(error, data)
}) })
}, }
remove: function (args, cb) { remove (args, cb) {
if (this.readOnly) return cb('Cannot remove file: read-only mode selected') if (this.readOnly) return cb('Cannot remove file: read-only mode selected')
var path = utils.absolutePath(args.path, this.currentSharedFolder) var path = utils.absolutePath(args.path, this.currentSharedFolder)
if (!fs.existsSync(path)) { if (!fs.existsSync(path)) {
...@@ -118,36 +119,24 @@ module.exports = { ...@@ -118,36 +119,24 @@ module.exports = {
} }
cb(error, true) cb(error, true)
}) })
}, }
isDirectory: function (args, cb) { isDirectory (args, cb) {
const path = utils.absolutePath(args.path, this.currentSharedFolder) const path = utils.absolutePath(args.path, this.currentSharedFolder)
cb(null, fs.statSync(path).isDirectory()) cb(null, fs.statSync(path).isDirectory())
}, }
isFile: function (args, cb) { isFile (args, cb) {
const path = utils.absolutePath(args.path, this.currentSharedFolder) const path = utils.absolutePath(args.path, this.currentSharedFolder)
cb(null, fs.statSync(path).isFile()) cb(null, fs.statSync(path).isFile())
}, }
setupNotifications: function (path) { setupNotifications (path) {
if (!isRealPath(path)) return if (!isRealPath(path)) return
var watcher = chokidar.watch(path, { depth: 0, ignorePermissionErrors: true }) var watcher = chokidar.watch(path, { depth: 0, ignorePermissionErrors: true })
console.log('setup notifications for ' + path) console.log('setup notifications for ' + path)
/* we can't listen on created file / folder
watcher.on('add', (f, stat) => {
isbinaryfile(f, (error, isBinary) => {
if (error) console.log(error)
console.log('add', f)
if (this.websocket.connection) this.websocket.send(message('created', { path: utils.relativePath(f, this.currentSharedFolder), isReadOnly: isBinary, isFolder: false }))
})
})
watcher.on('addDir', (f, stat) => {
if (this.websocket.connection) this.websocket.send(message('created', { path: utils.relativePath(f, this.currentSharedFolder), isReadOnly: false, isFolder: true }))
})
*/
watcher.on('change', (f, curr, prev) => { watcher.on('change', (f, curr, prev) => {
if (this.trackDownStreamUpdate[f]) { if (this.trackDownStreamUpdate[f]) {
delete this.trackDownStreamUpdate[f] delete this.trackDownStreamUpdate[f]
...@@ -162,6 +151,7 @@ module.exports = { ...@@ -162,6 +151,7 @@ module.exports = {
if (this.websocket.connection) this.websocket.send(message('removed', { path: utils.relativePath(f, this.currentSharedFolder), isFolder: true })) if (this.websocket.connection) this.websocket.send(message('removed', { path: utils.relativePath(f, this.currentSharedFolder), isFolder: true }))
}) })
} }
} }
function isRealPath (path, cb) { function isRealPath (path, cb) {
...@@ -178,3 +168,5 @@ function isRealPath (path, cb) { ...@@ -178,3 +168,5 @@ function isRealPath (path, cb) {
function message (name, value) { function message (name, value) {
return JSON.stringify({ type: 'notification', scope: 'sharedfolder', name: name, value: value }) return JSON.stringify({ type: 'notification', scope: 'sharedfolder', name: name, value: value })
} }
module.exports = SharedFolder
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment