-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a function to match the output of a process.
- Loading branch information
1 parent
a91145b
commit 388f0c7
Showing
10 changed files
with
129 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
|
||
export * as browser from './browser/index.js' | ||
export * as node from './node/index.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
|
||
export { matchOutput } from './match-output.js' | ||
export { waitForOutput } from './wait-for-output.js' | ||
export { default as execa } from './execa.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import pDefer from 'p-defer' | ||
import { toString as uint8ArrayToString } from 'uint8arrays/to-string' | ||
import execaUtil from './execa.js' | ||
import type { DefaultEncodingOption, EncodingOption, ExecaChildProcess, Options } from 'execa' | ||
|
||
export interface WaitForOutputOptions<EncodingType extends EncodingOption = DefaultEncodingOption> extends Options<EncodingType> { | ||
timeout?: number | ||
} | ||
|
||
export interface MatchOutputResult { | ||
matches: string[] | ||
process: ExecaChildProcess<string> | ||
} | ||
|
||
/** | ||
* Starts a process and matches the output against the passed regex. The match | ||
* result and process are returned, the user must manually kill the process | ||
* when they are finished with it. | ||
*/ | ||
export async function matchOutput (matcher: RegExp, command: string, args: string[] = [], opts: WaitForOutputOptions = {}): Promise<MatchOutputResult> { | ||
const foundMatch = pDefer<string[]>() | ||
|
||
const proc = execaUtil(command, args, { ...opts, all: true }, (exec) => { | ||
exec.all?.on('data', (data) => { | ||
process.stdout.write(data) | ||
output += uint8ArrayToString(data) | ||
|
||
const matches = matcher.exec(output) | ||
|
||
if (matches == null) { | ||
return | ||
} | ||
|
||
foundExpectedOutput = true | ||
foundMatch.resolve(matches) | ||
cancelTimeout() | ||
}) | ||
}) | ||
|
||
let output = '' | ||
const time = opts.timeout ?? 120000 | ||
|
||
let foundExpectedOutput = false | ||
let cancelTimeout = (): void => {} | ||
const timeoutPromise = new Promise<void>((resolve, reject) => { | ||
const timeout = setTimeout(() => { | ||
reject( | ||
new Error( | ||
`Did not match "${matcher}" in output from "${[command] | ||
.concat(args) | ||
.join(' ')}" after ${time / 1000}s` | ||
) | ||
) | ||
|
||
setTimeout(() => { | ||
proc.kill() | ||
}, 100) | ||
}, time) | ||
|
||
cancelTimeout = () => { | ||
clearTimeout(timeout) | ||
resolve() | ||
} | ||
}) | ||
|
||
let result: string[] | undefined | ||
|
||
try { | ||
const res = await Promise.race([foundMatch.promise, timeoutPromise]) | ||
|
||
if (res != null) { | ||
result = res | ||
} | ||
} catch (err: any) { | ||
if (err.killed == null) { | ||
throw err | ||
} | ||
} | ||
|
||
if (!foundExpectedOutput) { | ||
cancelTimeout() | ||
throw new Error( | ||
`Did not match "${matcher}" in output from "${[command] | ||
.concat(args) | ||
.join(' ')}"` | ||
) | ||
} | ||
|
||
if (result == null) { | ||
throw new Error('Process output was undefined') | ||
} | ||
|
||
return { | ||
process: proc, | ||
matches: result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { matchOutput } from '../../../dist/src/node/index.js' | ||
|
||
const result = await matchOutput(/(Here is some input)/mg, './test/fixtures/node/index.js') | ||
|
||
result.process.kill() |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters