hardhat websocket plugin

pull/1202/head
aniket-engg 4 years ago committed by Aniket
parent 6dfd7d91d9
commit d79bdab5f5
  1. 18
      apps/remix-ide/src/app/files/hardhat-handle.js
  2. 5
      apps/remix-ide/src/app/files/remixDProvider.js
  3. 2
      apps/remix-ide/src/app/files/remixd-handle.js
  4. 2
      apps/remix-ide/src/app/tabs/compileTab/compileTab.js
  5. 8
      libs/remixd/src/bin/remixd.ts
  6. 4
      libs/remixd/src/index.ts
  7. 1
      libs/remixd/src/serviceList.ts
  8. 55
      libs/remixd/src/services/hardhatClient.ts
  9. 8
      libs/remixd/src/services/remixdClient.ts

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

@ -50,11 +50,6 @@ module.exports = class RemixDProvider extends FileProvider {
return this._isReady return this._isReady
} }
async compileWithHardhat () {
console.log('Inside compileWithHardhat, calling hardhatCompile using _appManager')
return await this._appManager.call('remixd', 'hardhatCompile', {})
}
close (cb) { close (cb) {
this._isReady = false this._isReady = false
cb() cb()

@ -22,7 +22,7 @@ const profile = {
name: 'remixd', name: 'remixd',
displayName: 'RemixD', displayName: 'RemixD',
url: 'ws://127.0.0.1:65520', 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: [], events: [],
description: 'Using Remixd daemon, allow to access file system', description: 'Using Remixd daemon, allow to access file system',
kind: 'other', kind: 'other',

@ -83,7 +83,7 @@ class CompileTab {
console.log('mode is - ', this.fileManager.mode) console.log('mode is - ', this.fileManager.mode)
if(this.fileManager.mode === 'localhost') { if(this.fileManager.mode === 'localhost') {
console.log('calling compilehardhat') console.log('calling compilehardhat')
this.fileProvider.compileWithHardhat().then(console.log) // this.fileProvider.compileWithHardhat().then(console.log)
} }
this.fileManager.saveCurrentFile() this.fileManager.saveCurrentFile()
this.miscApi.clearAnnotations() this.miscApi.clearAnnotations()

@ -24,16 +24,18 @@ async function warnLatestVersion () {
const services = { const services = {
git: (readOnly: boolean) => new servicesList.GitClient(readOnly), git: (readOnly: boolean) => new servicesList.GitClient(readOnly),
hardhat: (readOnly: boolean) => new servicesList.HardhatClient(readOnly),
folder: (readOnly: boolean) => new servicesList.Sharedfolder(readOnly) folder: (readOnly: boolean) => new servicesList.Sharedfolder(readOnly)
} }
const ports = { const ports = {
git: 65521, git: 65521,
hardhat: 65522,
folder: 65520 folder: 65520
} }
const killCallBack: Array<Function> = [] const killCallBack: Array<Function> = []
function startService<S extends 'git' | 'folder'> (service: S, callback: (ws: WS, sharedFolderClient: servicesList.Sharedfolder) => void) { function startService<S extends 'git' | 'hardhat' | 'folder'> (service: S, callback: (ws: WS, sharedFolderClient: servicesList.Sharedfolder) => void) {
const socket = new WebSocket(ports[service], { remixIdeUrl: program.remixIde }, () => services[service](program.readOnly || false)) const socket = new WebSocket(ports[service], { remixIdeUrl: program.remixIde }, () => services[service](program.readOnly || false))
socket.start(callback) socket.start(callback)
killCallBack.push(socket.close.bind(socket)) killCallBack.push(socket.close.bind(socket))
@ -78,6 +80,10 @@ function startService<S extends 'git' | 'folder'> (service: S, callback: (ws: WS
sharedFolderClient.setupNotifications(program.sharedFolder) sharedFolderClient.setupNotifications(program.sharedFolder)
sharedFolderClient.sharedFolder(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) => { startService('git', (ws: WS, sharedFolderClient: servicesList.Sharedfolder) => {
sharedFolderClient.setWebSocket(ws) sharedFolderClient.setWebSocket(ws)

@ -1,6 +1,7 @@
'use strict' 'use strict'
import { RemixdClient as sharedFolder } from './services/remixdClient' import { RemixdClient as sharedFolder } from './services/remixdClient'
import { GitClient } from './services/gitClient' import { GitClient } from './services/gitClient'
import { HardhatClient } from './services/hardhatClient'
import Websocket from './websocket' import Websocket from './websocket'
import * as utils from './utils' import * as utils from './utils'
@ -9,6 +10,7 @@ module.exports = {
utils, utils,
services: { services: {
sharedFolder, sharedFolder,
GitClient GitClient,
HardhatClient
} }
} }

@ -1,2 +1,3 @@
export { RemixdClient as Sharedfolder } from './services/remixdClient' export { RemixdClient as Sharedfolder } from './services/remixdClient'
export { GitClient } from './services/gitClient' export { GitClient } from './services/gitClient'
export { HardhatClient } from './services/hardhatClient'

@ -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<string>
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!')
}
}

@ -15,7 +15,7 @@ export class RemixdClient extends PluginClient {
constructor (private readOnly = false) { constructor (private readOnly = false) {
super() 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 { setWebSocket (websocket: WS): void {
@ -27,12 +27,6 @@ export class RemixdClient extends PluginClient {
if (this.isLoaded) this.emit('rootFolderChanged') 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 { list (): Filelist {
try { try {
return utils.walkSync(this.currentSharedFolder, {}, this.currentSharedFolder) return utils.walkSync(this.currentSharedFolder, {}, this.currentSharedFolder)

Loading…
Cancel
Save