text: import fs from 'fs-extra'
  import path from 'path'
  
  import Launcher from './../launcher'
  import Watcher from './../watcher'
  import { missingConfigurationPrompt } from '../utils'
  
  export const command = 'run <configPath>'
  
  export const desc = 'Run your WDIO configuration file to initialize your tests.'
  
  export const builder = {
      watch: {
          desc: 'Run WebdriverIO in watch mode',
          type: 'boolean',
      },
      hostname: {
          alias: 'h',
          desc: 'automation driver host address',
          type: 'string'
      },
      port: {
          alias: 'p',
          desc: 'automation driver port',
          type: 'number'
      },
      user: {
          alias: 'u',
          desc: 'username if using a cloud service as automation backend',
          type: 'string'
      },
      key: {
          alias: 'k',
          desc: 'corresponding access key to the user',
          type: 'string'
      },
      logLevel: {
          alias: 'l',
          desc: 'level of logging verbosity',
          choices: ['trace', 'debug', 'info', 'warn', 'error', 'silent']
      },
      bail: {
          desc: 'stop test runner after specific amount of tests have failed',
          type: 'number'
      },
      baseUrl: {
          desc: 'shorten url command calls by setting a base url',
          type: 'string'
      },
      waitforTimeout: {
          alias: 'w',
          desc: 'timeout for all waitForXXX commands',
          type: 'number'
      },
      framework: {
          alias: 'f',
          desc: 'defines the framework (Mocha, Jasmine or Cucumber) to run the specs',
          type: 'string'
      },
      reporters: {
          alias: 'r',
          desc: 'reporters to print out the results on stdout',
          type: 'array'
      },
      suite: {
          desc: 'overwrites the specs attribute and runs the defined suite',
          type: 'array'
      },
      spec: {
          desc: 'run only a certain spec file - overrides specs piped from stdin',
          type: 'array'
      },
      exclude: {
          desc: 'exclude certain spec file from the test run - overrides exclude piped from stdin',
          type: 'array'
      },
      mochaOpts: {
          desc: 'Mocha options'
      },
      jasmineOpts: {
          desc: 'Jasmine options'
      },
      cucumberOpts: {
          desc: 'Cucumber options'
      }
  }
  
  export function launchWithStdin(wdioConfPath, params) {
      let stdinData = ''
      const stdin = process.openStdin()
  
      stdin.setEncoding('utf8')
      stdin.on('data', (data) => {
          stdinData += data
      })
      stdin.on('end', () => {
          if (stdinData.length > 0) {
              params.specs = stdinData.trim().split(/\r?\n/)
          }
          launch(wdioConfPath, params)
      })
  }
  
  export function launch(wdioConfPath, params) {
      const launcher = new Launcher(wdioConfPath, params)
      return launcher.run()
          .then(process.exit)
          .catch(err => {
              console.error(err)
              process.exit(1)
          })
  }
  
  export async function handler(argv) {
      const { configPath, ...params } = argv
  
      if (!fs.existsSync(configPath)) {
          await missingConfigurationPrompt('run', `No WebdriverIO configuration found in "${configPath}"`)
      }
  
      const localConf = path.join(process.cwd(), 'wdio.conf.js')
      const wdioConf = configPath || (fs.existsSync(localConf) ? localConf : null)
  
      /**
       * if `--watch` param is set, run launcher in watch mode
       */
      if (params.watch) {
          const watcher = new Watcher(wdioConf, params)
          return watcher.watch()
      }
  
      /**
       * if stdin.isTTY, then no piped input is present and launcher should be
       * called immediately, otherwise piped input is processed, expecting
       * a list of files to process.
       *
       * stdin.isTTY is false when command is from nodes spawn since it's treated as a pipe
       */
      if (process.stdin.isTTY || !process.stdout.isTTY) {
          return launch(wdioConf, params)
      }
  
      /*
       * get a list of spec files to run from stdin, overriding any other
       * configuration suite or specs.
       */
      launchWithStdin(wdioConf, params)
  }
  