test-openapi
Version:
Automated client requests
66 lines (52 loc) • 1.29 kB
JavaScript
const { stdout } = require('process')
const { promisify } = require('util')
const { result } = require('../../utils')
// Call reporters' functions then write return value to output
const callReporters = async function({ reporters, type }, ...args) {
const promises = reporters.map(reporter =>
callReporter({ reporter, type }, ...args),
)
await Promise.all(promises)
}
const callReporter = async function(
{
reporter,
reporter: {
options,
options: { output },
},
type,
},
...args
) {
if (reporter[type] === undefined) {
return
}
const argsA = getArgs({ args, options })
const message = await reporter[type](...argsA)
if (message !== undefined) {
output.write(message)
}
if (type === 'end') {
await endReporting({ output })
}
}
const getArgs = function({ args, options }) {
const [argA, context] = args.map(arg => result(arg, { options }))
const argsA = [argA, { ...context, options }].filter(
argB => argB !== undefined,
)
return argsA
}
const endReporting = async function({ output }) {
// Give enough time for `output` stream to be flushed
await promisify(setTimeout)()
if (output === stdout) {
return
}
output.destroy()
}
module.exports = {
callReporters,
}