Created
March 2, 2020 11:36
-
-
Save KSXGitHub/a4980c89d85406066f50b5b84e8e89f0 to your computer and use it in GitHub Desktop.
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
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