linguist-js
Version:
Analyse the programming languages used in a folder or from raw content, using the same rules that GitHub Linguist does.
24 lines (23 loc) • 812 B
JavaScript
;
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 && content.includes('\n')) {
return content.split(/\r?\n/)[0];
}
}
return content;
}