UNPKG

linguist-js

Version:

Analyse the programming languages used in a folder or from raw content, using the same rules that GitHub Linguist does.

18 lines (17 loc) 549 B
import FS from 'node:fs'; /** * Read part of a file on disc. * @throws 'EPERM' if the file is not readable. */ export default async function readFileChunk(filename, onlyFirstLine = false) { const chunkSize = 100; const stream = FS.createReadStream(filename, { highWaterMark: chunkSize }); let content = ''; for await (const data of stream) { // may throw content += data.toString(); if (onlyFirstLine && content.includes('\n')) { return content.split(/\r?\n/)[0]; } } return content; }