UNPKG

wdio-safaridriver-service

Version:
53 lines 2.11 kB
import fs from 'fs-extra'; import split2 from 'split2'; import logger from '@wdio/logger'; import tcpPortUsed from 'tcp-port-used'; import safaridriver, { DEFAULT_PORT } from 'safaridriver'; import { SevereServiceError } from 'webdriverio'; import { getFilePath } from './utils.js'; import { pkg } from './constants.js'; const POLL_INTERVAL = 100; const POLL_TIMEOUT = 10000; const log = logger('wdio-safaridriver-service'); export default class SafariDriverLauncher { constructor(_options, _, _config) { this._options = _options; this._config = _config; log.info(`Initiate Safaridriver Service (v${pkg.version})`); this._outputDir = this._options.outputDir || this._config.outputDir || process.cwd(); } async onPrepare() { const port = this._options.port || DEFAULT_PORT; log.info(`Start Safaridriver on port ${port}`); this._process = safaridriver.start(this._options); if (typeof this._outputDir === 'string') { await this._redirectLogStream(); } else { this._process.stdout?.pipe(split2()).on('data', log.info.bind(this)); this._process.stderr?.pipe(split2()).on('data', log.warn.bind(this)); } try { await tcpPortUsed.waitUntilUsed(port, POLL_INTERVAL, POLL_TIMEOUT); } catch (err) { throw new SevereServiceError(`Couldn't start Safaridriver: ${err.message}`); } process.on('exit', this.onComplete.bind(this)); process.on('SIGINT', this.onComplete.bind(this)); process.on('uncaughtException', this.onComplete.bind(this)); } onComplete() { if (this._process) { this._process.kill(); } } async _redirectLogStream() { const logFile = getFilePath(this._outputDir, this._options.logFileName); await fs.ensureFile(logFile); const logStream = fs.createWriteStream(logFile, { flags: 'w' }); this._process?.stdout?.pipe(logStream); this._process?.stderr?.pipe(logStream); } } //# sourceMappingURL=launcher.js.map