Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
check for port
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Mar 24, 2022
1 parent 46055c1 commit 0fcbe52
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 14 deletions.
7 changes: 6 additions & 1 deletion package.json
Expand Up @@ -66,6 +66,11 @@
"Christian Bromann <christian@saucelabs.com>"
],
"dependencies": {
"fs-extra": "^10.0.1"
"@types/split2": "^3.2.1",
"@types/tcp-port-used": "^1.0.1",
"@wdio/logger": "^7.19.0",
"fs-extra": "^10.0.1",
"split2": "^4.1.0",
"tcp-port-used": "^1.0.2"
}
}
48 changes: 37 additions & 11 deletions src/launcher.ts
@@ -1,38 +1,64 @@
import fs from 'fs-extra'
import type { ChildProcess } from 'child_process'

import { start, stop } from './safaridriver'
import { spawn, ChildProcess } from 'child_process'
import split2 from 'split2'
import logger from '@wdio/logger'
import tcpPortUsed from 'tcp-port-used'
import { SevereServiceError } from 'webdriverio'

import { getFilePath } from './utils'
import type { ServiceOptions } from './types'

const POLL_INTERVAL = 100
const POLL_TIMEOUT = 10000
const DEFAULT_PORT = 4444

const log = logger('wdio-safaridriver-service')

export default class SafariDriverLauncher {
private _process?: ChildProcess

constructor (private _options: ServiceOptions) {}

public onPrepare () {
public async onPrepare () {
const args = this._options.args || []
const port = this._options.port || DEFAULT_PORT

if (!args.find((arg) => arg.startsWith('-p')) && typeof this._options.port === 'number') {
args.push(`-p ${this._options.port}`)
if (!args.find((arg) => arg.startsWith('-p'))) {
args.push(`-p ${port}`)
}

this._process = start(args)
log.info(`Start Safaridriver with args ${args.join(' ')}`)
this._process = spawn('safaridriver', args)

if (typeof this._options.outputDir === 'string') {
this._redirectLogStream()
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: any) {
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))
}

public onComplete () {
return stop()
if (this._process) {
this._process.kill()
}
}

private _redirectLogStream () {
private async _redirectLogStream () {
const logFile = getFilePath(this._options.outputDir!)
const logStream = fs.createWriteStream(logFile, { flags: 'w' })
await fs.ensureFile(logFile)

const logStream = fs.createWriteStream(logFile, { flags: 'w' })
this._process?.stdout?.pipe(logStream)
this._process?.stderr?.pipe(logStream)
}
Expand Down
50 changes: 48 additions & 2 deletions yarn.lock
Expand Up @@ -392,6 +392,13 @@
dependencies:
"@types/node" "*"

"@types/split2@^3.2.1":
version "3.2.1"
resolved "https://registry.yarnpkg.com/@types/split2/-/split2-3.2.1.tgz#97c42b560a1b41064d46cd728cf1267d5755ee34"
integrity sha512-7uz3yU+LooBq4yNOzlZD9PU9/1Eu0rTD1MjQ6apOVEoHsPrMUrFw7W8XrvWtesm2vK67SBK9AyJcOXtMpl9bgQ==
dependencies:
"@types/node" "*"

"@types/stack-utils@^2.0.0":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.1.tgz#20f18294f797f2209b5f65c8e3b5c8e8261d127c"
Expand All @@ -409,6 +416,11 @@
resolved "https://registry.yarnpkg.com/@types/supports-color/-/supports-color-8.1.1.tgz#1b44b1b096479273adf7f93c75fc4ecc40a61ee4"
integrity sha512-dPWnWsf+kzIG140B8z2w3fr5D03TLWbOAFQl45xUpI3vcizeXriNR5VYkWZ+WTMsUHqZ9Xlt3hrxGNANFyNQfw==

"@types/tcp-port-used@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/tcp-port-used/-/tcp-port-used-1.0.1.tgz#4e15eada6d9f63a5e6c5ef73348d6023f1e91157"
integrity sha512-6pwWTx8oUtWvsiZUCrhrK/53MzKVLnuNSSaZILPy3uMes9QnTrLMar9BDlJArbMOjDcjb3QXFk6Rz8qmmuySZw==

"@types/through@*":
version "0.0.30"
resolved "https://registry.yarnpkg.com/@types/through/-/through-0.0.30.tgz#e0e42ce77e897bd6aead6f6ea62aeb135b8a3895"
Expand Down Expand Up @@ -636,7 +648,7 @@
split2 "^4.0.0"
stream-buffers "^3.0.2"

"@wdio/logger@7.19.0":
"@wdio/logger@7.19.0", "@wdio/logger@^7.19.0":
version "7.19.0"
resolved "https://registry.yarnpkg.com/@wdio/logger/-/logger-7.19.0.tgz#23697a4b4aaea56c3bd477a0393af2a5c175fc85"
integrity sha512-xR7SN/kGei1QJD1aagzxs3KMuzNxdT/7LYYx+lt6BII49+fqL/SO+5X0FDCZD0Ds93AuQvvz9eGyzrBI2FFXmQ==
Expand Down Expand Up @@ -1349,6 +1361,13 @@ debug@4, debug@^4.1.1, debug@^4.3.1, debug@^4.3.2, debug@^4.3.4:
dependencies:
ms "2.1.2"

debug@4.3.1:
version "4.3.1"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.1.tgz#f0d229c505e0c6d8c49ac553d1b13dc183f6b2ee"
integrity sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==
dependencies:
ms "2.1.2"

debug@4.3.3:
version "4.3.3"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.3.tgz#04266e0b70a98d4462e6e288e38259213332b664"
Expand Down Expand Up @@ -2525,6 +2544,11 @@ interpret@^1.0.0:
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.4.0.tgz#665ab8bc4da27a774a40584e812e3e0fa45b1a1e"
integrity sha512-agE4QfB2Lkp9uICn7BAqoscw4SZP9kTE2hxiFI3jBPmXJfdqiahTbUuKGsMoN2GtqL9AxhYioAcVvgsb1HvRbA==

ip-regex@^4.1.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/ip-regex/-/ip-regex-4.3.0.tgz#687275ab0f57fa76978ff8f4dddc8a23d5990db5"
integrity sha512-B9ZWJxHHOHUhUjCPrMpLD4xEq35bUTClHM1S6CBU5ixQnkZmwipwgc96vAd7AAGM9TGHvJR+Uss+/Ak6UphK+Q==

is-arrayish@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d"
Expand Down Expand Up @@ -2711,6 +2735,11 @@ is-unicode-supported@^0.1.0:
resolved "https://registry.yarnpkg.com/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz#3f26c76a809593b52bfa2ecb5710ed2779b522a7"
integrity sha512-knxG2q4UC3u8stRGyAVJCOdxFmv5DZiRcdlIaAQXAbSfJya+OhopNotLQrstBhququ4ZpuKbDc/8S6mgXgPFPw==

is-url@^1.2.4:
version "1.2.4"
resolved "https://registry.yarnpkg.com/is-url/-/is-url-1.2.4.tgz#04a4df46d28c4cff3d73d01ff06abeb318a1aa52"
integrity sha512-ITvGim8FhRiYe4IQ5uHSkj7pVaPDrCTkNd3yq3cV7iZAcJdHTUMPMEHcqSOy9xZ9qFenQCvi+2wjH9a1nXqHww==

is-utf8@^0.2.0:
version "0.2.1"
resolved "https://registry.yarnpkg.com/is-utf8/-/is-utf8-0.2.1.tgz#4b0da1442104d1b336340e80797e865cf39f7d72"
Expand All @@ -2735,6 +2764,15 @@ is-yarn-global@^0.3.0:
resolved "https://registry.yarnpkg.com/is-yarn-global/-/is-yarn-global-0.3.0.tgz#d502d3382590ea3004893746754c89139973e232"
integrity sha512-VjSeb/lHmkoyd8ryPVIKvOCn4D1koMqY+vqyjjUfc3xyKtP4dYOxM44sZrnqQSzSds3xyOrUTLTC9LVCVgLngw==

is2@^2.0.6:
version "2.0.7"
resolved "https://registry.yarnpkg.com/is2/-/is2-2.0.7.tgz#d084e10cab3bd45d6c9dfde7a48599fcbb93fcac"
integrity sha512-4vBQoURAXC6hnLFxD4VW7uc04XiwTTl/8ydYJxKvPwkWQrSjInkuM5VZVg6BGr1/natq69zDuvO9lGpLClJqvA==
dependencies:
deep-is "^0.1.3"
ip-regex "^4.1.0"
is-url "^1.2.4"

isarray@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
Expand Down Expand Up @@ -4172,7 +4210,7 @@ split-on-first@^1.0.0:
resolved "https://registry.yarnpkg.com/split-on-first/-/split-on-first-1.1.0.tgz#f610afeee3b12bce1d0c30425e76398b78249a5f"
integrity sha512-43ZssAJaMusuKWL8sKUBQXHWOpq8d6CfN/u1p4gUzfJkM05C8rxTmYrkIPTXapZpORA6LkkzcUulJ8FqA7Uudw==

split2@^4.0.0:
split2@^4.0.0, split2@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/split2/-/split2-4.1.0.tgz#101907a24370f85bb782f08adaabe4e281ecf809"
integrity sha512-VBiJxFkxiXRlUIeyMQi8s4hgvKCSjtknJv/LVYbrgALPwf5zSKmEwV9Lst25AkvMDnvxODugjdl6KZgwKM1WYQ==
Expand Down Expand Up @@ -4340,6 +4378,14 @@ tar-stream@^2.1.4, tar-stream@^2.2.0:
inherits "^2.0.3"
readable-stream "^3.1.1"

tcp-port-used@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/tcp-port-used/-/tcp-port-used-1.0.2.tgz#9652b7436eb1f4cfae111c79b558a25769f6faea"
integrity sha512-l7ar8lLUD3XS1V2lfoJlCBaeoaWo/2xfYt81hM7VlvR4RrMVFqfmzfhLVk40hAb368uitje5gPtBRL1m/DGvLA==
dependencies:
debug "4.3.1"
is2 "^2.0.6"

text-table@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"
Expand Down

0 comments on commit 0fcbe52

Please sign in to comment.