1 | const fs = require('fs');
|
2 | const path = require('path');
|
3 | const ignore = require('ignore');
|
4 | const findup = require('findup-sync');
|
5 |
|
6 | const ignoreList = [
|
7 | 'hubspot.config.yml',
|
8 | 'hubspot.config.yaml',
|
9 | 'node_modules',
|
10 | '.*',
|
11 | '*.log',
|
12 | '*.swp',
|
13 |
|
14 |
|
15 | 'Icon\\r',
|
16 | '__MACOSX',
|
17 |
|
18 |
|
19 | '~',
|
20 |
|
21 |
|
22 | 'Thumbs.db',
|
23 | 'ehthumbs.db',
|
24 | 'Desktop.ini',
|
25 | '@eaDir',
|
26 | ];
|
27 |
|
28 | const ignoreRules = ignore().add(ignoreList);
|
29 |
|
30 | let configPath = null;
|
31 | let loaded = false;
|
32 | function 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 |
|
46 | function shouldIgnoreFile(file, cwd) {
|
47 | loadIgnoreConfig();
|
48 | const relativeTo = configPath || cwd;
|
49 | return ignoreRules.ignores(path.relative(relativeTo, file));
|
50 | }
|
51 |
|
52 | function createIgnoreFilter(cwd) {
|
53 | loadIgnoreConfig();
|
54 | return file => !shouldIgnoreFile(file, cwd);
|
55 | }
|
56 |
|
57 | function ignoreFile(filePath) {
|
58 | ignoreRules.add(filePath);
|
59 | }
|
60 |
|
61 | module.exports = {
|
62 | loadIgnoreConfig,
|
63 | shouldIgnoreFile,
|
64 | createIgnoreFilter,
|
65 | ignoreFile,
|
66 | };
|