use proper javascript object for scripting

pull/1/head
yann300 6 years ago
parent c745cdb0e9
commit f636f7ee05
  1. 4
      src/app/panels/editor-panel.js
  2. 4
      src/app/panels/terminal.js
  3. 27
      src/lib/cmdInterpreter.js
  4. 18
      src/lib/cmdInterpreterAPI.js

@ -7,7 +7,6 @@ var Terminal = require('./terminal')
var Editor = require('../editor/editor') var Editor = require('../editor/editor')
var globalRegistry = require('../../global/registry') var globalRegistry = require('../../global/registry')
var CommandInterpreter = require('../../lib/cmdInterpreter')
var ContextualListener = require('../editor/contextualListener') var ContextualListener = require('../editor/contextualListener')
var ContextView = require('../editor/contextView') var ContextView = require('../editor/contextView')
var styles = require('./styles/editor-panel-styles') var styles = require('./styles/editor-panel-styles')
@ -47,8 +46,7 @@ class EditorPanel {
contextualListener: contextualListener, contextualListener: contextualListener,
contextView: new ContextView({contextualListener: contextualListener, editor: editor}), contextView: new ContextView({contextualListener: contextualListener, editor: editor}),
terminal: new Terminal({ terminal: new Terminal({
udapp: self._deps.udapp, udapp: self._deps.udapp
cmdInterpreter: new CommandInterpreter()
}, },
{ {
getPosition: (event) => { getPosition: (event) => {

@ -8,6 +8,7 @@ var remixLib = require('remix-lib')
var EventManager = remixLib.EventManager var EventManager = remixLib.EventManager
var Web3 = require('web3') var Web3 = require('web3')
var CommandInterpreterAPI = require('../../lib/cmdInterpreterAPI')
var executionContext = require('../../execution-context') var executionContext = require('../../execution-context')
var Dropdown = require('../ui/dropdown') var Dropdown = require('../ui/dropdown')
@ -37,6 +38,7 @@ class Terminal {
} }
self._view = { el: null, bar: null, input: null, term: null, journal: null, cli: null } self._view = { el: null, bar: null, input: null, term: null, journal: null, cli: null }
self._components = {} self._components = {}
self._components.cmdInterpreter = new CommandInterpreterAPI(this)
self._components.dropdown = new Dropdown({ self._components.dropdown = new Dropdown({
options: [ options: [
'only remix transactions', 'only remix transactions',
@ -74,7 +76,6 @@ class Terminal {
self.registerCommand('script', function execute (args, scopedCommands, append) { self.registerCommand('script', function execute (args, scopedCommands, append) {
var script = String(args[0]) var script = String(args[0])
scopedCommands.log(`> ${script}`) scopedCommands.log(`> ${script}`)
if (self._opts.cmdInterpreter && self._opts.cmdInterpreter.interpret(script)) return
self._shell(script, scopedCommands, function (error, output) { self._shell(script, scopedCommands, function (error, output) {
if (error) scopedCommands.error(error) if (error) scopedCommands.error(error)
else scopedCommands.log(output) else scopedCommands.log(output)
@ -561,6 +562,7 @@ class Terminal {
function domTerminalFeatures (self, scopedCommands) { function domTerminalFeatures (self, scopedCommands) {
return { return {
remix: self._components.cmdInterpreter,
web3: executionContext.getProvider() !== 'vm' ? new Web3(executionContext.web3().currentProvider) : null, web3: executionContext.getProvider() !== 'vm' ? new Web3(executionContext.web3().currentProvider) : null,
console: { console: {
log: function () { scopedCommands.log.apply(scopedCommands, arguments) }, log: function () { scopedCommands.log.apply(scopedCommands, arguments) },

@ -1,27 +0,0 @@
'use strict'
var remixLib = require('remix-lib')
var EventManager = remixLib.EventManager
var CommandInterpreterAPI = require('./cmdInterpreterAPI')
class CmdInterpreter {
constructor () {
this.event = new EventManager()
this.api = new CommandInterpreterAPI(this)
}
interpret (cmd, cb) {
if (!cmd) return false
var accept = commandsRegEx.exec(cmd)
if (accept) {
var param = accept[2]
if (param) param = param.trim()
this.api[accept[1]](param, cb)
return accept[1]
}
return null
}
}
var commandsRegEx = /^remix:(debug|loadgist|setproviderurl|loadurl|batch)(.*)/
module.exports = CmdInterpreter

@ -8,12 +8,12 @@ var toolTip = require('../app/ui/tooltip')
var globalRegistry = require('../global/registry') var globalRegistry = require('../global/registry')
class CmdInterpreterAPI { class CmdInterpreterAPI {
constructor (cmdInterpreter, localRegistry) { constructor (terminal, localRegistry) {
const self = this const self = this
self.event = new EventManager() self.event = new EventManager()
self._components = {} self._components = {}
self._components.registry = localRegistry || globalRegistry self._components.registry = localRegistry || globalRegistry
self._components.cmdInterpreter = cmdInterpreter self._components.terminal = terminal
self._deps = { self._deps = {
app: self._components.registry.get('app').api, app: self._components.registry.get('app').api,
editor: self._components.registry.get('editor').api editor: self._components.registry.get('editor').api
@ -60,7 +60,7 @@ class CmdInterpreterAPI {
if (cb) cb() if (cb) cb()
}) })
} }
batch (url, cb) { exeCurrent (cb) {
const self = this const self = this
var content = self._deps.editor.currentContent() var content = self._deps.editor.currentContent()
if (!content) { if (!content) {
@ -68,17 +68,7 @@ class CmdInterpreterAPI {
if (cb) cb() if (cb) cb()
return return
} }
var split = content.split('\n') self._components.terminal.commands.script(content)
async.eachSeries(split, (value, cb) => {
if (!self._components.cmdInterpreter.interpret(value, (error) => {
error ? cb(`Cannot run ${value}. stopping`) : cb()
})) {
cb(`Cannot interpret ${value}. stopping`)
}
}, (error) => {
if (error) toolTip(error)
if (cb) cb()
})
} }
} }

Loading…
Cancel
Save