diff --git a/apps/remix-ide/src/app/files/hardhat-handle.js b/apps/remix-ide/src/app/files/hardhat-handle.js new file mode 100644 index 0000000000..9f95211b76 --- /dev/null +++ b/apps/remix-ide/src/app/files/hardhat-handle.js @@ -0,0 +1,18 @@ +import { WebsocketPlugin } from '@remixproject/engine-web' +import * as packageJson from '../../../../../package.json' + +const profile = { + name: 'hardhat', + displayName: 'Hardhat', + url: 'ws://127.0.0.1:65522', + methods: ['compile'], + description: 'Using Remixd daemon, allow to access hardhat API', + kind: 'other', + version: packageJson.version +} + +export class hardhatHandle extends WebsocketPlugin { + constructor () { + super(profile) + } +} diff --git a/apps/remix-ide/src/app/files/remixDProvider.js b/apps/remix-ide/src/app/files/remixDProvider.js index 044e46a165..60337c11e4 100644 --- a/apps/remix-ide/src/app/files/remixDProvider.js +++ b/apps/remix-ide/src/app/files/remixDProvider.js @@ -50,11 +50,6 @@ module.exports = class RemixDProvider extends FileProvider { return this._isReady } - async compileWithHardhat () { - console.log('Inside compileWithHardhat, calling hardhatCompile using _appManager') - return await this._appManager.call('remixd', 'hardhatCompile', {}) - } - close (cb) { this._isReady = false cb() diff --git a/apps/remix-ide/src/app/files/remixd-handle.js b/apps/remix-ide/src/app/files/remixd-handle.js index e8145f58d8..3ec635040c 100644 --- a/apps/remix-ide/src/app/files/remixd-handle.js +++ b/apps/remix-ide/src/app/files/remixd-handle.js @@ -22,7 +22,7 @@ const profile = { name: 'remixd', displayName: 'RemixD', url: 'ws://127.0.0.1:65520', - methods: ['folderIsReadOnly', 'hardhatCompile', 'resolveDirectory', 'get', 'exists', 'isFile', 'set', 'rename', 'remove', 'isDirectory', 'list', 'createDir'], + methods: ['folderIsReadOnly', 'resolveDirectory', 'get', 'exists', 'isFile', 'set', 'rename', 'remove', 'isDirectory', 'list', 'createDir'], events: [], description: 'Using Remixd daemon, allow to access file system', kind: 'other', diff --git a/apps/remix-ide/src/app/tabs/compileTab/compileTab.js b/apps/remix-ide/src/app/tabs/compileTab/compileTab.js index ea7377c3fd..c80514370b 100644 --- a/apps/remix-ide/src/app/tabs/compileTab/compileTab.js +++ b/apps/remix-ide/src/app/tabs/compileTab/compileTab.js @@ -83,7 +83,7 @@ class CompileTab { console.log('mode is - ', this.fileManager.mode) if(this.fileManager.mode === 'localhost') { console.log('calling compilehardhat') - this.fileProvider.compileWithHardhat().then(console.log) + // this.fileProvider.compileWithHardhat().then(console.log) } this.fileManager.saveCurrentFile() this.miscApi.clearAnnotations() diff --git a/libs/remixd/src/bin/remixd.ts b/libs/remixd/src/bin/remixd.ts index 5b7076fd0d..8d2ebe9bb2 100644 --- a/libs/remixd/src/bin/remixd.ts +++ b/libs/remixd/src/bin/remixd.ts @@ -24,16 +24,18 @@ async function warnLatestVersion () { const services = { git: (readOnly: boolean) => new servicesList.GitClient(readOnly), + hardhat: (readOnly: boolean) => new servicesList.HardhatClient(readOnly), folder: (readOnly: boolean) => new servicesList.Sharedfolder(readOnly) } const ports = { git: 65521, + hardhat: 65522, folder: 65520 } const killCallBack: Array = [] -function startService (service: S, callback: (ws: WS, sharedFolderClient: servicesList.Sharedfolder) => void) { +function startService (service: S, callback: (ws: WS, sharedFolderClient: servicesList.Sharedfolder) => void) { const socket = new WebSocket(ports[service], { remixIdeUrl: program.remixIde }, () => services[service](program.readOnly || false)) socket.start(callback) killCallBack.push(socket.close.bind(socket)) @@ -78,6 +80,10 @@ function startService (service: S, callback: (ws: WS sharedFolderClient.setupNotifications(program.sharedFolder) sharedFolderClient.sharedFolder(program.sharedFolder) }) + startService('hardhat', (ws: WS, sharedFolderClient: servicesList.Sharedfolder) => { + sharedFolderClient.setWebSocket(ws) + sharedFolderClient.sharedFolder(program.sharedFolder) + }) /* startService('git', (ws: WS, sharedFolderClient: servicesList.Sharedfolder) => { sharedFolderClient.setWebSocket(ws) diff --git a/libs/remixd/src/index.ts b/libs/remixd/src/index.ts index 04725c14f2..849f35b6fa 100644 --- a/libs/remixd/src/index.ts +++ b/libs/remixd/src/index.ts @@ -1,6 +1,7 @@ 'use strict' import { RemixdClient as sharedFolder } from './services/remixdClient' import { GitClient } from './services/gitClient' +import { HardhatClient } from './services/hardhatClient' import Websocket from './websocket' import * as utils from './utils' @@ -9,6 +10,7 @@ module.exports = { utils, services: { sharedFolder, - GitClient + GitClient, + HardhatClient } } diff --git a/libs/remixd/src/serviceList.ts b/libs/remixd/src/serviceList.ts index 5db445ee66..19d613b7c2 100644 --- a/libs/remixd/src/serviceList.ts +++ b/libs/remixd/src/serviceList.ts @@ -1,2 +1,3 @@ export { RemixdClient as Sharedfolder } from './services/remixdClient' export { GitClient } from './services/gitClient' +export { HardhatClient } from './services/hardhatClient' diff --git a/libs/remixd/src/services/hardhatClient.ts b/libs/remixd/src/services/hardhatClient.ts new file mode 100644 index 0000000000..3b86a2e637 --- /dev/null +++ b/libs/remixd/src/services/hardhatClient.ts @@ -0,0 +1,55 @@ +import * as WS from 'ws' // eslint-disable-line +import { PluginClient } from '@remixproject/plugin' +const { spawn } = require('child_process') + +export class HardhatClient extends PluginClient { + methods: Array + websocket: WS + currentSharedFolder: string + + constructor (private readOnly = false) { + super() + console.log('this is HardhatClient constructor') + this.methods = ['compile'] + } + + setWebSocket (websocket: WS): void { + this.websocket = websocket + } + + sharedFolder (currentSharedFolder: string): void { + this.currentSharedFolder = currentSharedFolder + } + + compile (cmd: string) { + // assertCommand(cmd) + const options = { cwd: this.currentSharedFolder, shell: true } + const child = spawn(cmd, options) + let result = '' + let error = '' + return new Promise((resolve, reject) => { + child.stdout.on('data', (data) => { + console.log('data in compile in HardhatClient', data) + result += data.toString() + }) + child.stderr.on('data', (err) => { + error += err.toString() + }) + child.on('close', () => { + if (error) reject(error) + else resolve(result) + }) + }) + } +} + +/** + * Validate that command can be run by service + * @param cmd + */ +function assertCommand (cmd) { + const regex = '^hardhat\\s[^&|;]*$' + if (!RegExp(regex).test(cmd)) { // git then space and then everything else + throw new Error('Invalid command for service!') + } +} diff --git a/libs/remixd/src/services/remixdClient.ts b/libs/remixd/src/services/remixdClient.ts index 42752c3adc..7fb5062335 100644 --- a/libs/remixd/src/services/remixdClient.ts +++ b/libs/remixd/src/services/remixdClient.ts @@ -15,7 +15,7 @@ export class RemixdClient extends PluginClient { constructor (private readOnly = false) { super() - this.methods = ['folderIsReadOnly', 'hardhatCompile', 'resolveDirectory', 'get', 'exists', 'isFile', 'set', 'rename', 'remove', 'isDirectory', 'list', 'createDir', 'canDeactivate'] + this.methods = ['folderIsReadOnly', 'resolveDirectory', 'get', 'exists', 'isFile', 'set', 'rename', 'remove', 'isDirectory', 'list', 'createDir', 'canDeactivate'] } setWebSocket (websocket: WS): void { @@ -27,12 +27,6 @@ export class RemixdClient extends PluginClient { if (this.isLoaded) this.emit('rootFolderChanged') } - async hardhatCompile() { - console.log('inside hardhatCompile') - console.log('here is hre-->', hre.tasks) - // await hre.tasks.accounts.action(); - } - list (): Filelist { try { return utils.walkSync(this.currentSharedFolder, {}, this.currentSharedFolder)