Files
runner-container-hooks/packages/hooklib/src/utils.ts
Thomas Boop 47cbf5a0d7 Misc Tracing cleanup (#15)
* cleanup final bits

* fix import
2022-06-15 09:28:43 -04:00

43 lines
1.0 KiB
TypeScript

import * as events from 'events'
import * as fs from 'fs'
import * as os from 'os'
import * as readline from 'readline'
import { HookData } from './interfaces'
export async function getInputFromStdin(): Promise<HookData> {
let input = ''
const rl = readline.createInterface({
input: process.stdin
})
rl.on('line', line => {
input = line
})
await events.default.once(rl, 'close')
const inputJson = JSON.parse(input)
return inputJson as HookData
}
export function writeToResponseFile(filePath: string, message: any): void {
if (!filePath) {
throw new Error(`Expected file path`)
}
if (!fs.existsSync(filePath)) {
throw new Error(`Missing file at path: ${filePath}`)
}
fs.appendFileSync(filePath, `${toCommandValue(message)}${os.EOL}`, {
encoding: 'utf8'
})
}
function toCommandValue(input: any): string {
if (input === null || input === undefined) {
return ''
} else if (typeof input === 'string' || input instanceof String) {
return input as string
}
return JSON.stringify(input)
}