Skip to content

Instantly share code, notes, and snippets.

@KSXGitHub
Created March 2, 2020 11:36
Show Gist options
  • Save KSXGitHub/a4980c89d85406066f50b5b84e8e89f0 to your computer and use it in GitHub Desktop.
Save KSXGitHub/a4980c89d85406066f50b5b84e8e89f0 to your computer and use it in GitHub Desktop.
import { createReadStream } from 'fs'
import { createInterface } from 'readline'
const READ_STREAM_OPTIONS = { encoding: 'utf8' } as const
export function firstLine (filename: string) {
const stream = createReadStream(filename, READ_STREAM_OPTIONS)
const reader = createInterface(stream)
let result: string
reader.once('line', line => {
result = line
reader.close()
})
return new Promise<string>(resolve => {
reader.once('close', () => resolve(result))
})
}
export default firstLine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment