Skip to content

Commit

Permalink
Fix vendored files not being hidden
Browse files Browse the repository at this point in the history
Vendored files fetched from github-linguist were not being checked properly.

Also doubles performance vs 1.8.0.
  • Loading branch information
Nixinova committed Aug 27, 2021
1 parent 1fc9edb commit 531a9da
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.8.2
- Fixed known vendored files not being removed from output.

## 1.8.1
- Fixed input folder paths being case sensitive.
- Fixed dotfiles not showing up in the output.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "linguist-js",
"version": "1.8.1",
"version": "1.8.2",
"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
14 changes: 6 additions & 8 deletions src/helpers/walk-tree.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import fs from 'fs';
import paths from 'path';
import glob2regex from 'glob-to-regexp';

const allFiles = new Set<string>();
const allFolders = new Set<string>();

/** Generate list of files in a directory. */
export default function walk(folder: string | string[], ignoredGlobs: string[] = []): { files: string[], folders: string[] } {
const ignored = ignoredGlobs.map(glob => glob2regex(glob, { extended: true }));
export default function walk(folder: string | string[], ignored: RegExp[] = []): { files: string[], folders: string[] } {
if (Array.isArray(folder)) {
for (const path of folder) walk(path, ignoredGlobs);
for (const path of folder) walk(path, ignored);
}
else {
allFolders.add(folder.replace(/\\/g, '/'));
const files = fs.readdirSync(folder);
for (const file of files) {
if (ignored.some(pattern => pattern.test(file))) continue;
const path = paths.resolve(folder, file);
const path = paths.resolve(folder, file).replace(/\\/g, '/');
if (ignored.some(pattern => pattern.test(path))) continue;
allFolders.add(folder.replace(/\\/g, '/'));
if (fs.lstatSync(path).isDirectory()) {
allFolders.add(path.replace(/\\/g, '/'))
walk(path);
walk(path, ignored);
continue;
}
allFiles.add(path.replace(/\\/g, '/'));
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ async function analyse(input?: string | string[], opts: T.Options = {}): Promise
};

const ignoredFiles = [
'.git',
opts.keepVendored ? vendorData.map(path => pcre(path).source) : [],
opts.ignore?.map(path => glob2regex('*' + path + '*', { extended: true }).source) ?? [],
/\/\.git\//,
opts.keepVendored ? [] : vendorData.map(path => pcre(path)),
opts.ignore?.map(path => glob2regex('*' + path + '*', { extended: true })) ?? [],
].flat();
let {files, folders} = walk(input ?? '.', ignoredFiles);
let { files, folders } = walk(input ?? '.', ignoredFiles);

// Apply aliases
opts = { checkIgnored: !opts.quick, checkAttributes: !opts.quick, checkHeuristics: !opts.quick, checkShebang: !opts.quick, ...opts };
Expand Down
9 changes: 5 additions & 4 deletions test/perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ async function perfTest() {
let t2 = +new Date();
time += t2 - t1;
}
const total = time / 1e3;
const unit = 'ms';
const total = time;
const average = total / amount;
const EXPECTED_MAX = 0.160; // 1.6
const EXPECTED_MAX = 80; // 1.8.2
console.log('\n<Performance test results>');
console.log('Total:', total, 'sec', `(n=${amount})`);
console.log('Average:', average, 'sec');
console.log('Total:', total, unit, `(n=${amount})`);
console.log('Average:', average, unit);
if (average > EXPECTED_MAX) console.warn('Warning: average runtime higher than expected');
}
perfTest();

0 comments on commit 531a9da

Please sign in to comment.