linguist-js
Advanced tools
Comparing version 1.2.1 to 1.2.2
10
cli.js
@@ -16,3 +16,3 @@ #!/usr/bin/env node | ||
analyse: ['a', 'analyze'], | ||
vendored: ['v', 'vendor'], | ||
vendored: ['V', 'vendor'], | ||
gitattributes: ['g'], | ||
@@ -28,6 +28,6 @@ }, | ||
`Analyse the language of all files found in a folder.`, | ||
`<folder> The folder to analyse (optional; default './')`, | ||
`--files List every file parsed (optional)`, | ||
`--vendored Prevent skipping over vendored files (optional)`, | ||
`--gitattributes Check .gitattributes files for custom file associations (optional)`, | ||
`<folder>`, `\tThe folder to analyse (optional; default './')`, | ||
`-f|--files`, `\tList every file parsed (optional)`, | ||
`-V|--vendored`, `\tPrevent skipping over vendored/generated files (optional)`, | ||
`--no-g|--no-gitattributes`, `\tDon't check .gitattributes files for custom file associations (optional)`, | ||
].join('\n')); | ||
@@ -34,0 +34,0 @@ usage(`linguist --version`, 'Display the installed version of linguist-js'); |
43
index.js
@@ -26,26 +26,22 @@ import fs from 'fs'; | ||
// Load gitattributes | ||
if (opts.checkAttributes) { | ||
const getFileRegex = line => glob2regex('**/' + line.split(' ')[0], { globstar: true }); | ||
for (const file in files) { | ||
if (opts.checkAttributes !== false) { | ||
const convertToRegex = path => glob2regex('**/' + path, { globstar: true }); | ||
for (const file of files) { | ||
folders.add(file.replace(/[^\\/]+$/, '')); | ||
} | ||
for (const folder in folders) { | ||
let data; | ||
for (const folder of folders) { | ||
// Attempt to read gitattributes | ||
let data = ''; | ||
try { data = fs.readFileSync(folder + '.gitattributes', { encoding: 'utf8' }); } | ||
catch { return; } | ||
catch { continue; } | ||
// Custom vendor options | ||
{ | ||
const matches = data.match(/^\S+ .*linguist-(vendored|generated|documentation)(?!=false)/gm) || []; | ||
for (const line in matches) { | ||
const filePattern = getFileRegex(line).source; | ||
vendorData.push(folder + filePattern.substr(1)); | ||
} | ||
const vendorMatches = data.matchAll(/^(\S+).*[^-]linguist-(vendored|generated|documentation)(?!=false)/gm); | ||
for (const [line, path] of vendorMatches) { | ||
vendorData.push(folder + convertToRegex(path).source.substr(1)); | ||
} | ||
// Custom file associations | ||
{ | ||
const matches = data.match(/^\S+ .*linguist-language=\S+/gm) || []; | ||
const customLangMatches = data.matchAll(/^(\S+).*[^-]linguist-language=(\S+)/gm); | ||
const langDataArray = Object.entries(langData); | ||
for (const line in matches) { | ||
let filePattern = getFileRegex(line).source; | ||
let forcedLang = line.match(/linguist-language=(\S+)/)[1]; | ||
for (let [line, path, forcedLang] of customLangMatches) { | ||
// If specified language is an alias, associate it with its full name | ||
@@ -59,3 +55,3 @@ if (!langData[forcedLang]) { | ||
} | ||
const fullPath = folder + filePattern.substr(1); | ||
const fullPath = folder + convertToRegex(path).source.substr(1); | ||
overrides[fullPath] = forcedLang; | ||
@@ -65,3 +61,2 @@ } | ||
} | ||
} | ||
// Check vendored files | ||
@@ -73,2 +68,3 @@ if (!opts.keepVendored) { | ||
} | ||
} | ||
// Load all files and parse languages | ||
@@ -85,6 +81,11 @@ const addResult = (file, data) => { | ||
files.forEach(file => { | ||
if (fs.lstatSync(file).isDirectory()) return; | ||
// Check override for manual language classification | ||
if (opts.checkAttributes) { | ||
const match = overridesArray.find(item => file.match(new RegExp(item[0]))); | ||
if (match) addResult(file, match[1]); | ||
if (match) { | ||
const forcedLang = match[1]; | ||
addResult(file, forcedLang); | ||
return; | ||
} | ||
} | ||
@@ -100,2 +101,6 @@ // Search each language | ||
} | ||
// Fallback to null if no language matches | ||
if (!results[file]) { | ||
addResult(file, null); | ||
} | ||
}); | ||
@@ -102,0 +107,0 @@ // Parse heuristics if applicable |
{ | ||
"name": "linguist-js", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Analyse languages used in a folder. Powered by GitHub Linguist, although it doesn't need to be installed.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -0,1 +1,5 @@ | ||
[![Latest version](https://img.shields.io/github/v/release/Nixinova/Linguist?label=latest%20version&style=flat-square)](https://github.com/Nixinova/Linguist/releases) | ||
[![Last updated](https://img.shields.io/github/release-date/Nixinova/Linguist?label=updated&style=flat-square)](https://github.com/Nixinova/Linguist/releases) | ||
[![npm downloads](https://img.shields.io/npm/dt/linguist?logo=npm)](https://www.npmjs.com/package/linguist) | ||
# Linguist | ||
@@ -2,0 +6,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11794
178
106