From 6403bca93b723ae602c7e6196b80f2023023e47a Mon Sep 17 00:00:00 2001 From: Evgeniy Filatov Date: Tue, 7 Aug 2018 12:08:36 +0300 Subject: [PATCH] implemented `execute` terminal command --- src/lib/cmdInterpreterAPI.js | 40 ++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/lib/cmdInterpreterAPI.js b/src/lib/cmdInterpreterAPI.js index 5f1903e83a..092c1da22f 100644 --- a/src/lib/cmdInterpreterAPI.js +++ b/src/lib/cmdInterpreterAPI.js @@ -17,6 +17,7 @@ class CmdInterpreterAPI { self._components.terminal = terminal self._deps = { app: self._components.registry.get('app').api, + fileManager: self._components.registry.get('filemanager').api, editor: self._components.registry.get('editor').api } self.commandHelp = { @@ -24,6 +25,7 @@ class CmdInterpreterAPI { '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.execute(filepath)': 'Run the script specified by file path. If filepath is empty, script currently displayed in the editor is executed.', 'remix.exeCurrent()': 'Run the script currently displayed in the editor', 'remix.help()': 'Display this help message' } @@ -70,14 +72,44 @@ class CmdInterpreterAPI { }) } exeCurrent (cb) { + return this.execute(undefined, cb) + } + execute (file, cb) { const self = this - var content = self._deps.editor.currentContent() - if (!content) { - toolTip('no content to execute') + + function _execute (content, cb) { + if (!content) { + toolTip('no content to execute') + if (cb) cb() + return + } + + self._components.terminal.commands.script(content) + } + + if (typeof file === 'undefined') { + var content = self._deps.editor.currentContent() + _execute(content, cb) + return + } + + var provider = self._deps.fileManager.fileProviderOf(file) + + if (!provider) { + toolTip(`provider for path ${file} not found`) if (cb) cb() return } - self._components.terminal.commands.script(content) + + provider.get(file, (error, content) => { + if (error) { + toolTip(error) + if (cb) cb() + return + } + + _execute(content, cb) + }) } help (cb) { const self = this