UNPKG

linguist-js

Version:

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

38 lines (37 loc) 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = parseAttributes; const norm_path_1 = require("./norm-path"); /** * Parses a gitattributes file. */ function parseAttributes(content, folderRoot = '.') { var _a, _b; const output = []; for (const rawLine of content.split('\n')) { const line = rawLine.replace(/#.*/, '').trim(); if (!line) continue; const parts = line.split(/\s+/g); const fileGlob = parts[0]; const relFileGlob = (0, norm_path_1.normPath)(folderRoot, fileGlob); const attrParts = parts.slice(1); const isTrue = (str) => !str.startsWith('-') && !str.endsWith('=false'); const isFalse = (str) => str.startsWith('-') || str.endsWith('=false'); const trueParts = (str) => attrParts.filter(part => part.includes(str) && isTrue(part)); const falseParts = (str) => attrParts.filter(part => part.includes(str) && isFalse(part)); const hasTrueParts = (str) => trueParts(str).length > 0; const hasFalseParts = (str) => falseParts(str).length > 0; const boolOrNullVal = (str) => hasTrueParts(str) ? true : hasFalseParts(str) ? false : null; const attrs = { 'generated': boolOrNullVal('linguist-generated'), 'vendored': boolOrNullVal('linguist-vendored'), 'documentation': boolOrNullVal('linguist-documentation'), 'detectable': boolOrNullVal('linguist-detectable'), 'binary': hasTrueParts('binary') || hasFalseParts('text') ? true : hasFalseParts('binary') || hasTrueParts('text') ? false : null, 'language': (_b = (_a = trueParts('linguist-language').at(-1)) === null || _a === void 0 ? void 0 : _a.split('=')[1]) !== null && _b !== void 0 ? _b : null, }; output.push({ glob: relFileGlob, attrs }); } return output; }