From f636f7ee05a33b335277867c1795f999f2f3daa8 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 16 Jul 2018 15:48:51 +0200 Subject: [PATCH] use proper javascript object for scripting --- src/app/panels/editor-panel.js | 4 +--- src/app/panels/terminal.js | 4 +++- src/lib/cmdInterpreter.js | 27 --------------------------- src/lib/cmdInterpreterAPI.js | 18 ++++-------------- 4 files changed, 8 insertions(+), 45 deletions(-) delete mode 100644 src/lib/cmdInterpreter.js diff --git a/src/app/panels/editor-panel.js b/src/app/panels/editor-panel.js index 5def14fd02..45fab49aee 100644 --- a/src/app/panels/editor-panel.js +++ b/src/app/panels/editor-panel.js @@ -7,7 +7,6 @@ var Terminal = require('./terminal') var Editor = require('../editor/editor') var globalRegistry = require('../../global/registry') -var CommandInterpreter = require('../../lib/cmdInterpreter') var ContextualListener = require('../editor/contextualListener') var ContextView = require('../editor/contextView') var styles = require('./styles/editor-panel-styles') @@ -47,8 +46,7 @@ class EditorPanel { contextualListener: contextualListener, contextView: new ContextView({contextualListener: contextualListener, editor: editor}), terminal: new Terminal({ - udapp: self._deps.udapp, - cmdInterpreter: new CommandInterpreter() + udapp: self._deps.udapp }, { getPosition: (event) => { diff --git a/src/app/panels/terminal.js b/src/app/panels/terminal.js index 5dfc88600b..bc253ca8da 100644 --- a/src/app/panels/terminal.js +++ b/src/app/panels/terminal.js @@ -8,6 +8,7 @@ var remixLib = require('remix-lib') var EventManager = remixLib.EventManager var Web3 = require('web3') +var CommandInterpreterAPI = require('../../lib/cmdInterpreterAPI') var executionContext = require('../../execution-context') 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._components = {} + self._components.cmdInterpreter = new CommandInterpreterAPI(this) self._components.dropdown = new Dropdown({ options: [ 'only remix transactions', @@ -74,7 +76,6 @@ class Terminal { self.registerCommand('script', function execute (args, scopedCommands, append) { var script = String(args[0]) scopedCommands.log(`> ${script}`) - if (self._opts.cmdInterpreter && self._opts.cmdInterpreter.interpret(script)) return self._shell(script, scopedCommands, function (error, output) { if (error) scopedCommands.error(error) else scopedCommands.log(output) @@ -561,6 +562,7 @@ class Terminal { function domTerminalFeatures (self, scopedCommands) { return { + remix: self._components.cmdInterpreter, web3: executionContext.getProvider() !== 'vm' ? new Web3(executionContext.web3().currentProvider) : null, console: { log: function () { scopedCommands.log.apply(scopedCommands, arguments) }, diff --git a/src/lib/cmdInterpreter.js b/src/lib/cmdInterpreter.js deleted file mode 100644 index 66af66236a..0000000000 --- a/src/lib/cmdInterpreter.js +++ /dev/null @@ -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 diff --git a/src/lib/cmdInterpreterAPI.js b/src/lib/cmdInterpreterAPI.js index bea077d136..23c3bc3311 100644 --- a/src/lib/cmdInterpreterAPI.js +++ b/src/lib/cmdInterpreterAPI.js @@ -8,12 +8,12 @@ var toolTip = require('../app/ui/tooltip') var globalRegistry = require('../global/registry') class CmdInterpreterAPI { - constructor (cmdInterpreter, localRegistry) { + constructor (terminal, localRegistry) { const self = this self.event = new EventManager() self._components = {} self._components.registry = localRegistry || globalRegistry - self._components.cmdInterpreter = cmdInterpreter + self._components.terminal = terminal self._deps = { app: self._components.registry.get('app').api, editor: self._components.registry.get('editor').api @@ -60,7 +60,7 @@ class CmdInterpreterAPI { if (cb) cb() }) } - batch (url, cb) { + exeCurrent (cb) { const self = this var content = self._deps.editor.currentContent() if (!content) { @@ -68,17 +68,7 @@ class CmdInterpreterAPI { if (cb) cb() return } - var split = content.split('\n') - 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() - }) + self._components.terminal.commands.script(content) } }