UNPKG

1.63 kBJavaScriptView Raw
1const fs = require('fs');
2const path = require('path');
3const ignore = require('ignore');
4const findup = require('findup-sync');
5
6// Rules used by ignore do not allow RegEx -- we are basing most of this list
7// on the Junk library, which uses RegEx https://github.com/sindresorhus/junk/blob/master/index.js#L3
8const ignoreList = [
9 '**/node_modules', // dependencies
10 '**/.*', // hidden files/folders
11 '**/*.log', // Error log for npm
12 '**/*.swp', // Swap file for vim state
13
14 // # macOS
15 '**/Icon\\r', // Custom Finder icon: http://superuser.com/questions/298785/icon-file-on-os-x-desktop
16 '**/__MACOSX', // Resource fork
17
18 // # Linux
19 '**/~', // Backup file
20
21 // # Windows
22 '**/Thumbs.db', // Image file cache
23 '**/ehthumbs.db', // Folder config file
24 '**/Desktop.ini', // Stores custom folder attributes
25 '**/@eaDir', // Synology Diskstation "hidden" folder where the server stores thumbnails
26];
27
28const ignoreRules = ignore().add(ignoreList);
29
30let configPath = null;
31let loaded = false;
32function loadIgnoreConfig() {
33 if (loaded) {
34 return;
35 }
36 const file = findup('.hsignore');
37 if (file) {
38 if (fs.existsSync(file)) {
39 ignoreRules.add(fs.readFileSync(file).toString());
40 configPath = path.dirname(file);
41 }
42 }
43 loaded = true;
44}
45
46function shouldIgnoreFile(file, cwd) {
47 loadIgnoreConfig();
48 const relativeTo = configPath || cwd;
49 return ignoreRules.ignores(path.relative(relativeTo, file));
50}
51
52function createIgnoreFilter(cwd) {
53 loadIgnoreConfig();
54 return file => !shouldIgnoreFile(file, cwd);
55}
56
57module.exports = {
58 loadIgnoreConfig,
59 shouldIgnoreFile,
60 createIgnoreFilter,
61};