Skip to content

Commit

Permalink
Add binary file checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixinova committed Aug 15, 2021
1 parent fe0b5f3 commit 10afa18
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 11 deletions.
8 changes: 6 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 1.6.0
*2021-08-15*
- Added checking of binary files to avoid including them in the language results.
- Added `keepBinary` option to control whether binary files are checked (defaults to `false`).

## 1.5.5
*2021-08-15*
- Fixed a crash occurring when using the CLI.
Expand Down Expand Up @@ -27,8 +32,7 @@
## 1.5.0
*2021-08-12*
- Added checking of shebang (`#!`) lines for explicit language classification.
- Added `checkShebang` API option to implement shebang checking (defaults to `true` unless `quick` is set).
- Added `--checkShebang` CLI option (ditto).
- Added `checkShebang` option to implement shebang checking (defaults to `true` unless `quick` is set).

## 1.4.5
*2021-08-10*
Expand Down
18 changes: 16 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "linguist-js",
"version": "1.5.5",
"version": "1.6.0",
"description": "Analyse languages used in a folder. Powered by GitHub Linguist, although it doesn't need to be installed.",
"main": "dist/index.js",
"bin": {
Expand Down Expand Up @@ -32,6 +32,7 @@
},
"homepage": "https://github.com/Nixinova/Linguist#readme",
"dependencies": {
"binary-extensions": "^2.2.0",
"commander": "^8.1.0",
"cross-fetch": "^3.1.4",
"glob-to-regexp": "^0.4.1",
Expand Down
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fetch from 'cross-fetch';
import yaml from 'js-yaml';
import glob from 'tiny-glob';
import glob2regex from 'glob-to-regexp';
import binaryData from 'binary-extensions';
import Cache from 'node-cache';

import * as T from './types';
Expand Down Expand Up @@ -180,9 +181,11 @@ export = async function analyse(root = '.', opts: T.Options = {}): Promise<T.Res
// Fallback to null if no language matches
if (!results[file]) addResult(file, null);
}

// Parse heuristics if applicable
for (const file in results) {
// Skip binary files
if (!opts.keepBinary && binaryData.some(ext => file.endsWith(ext))) continue;

// Parse heuristics if applicable
heuristics:
for (const heuristics of heuristicsData.disambiguations) {
// Check heuristic extensions
Expand Down Expand Up @@ -215,9 +218,7 @@ export = async function analyse(root = '.', opts: T.Options = {}): Promise<T.Res
finalResults[file] ??= Array.isArray(lastLanguage) ? lastLanguage[0] : lastLanguage;
}
}
}
// If no heuristics, load the only language
for (const file in results) {
// If no heuristics, load the only language
finalResults[file] ??= results[file][0];
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type Integer = number

export interface Options {
keepVendored?: boolean
keepBinary?: boolean
quick?: boolean
checkIgnored?: boolean
checkAttributes?: boolean
Expand Down
Empty file added test/samples/image.bmp
Empty file.
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function test() {

await linguist(samplesFolder).then(actual => {
const assert = (a, b, msg) => console.assert(a === b, msg, [a, b]);
const getResults = obj => Object.entries(obj.results).flat().join(',');
const getResults = obj => Object.entries(obj.results).sort(([a], [b]) => a < b ? +1 : -1).flat().join(',');
console.log('Results:', actual);
console.log('TOML data:', actual.languages.all['TOML'], '\n');
assert(getResults(expected), getResults(actual), 'Results');
Expand Down

0 comments on commit 10afa18

Please sign in to comment.