diff --git a/apps/remix-ide/src/app/tabs/intelligent-script-executor.js b/apps/remix-ide/src/app/tabs/intelligent-script-executor.js deleted file mode 100644 index a6736c8b1c..0000000000 --- a/apps/remix-ide/src/app/tabs/intelligent-script-executor.js +++ /dev/null @@ -1,38 +0,0 @@ -import { Plugin } from '@remixproject/engine' -import * as packageJson from '../../../../../package.json' - -export const profile = { - name: 'intelligentScriptExecutor', - displayName: 'Intelligent Script Executor', - description: 'after each compilation, run the script defined in Natspec.', - methods: [], - version: packageJson.version, - kind: 'none' -} - -export class IntelligentScriptExecutor extends Plugin { - constructor () { - super(profile) - } - - onActivation () { - let listen = false - setTimeout(() => { - listen = true - }, 500) - this.on('compilerMetadata', 'artefactsUpdated', async (fileName, contract) => { - if (!listen) return - if (contract.object && contract.object.devdoc['custom:dev-run-script']) { - const text = contract.object.devdoc['custom:dev-run-script'] - await this.call('terminal', 'log', `running ${text} ...`) - const content = await this.call('fileManager', 'readFile', text) - await this.call('udapp', 'clearAllInstances') - await this.call('scriptRunner', 'execute', content) - } - }) - } - - onDeactivation () { - this.off('solidity', 'compilationFinished') - } -} diff --git a/apps/remix-ide/src/app/tabs/intelligent-script-executor.ts b/apps/remix-ide/src/app/tabs/intelligent-script-executor.ts new file mode 100644 index 0000000000..c524b36137 --- /dev/null +++ b/apps/remix-ide/src/app/tabs/intelligent-script-executor.ts @@ -0,0 +1,60 @@ +import { Plugin } from '@remixproject/engine' +import * as packageJson from '../../../../../package.json' + +export const profile = { + name: 'intelligentScriptExecutor', + displayName: 'Intelligent Script Executor', + description: 'after each compilation, run the script defined in Natspec.', + methods: [], + version: packageJson.version, + kind: 'none' +} + +type listener = (event: KeyboardEvent) => void + +export class IntelligentScriptExecutor extends Plugin { + executionListener: listener + targetFileName: string + + constructor () { + super(profile) + this.executionListener = async (e) => { + // ctrl+e or command+e + const file = await this.call('fileManager', 'file') + if ((e.metaKey || e.ctrlKey) && e.keyCode === 69 && file !== '') { + if (file.endsWith('.sol')) { + e.preventDefault() + this.targetFileName = file + await this.call('solidity', 'compile', file) + } else if (file.endsWith('.js')) { + e.preventDefault() + this.runScript(file) + } + } + } + } + + async runScript (fileName) { + await this.call('terminal', 'log', `running ${fileName} ...`) + const content = await this.call('fileManager', 'readFile', fileName) + await this.call('udapp', 'clearAllInstances') + await this.call('scriptRunner', 'execute', content) + } + + onActivation () { + window.document.addEventListener('keydown', this.executionListener) + + this.on('compilerMetadata', 'artefactsUpdated', async (fileName, contract) => { + if (this.targetFileName === contract.file && contract.object && contract.object.devdoc['custom:dev-run-script']) { + const file = contract.object.devdoc['custom:dev-run-script'] + if (file) this.runScript(file) + } + this.targetFileName = null + }) + } + + onDeactivation () { + window.document.removeEventListener('keydown', this.executionListener) + this.off('compilerMetadata', 'artefactsUpdated') + } +}