Skip to content

Commit

Permalink
support standalone repl
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Dec 8, 2016
1 parent f553c25 commit 89c584a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
54 changes: 40 additions & 14 deletions lib/commands/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@

import vm from 'vm'
import repl from 'repl'
import { RuntimeError } from '../utils/ErrorHandler'

let debug = function (enableStdout, enableLogging) {
let debug = function (commandTimeout = 5000, enableStdout, enableLogging) {
let commandIsRunning = false
let logLevel = this.logger.logLevel
this.logger.logLevel = 'verbose'
Expand All @@ -39,6 +40,44 @@ let debug = function (enableStdout, enableLogging) {
this.logger.logLevel = logLevel
}

const myEval = (cmd, context, filename, callback) => {
if (commandIsRunning) {
return
}

if (cmd === 'browser\n') {
return callback(null, '[WebdriverIO REPL client]')
}

commandIsRunning = true
if (typeof global.wdioSync === 'function') {
return global.wdioSync(() => {
vm.runInThisContext(`global._result = (function () { return ${cmd} }).apply(this)`)
callback(null, global._result)
commandIsRunning = false
})()
}

context.browser = this
vm.runInThisContext(`global._result = (function () { return ${cmd} }).apply(this)`)

if (!global._result || typeof global._result.then !== 'function') {
commandIsRunning = false
return callback(null, global._result)
}

const timeout = setTimeout(() => callback(new RuntimeError('Command execution timed out')), commandTimeout)
global._result.then((res) => {
commandIsRunning = false
clearTimeout(timeout)
return callback(null, res)
}, (e) => {
commandIsRunning = false
clearTimeout(timeout)
return callback(e)
})
}

const replServer = repl.start({
prompt: '> ',
eval: myEval,
Expand All @@ -48,19 +87,6 @@ let debug = function (enableStdout, enableLogging) {
ignoreUndefined: true
})

function myEval (cmd, context, filename, callback) {
if (commandIsRunning) {
return
}

commandIsRunning = true
global.wdioSync(() => {
vm.runInThisContext(`global._result = (function () { return ${cmd} }).apply(this)`)
callback(null, global._result)
commandIsRunning = false
})()
}

return new Promise((resolve) => {
replServer.on('exit', () => {
this.logger.logLevel = logLevel
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ class Logger {
* debugger info message
*/
debug () {
this.write('\n')
this.write('')
this.log(`${COLORS.yellow}DEBUG\t${COLORS.reset}Queue has stopped!`)
this.log(`${COLORS.yellow}DEBUG\t${COLORS.reset}You can now go into the browser or use the command line as REPL`)
this.log(`${COLORS.yellow}DEBUG\t${COLORS.dkgray}(To exit, press ^C again or type .exit)${COLORS.reset}`)
this.log(`${COLORS.yellow}DEBUG\t${COLORS.dkgray}(To exit, press ^C again or type .exit)${COLORS.reset}\n`)
}

/**
Expand Down

0 comments on commit 89c584a

Please sign in to comment.