From 099b4a00534d33416efa935ab5110a8ed3d13df7 Mon Sep 17 00:00:00 2001 From: aniket-engg Date: Tue, 15 Mar 2022 17:58:56 +0530 Subject: [PATCH] truffle integration --- libs/remixd/src/bin/remixd.ts | 9 ++++++- libs/remixd/src/index.ts | 2 ++ libs/remixd/src/serviceList.ts | 1 + libs/remixd/src/services/truffleClient.ts | 31 +++++++++++++++++++++++ libs/remixd/src/websocket.ts | 3 ++- 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 libs/remixd/src/services/truffleClient.ts diff --git a/libs/remixd/src/bin/remixd.ts b/libs/remixd/src/bin/remixd.ts index f30b68f247..75319b8448 100644 --- a/libs/remixd/src/bin/remixd.ts +++ b/libs/remixd/src/bin/remixd.ts @@ -25,6 +25,7 @@ async function warnLatestVersion () { const services = { git: (readOnly: boolean) => new servicesList.GitClient(readOnly), hardhat: (readOnly: boolean) => new servicesList.HardhatClient(readOnly), + truffle: (readOnly: boolean) => new servicesList.TruffleClient(readOnly), slither: (readOnly: boolean) => new servicesList.SlitherClient(readOnly), folder: (readOnly: boolean) => new servicesList.Sharedfolder(readOnly) } @@ -34,11 +35,12 @@ const ports = { git: 65521, hardhat: 65522, slither: 65523, + truffle: 65524, folder: 65520 } const killCallBack: Array = [] // any is function -function startService (service: S, callback: (ws: WS, sharedFolderClient: servicesList.Sharedfolder, error?:Error) => void) { +function startService (service: S, callback: (ws: WS, sharedFolderClient: servicesList.Sharedfolder, error?:Error) => void) { const socket = new WebSocket(ports[service], { remixIdeUrl: program.remixIde }, () => services[service](program.readOnly || false)) socket.start(callback) killCallBack.push(socket.close.bind(socket)) @@ -100,6 +102,11 @@ function errorHandler (error: any, service: string) { sharedFolderClient.setWebSocket(ws) sharedFolderClient.sharedFolder(program.sharedFolder) }) + + startService('truffle', (ws: WS, sharedFolderClient: servicesList.Sharedfolder) => { + sharedFolderClient.setWebSocket(ws) + sharedFolderClient.sharedFolder(program.sharedFolder) + }) // Run hardhat service if a hardhat project is shared as folder const hardhatConfigFilePath = absolutePath('./', program.sharedFolder) const isHardhatProject = existsSync(hardhatConfigFilePath + '/hardhat.config.js') || existsSync(hardhatConfigFilePath + '/hardhat.config.ts') diff --git a/libs/remixd/src/index.ts b/libs/remixd/src/index.ts index 8b35cea6dd..05195edb3c 100644 --- a/libs/remixd/src/index.ts +++ b/libs/remixd/src/index.ts @@ -2,6 +2,7 @@ import { RemixdClient as sharedFolder } from './services/remixdClient' import { GitClient } from './services/gitClient' import { HardhatClient } from './services/hardhatClient' +import { TruffleClient } from './services/truffleClient' import { SlitherClient } from './services/slitherClient' import Websocket from './websocket' import * as utils from './utils' @@ -13,6 +14,7 @@ module.exports = { sharedFolder, GitClient, HardhatClient, + TruffleClient, SlitherClient } } diff --git a/libs/remixd/src/serviceList.ts b/libs/remixd/src/serviceList.ts index ec8c1f374d..0b71166649 100644 --- a/libs/remixd/src/serviceList.ts +++ b/libs/remixd/src/serviceList.ts @@ -1,4 +1,5 @@ export { RemixdClient as Sharedfolder } from './services/remixdClient' export { GitClient } from './services/gitClient' export { HardhatClient } from './services/hardhatClient' +export { TruffleClient } from './services/truffleClient' export { SlitherClient } from './services/slitherClient' diff --git a/libs/remixd/src/services/truffleClient.ts b/libs/remixd/src/services/truffleClient.ts new file mode 100644 index 0000000000..22e052a257 --- /dev/null +++ b/libs/remixd/src/services/truffleClient.ts @@ -0,0 +1,31 @@ +import * as WS from 'ws' // eslint-disable-line +import { PluginClient } from '@remixproject/plugin' +const { spawn } = require('child_process') // eslint-disable-line + +export class TruffleClient extends PluginClient { + methods: Array + websocket: WS + currentSharedFolder: string + + constructor (private readOnly = false) { + super() + this.methods = ['compile'] + } + + setWebSocket (websocket: WS): void { + this.websocket = websocket + } + + sharedFolder (currentSharedFolder: string): void { + this.currentSharedFolder = currentSharedFolder + } + + compile (configPath: string) { + return new Promise((resolve, reject) => { + if (this.readOnly) { + const errMsg = '[Truffle Compilation]: Cannot compile in read-only mode' + return reject(new Error(errMsg)) + } + }) + } +} diff --git a/libs/remixd/src/websocket.ts b/libs/remixd/src/websocket.ts index 84a0edb7f8..8b57f7311c 100644 --- a/libs/remixd/src/websocket.ts +++ b/libs/remixd/src/websocket.ts @@ -20,7 +20,8 @@ export default class WebSocket { 65520: 'remixd', 65521: 'git', 65522: 'hardhat', - 65523: 'slither' + 65523: 'slither', + 65524: 'truffle' } this.server.on('error', (error: Error) => {