UNPKG

linguist-js

Version:

Analyse languages used in a folder. Powered by GitHub Linguist, although it doesn't need to be installed.

24 lines (23 loc) 786 B
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = readFileChunk; const fs_1 = __importDefault(require("fs")); /** * Read part of a file on disc. * @throws 'EPERM' if the file is not readable. */ async function readFileChunk(filename, onlyFirstLine = false) { const chunkSize = 100; const stream = fs_1.default.createReadStream(filename, { highWaterMark: chunkSize }); let content = ''; for await (const data of stream) { // may throw content += data.toString(); if (onlyFirstLine) { return content.split(/\r?\n/)[0]; } } return content; }