forked from JonathanWolfe/file-tree-exclude
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
112 lines (95 loc) · 3.59 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
define(function (require, exports, module) {
'use strict';
// Get our Brackets modules
var _ = brackets.getModule('thirdparty/lodash');
var Commands = brackets.getModule('command/Commands');
var CommandManager = brackets.getModule('command/CommandManager');
var FileSystem = brackets.getModule('filesystem/FileSystem');
var FileSystemImpl = FileSystem._FileSystem;
var ProjectManager = brackets.getModule('project/ProjectManager');
var PreferencesManager = brackets.getModule('preferences/PreferencesManager');
var PackageJson = JSON.parse(require('text!./package.json'));
var StateManager = PreferencesManager.stateManager;
// Default excludes
var defaultExcludeList = [
'/.git/',
'/dist/',
'/bower_components/',
'/node_modules/'
];
// Get the preferences for this extension
var preferences = PreferencesManager.getExtensionPrefs(PackageJson.name);
preferences.definePreference('excludeList', 'array', defaultExcludeList);
// projectRoot & projectPath
var excludeList = null;
var projectPath = null;
// Check if the extension has been updated
if (PackageJson.version !== StateManager.get(PackageJson.name + '.version')) {
StateManager.set(PackageJson.name + '.version', PackageJson.version);
preferences.set('excludeList', defaultExcludeList);
}
function escapeStringToRegexp(str) {
// https://github.com/sindresorhus/escape-string-regexp/blob/master/index.js
return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
}
function toRegexp(str) {
if (typeof str !== 'string') { str = str.toString(); }
var start = '';
var body = str;
var end = '';
// if starts with ^, it must be start of the string
// if starts with /, it can be either start of the string or start of the file/folder
if (body[0] === '^') {
start = '^';
body = body.slice(1);
} else if (body[0] === '/') {
start = '(^|/)';
body = body.slice(1);
}
// if ends with slash, make it possible end of the string too
if (body.slice(-1) === '/') {
end = '(/|$)';
body = body.slice(0, -1);
}
return start + escapeStringToRegexp(body) + end;
}
function fetchVariables(forceRefresh) {
var projectRoot = ProjectManager.getProjectRoot();
projectPath = projectRoot ? projectRoot.fullPath : null;
excludeList = preferences.get('excludeList', projectPath);
excludeList = excludeList.map(toRegexp);
if (forceRefresh === true) {
CommandManager.execute(Commands.FILE_REFRESH);
}
}
function clearVariables() {
projectPath = null;
}
// attach events
ProjectManager.on('projectOpen', function () { fetchVariables(true); });
ProjectManager.on('projectRefresh', function () { fetchVariables(true); });
ProjectManager.on('beforeProjectClose', function () { clearVariables(); });
FileSystem.on('change', function (event, entry, added, removed) {
// entry === null when manual refresh is done
if (entry == null) {
fetchVariables(false);
} else if (entry.name === '.brackets.json') {
fetchVariables(true);
}
});
// Filter itself
var _oldFilter = FileSystemImpl.prototype._indexFilter;
FileSystemImpl.prototype._indexFilter = function (path, name) {
if (!excludeList || !projectPath) {
fetchVariables();
if (!excludeList || !projectPath) {
return _oldFilter.apply(this, arguments);
}
}
var relativePath = path.slice(projectPath.length);
var excluded = _.any(excludeList, function (toMatch) {
return relativePath.match(toMatch) != null;
});
return excluded ? false : _oldFilter.apply(this, arguments);
};
});