forked from XinFinOrg/XDCremix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdInterpreterAPI.js
94 lines (91 loc) · 2.85 KB
/
cmdInterpreterAPI.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'use strict'
var yo = require('yo-yo')
var async = require('async')
var remixLib = require('remix-lib')
var EventManager = remixLib.EventManager
var executionContext = require('../execution-context')
var toolTip = require('../app/ui/tooltip')
var globalRegistry = require('../global/registry')
class CmdInterpreterAPI {
constructor (terminal, localRegistry) {
const self = this
self.event = new EventManager()
self._components = {}
self._components.registry = localRegistry || globalRegistry
self._components.terminal = terminal
self._deps = {
app: self._components.registry.get('app').api,
editor: self._components.registry.get('editor').api
}
self.commandHelp = {
'remix.debug(hash)': 'Start debugging a transaction.',
'remix.loadgist(id)': 'Load a gist in the file explorer.',
'remix.loadurl(url)': 'Load the given url in the file explorer. The url can be of type github, swarm or ipfs.',
'remix.setproviderurl(url)': 'Change the current provider to Web3 provider and set the url endpoint.',
'remix.exeCurrent()': 'Run the script currenttly displayed in the editor',
'remix.help()': 'Display this help message'
}
}
debug (hash, cb) {
const self = this
self._deps.app.startdebugging(hash)
if (cb) cb()
}
loadgist (id, cb) {
const self = this
self._deps.app.loadFromGist({gist: id})
if (cb) cb()
}
loadurl (url, cb) {
const self = this
self._deps.app.importExternal(url, (err, content) => {
if (err) {
toolTip(`Unable to load ${url}: ${err}`)
if (cb) cb(err)
} else {
try {
content = JSON.parse(content)
async.eachOfSeries(content.sources, (value, file, callbackSource) => {
var url = value.urls[0] // @TODO retrieve all other contents ?
self._deps.app.importExternal(url, (error, content) => {
if (error) {
toolTip(`Cannot retrieve the content of ${url}: ${error}`)
}
callbackSource()
})
}, (error) => {
if (cb) cb(error)
})
} catch (e) {}
if (cb) cb()
}
})
}
setproviderurl (url, cb) {
executionContext.setProviderFromEndpoint(url, 'web3', (error) => {
if (error) toolTip(error)
if (cb) cb()
})
}
exeCurrent (cb) {
const self = this
var content = self._deps.editor.currentContent()
if (!content) {
toolTip('no content to execute')
if (cb) cb()
return
}
self._components.terminal.commands.script(content)
}
help (cb) {
const self = this
var help = yo`<div></div>`
for (var k in self.commandHelp) {
help.appendChild(yo`<div>${k}: ${self.commandHelp[k]}</div>`)
}
self._components.terminal.commands.html(help)
if (cb) cb()
return ''
}
}
module.exports = CmdInterpreterAPI