Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide files with given glob patterns #150

Merged
merged 4 commits into from
Mar 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,14 @@ export let config = {
details.`,
type: 'boolean',
default: false,
},
ignoredPatterns: {
title: 'Ignore patterns',
description: 'Array of glob patterns to hide matching filenames.',
type: 'array',
default: ['*.pyc', '*.pyo'],
items: {
type: 'string',
},
}
};
12 changes: 12 additions & 0 deletions lib/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
cachedProperty,
defineImmutable,
getProjectPath,
ignoredPatterns,
preferredSeparatorFor
} from './utils';

Expand Down Expand Up @@ -147,6 +148,7 @@ export class Path {
}
}

filenames = filenames.filter(isVisible)
return filenames.map((fn) => new Path(this.directory + fn));
}

Expand Down Expand Up @@ -239,3 +241,13 @@ function matchFragment(fragment, filename, caseSensitive=false) {

return filename.startsWith(fragment);
}

/**
* Return whether the filename is not hidden by the ignoredPatterns config.
*/
function isVisible(filename) {
for (const ignoredPattern of ignoredPatterns()) {
if (ignoredPattern.match(filename)) return false;
}
return true;
}
30 changes: 30 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/** @babel */

import minimatch from 'minimatch';
import stdPath from 'path';
import StringMap from 'stringmap';

import osenv from 'osenv';

import * as config from './config';


/**
* Generates the return value for the wrapper property on first access
Expand Down Expand Up @@ -135,3 +139,29 @@ export function closest(selector) {
return null;
}
}


/**
* Return value from StringMap if exists
* otherwise generate value with getValue, set the StringMap and return
*/
export function getOrUpdateStringMap(stringMap, key, getValue) {
if (stringMap.has(key) ) {
return stringMap.get(key);
}
const value = getValue(key);
stringMap.set(key, value);
return value;
}

/**
* Returns an array of Minimatch instances to test whether a filename
* is ignored. Cache the Minimatch instances instead of re-compiling
* the regexp.
*/
let _minimatchesCache = StringMap();
export function ignoredPatterns() {
const ignoredGlobs = config.get('ignoredPatterns')
return ignoredGlobs.map((glob) => getOrUpdateStringMap(_minimatchesCache, glob, (glob) =>
new minimatch.Minimatch(glob)))
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@
"event-kit": "^1.3.0",
"fuzzaldrin-plus": "^0.3.1",
"mkdirp": "^0.5.1",
"minimatch": "^3.0.4",
"osenv": "^0.1.3",
"touch": "^1.0.0"
"touch": "^1.0.0",
"stringmap": "~0.2.2"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: stringmap is already used a dependencies of dependencies, so this shouldn't increase load time

},
"devDependencies": {
"babel": "^5.8.35",
Expand Down