From 202198a32e4e783501af08f03ced212f8e05a310 Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Thu, 25 Jun 2020 09:23:02 +0100 Subject: [PATCH 1/4] Restored setupNotifications for remixd --- bin/remixd.ts | 3 ++- package-lock.json | 9 +++++++++ package.json | 10 +++++----- src/index.ts | 3 +-- src/services/remixdClient.ts | 34 +++++++++++++++++++++++++++++++++- 5 files changed, 50 insertions(+), 9 deletions(-) diff --git a/bin/remixd.ts b/bin/remixd.ts index 19ac7cc372..105d9dd56e 100644 --- a/bin/remixd.ts +++ b/bin/remixd.ts @@ -31,6 +31,7 @@ if (program.sharedFolder) { websocketHandler.start((ws: WS) => { sharedFolderClient.setWebSocket(ws) + sharedFolderClient.setupNotifications(program.sharedFolder) sharedFolderClient.sharedFolder(program.sharedFolder, program.readOnly || false) }) killCallBack.push(websocketHandler.close.bind(websocketHandler)) @@ -41,7 +42,7 @@ if (program.sharedFolder) { // kill function kill () { - for (var k in killCallBack) { + for (const k in killCallBack) { try { killCallBack[k]() } catch (e) { diff --git a/package-lock.json b/package-lock.json index 5998a67131..b639056de3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -139,6 +139,15 @@ "integrity": "sha512-OCutwjDZ4aFS6PB1UZ988C4YgwlBHJd6wCeQqaLdmadZ/7e+w79+hbMUFC1QXDNCmdyoRfAFdm0RypzwR+Qpag==", "dev": true }, + "@types/fs-extra": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.1.tgz", + "integrity": "sha512-B42Sxuaz09MhC3DDeW5kubRcQ5by4iuVQ0cRRWM2lggLzAa/KVom0Aft/208NgMvNQQZ86s5rVcqDdn/SH0/mg==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/json-schema": { "version": "7.0.5", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.5.tgz", diff --git a/package.json b/package.json index 7145561c04..fbfeb0c5e6 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "test": "echo \"Error: no test specified\"", "start": "./lib/bin/remixd.js", "npip": "npip", - "lint": "eslint ./src --ext .ts", + "lint": "eslint ./src ./bin --ext .ts", "build": "tsc -p ./ && chmod +x ./lib/bin/remixd.js", "dev": "nodemon" }, @@ -34,12 +34,11 @@ "dependencies": { "@remixproject/plugin": "0.3.0-alpha.3", "@remixproject/plugin-ws": "0.3.0-alpha.1", - "chalk": "^4.0.0", - "chokidar": "^2.0.2", "commander": "^2.20.3", - "fs-extra": "^3.0.1", "isbinaryfile": "^3.0.2", - "ws": "^7.3.0" + "ws": "^7.3.0", + "chokidar": "^2.1.8", + "fs-extra": "^3.0.1" }, "python": { "execPath": "python3", @@ -48,6 +47,7 @@ } }, "devDependencies": { + "@types/fs-extra": "^9.0.1", "@types/node": "^14.0.5", "@types/ws": "^7.2.4", "@typescript-eslint/eslint-plugin": "^3.2.0", diff --git a/src/index.ts b/src/index.ts index 604930f8ee..a92d08b68f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,8 @@ 'use strict' module.exports = { - Router: require('./router'), utils: require('./utils'), services: { - sharedFolder: require('./services/sharedFolder') + sharedFolder: require('./services/remixdClient') } } diff --git a/src/services/remixdClient.ts b/src/services/remixdClient.ts index bfdc04e743..e300dccfa0 100644 --- a/src/services/remixdClient.ts +++ b/src/services/remixdClient.ts @@ -1,9 +1,10 @@ import { PluginClient } from '@remixproject/plugin' import { SharedFolderArgs, TrackDownStreamUpdate, WS, Filelist, ResolveDirectory, FileContent } from '../../types' import * as utils from '../utils' +import * as chokidar from 'chokidar' +import * as fs from 'fs-extra' const isbinaryfile = require('isbinaryfile') -const fs = require('fs-extra') export class RemixdClient extends PluginClient { methods: ['folderIsReadOnly', 'resolveDirectory', 'get', 'exists', 'isFile', 'set', 'list', 'isDirectory'] @@ -193,6 +194,37 @@ export class RemixdClient extends PluginClient { throw new Error(error) } } + + setupNotifications (path: string): void { + if (!isRealPath(path)) return + const watcher = chokidar.watch(path, { depth: 0, ignorePermissionErrors: true }) + 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) + this.emit('created', { path: utils.relativePath(f, this.currentSharedFolder), isReadOnly: isBinary, isFolder: false }) + }) + }) + watcher.on('addDir', (f, stat) => { + this.emit('created', { path: utils.relativePath(f, this.currentSharedFolder), isReadOnly: false, isFolder: true }) + }) + */ + watcher.on('change', (f: string) => { + if (this.trackDownStreamUpdate[f]) { + delete this.trackDownStreamUpdate[f] + return + } + this.emit('changed', utils.relativePath(f, this.currentSharedFolder)) + }) + watcher.on('unlink', (f: string) => { + this.emit('removed', { path: utils.relativePath(f, this.currentSharedFolder), isFolder: false }) + }) + watcher.on('unlinkDir', (f: string) => { + this.emit('removed', { path: utils.relativePath(f, this.currentSharedFolder), isFolder: true }) + }) + } } function isRealPath (path: string): boolean { From 1502cd95c9657038be10ed3c2102213b0e3c3ac2 Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Thu, 25 Jun 2020 09:35:24 +0100 Subject: [PATCH 2/4] Modify emitted event parameters --- src/services/remixdClient.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/services/remixdClient.ts b/src/services/remixdClient.ts index e300dccfa0..729ef61dcb 100644 --- a/src/services/remixdClient.ts +++ b/src/services/remixdClient.ts @@ -219,10 +219,10 @@ export class RemixdClient extends PluginClient { this.emit('changed', utils.relativePath(f, this.currentSharedFolder)) }) watcher.on('unlink', (f: string) => { - this.emit('removed', { path: utils.relativePath(f, this.currentSharedFolder), isFolder: false }) + this.emit('removed', utils.relativePath(f, this.currentSharedFolder), false) }) watcher.on('unlinkDir', (f: string) => { - this.emit('removed', { path: utils.relativePath(f, this.currentSharedFolder), isFolder: true }) + this.emit('removed', utils.relativePath(f, this.currentSharedFolder), true) }) } } From ed177e806382ce84e96f542aebad620af1bdf5f4 Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Thu, 25 Jun 2020 11:07:09 +0100 Subject: [PATCH 3/4] Check if notification absolute path exists --- src/services/remixdClient.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/services/remixdClient.ts b/src/services/remixdClient.ts index 729ef61dcb..3bc2626d23 100644 --- a/src/services/remixdClient.ts +++ b/src/services/remixdClient.ts @@ -196,7 +196,9 @@ export class RemixdClient extends PluginClient { } setupNotifications (path: string): void { - if (!isRealPath(path)) return + const absPath = utils.absolutePath('./', path) + + if (!isRealPath(absPath)) return const watcher = chokidar.watch(path, { depth: 0, ignorePermissionErrors: true }) console.log('setup notifications for ' + path) /* we can't listen on created file / folder From 91e2f2315757b4f582702c6c70e2f1275a617b2b Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Thu, 25 Jun 2020 14:26:41 +0100 Subject: [PATCH 4/4] Expose websocket and shared folder api for external services --- src/index.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index a92d08b68f..8211aa8028 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,12 @@ 'use strict' +import { RemixdClient as sharedFolder } from './services/remixdClient' +import Websocket from './websocket' +import * as utils from './utils' module.exports = { - utils: require('./utils'), + Websocket, + utils, services: { - sharedFolder: require('./services/remixdClient') + sharedFolder } }