1 | const fs = require('fs');
|
2 | const path = require('path');
|
3 | const ignore = require('ignore');
|
4 | const findup = require('findup-sync');
|
5 |
|
6 | const ignoreRules = ignore().add('node_modules');
|
7 |
|
8 | let configPath = null;
|
9 | let loaded = false;
|
10 | function loadIgnoreConfig() {
|
11 | if (loaded) {
|
12 | return;
|
13 | }
|
14 | const file = findup('.hsignore');
|
15 | if (file) {
|
16 | if (fs.existsSync(file)) {
|
17 | ignoreRules.add(fs.readFileSync(file).toString());
|
18 | configPath = path.dirname(file);
|
19 | }
|
20 | }
|
21 | loaded = true;
|
22 | }
|
23 |
|
24 | function shouldIgnoreFile(file, cwd) {
|
25 | loadIgnoreConfig();
|
26 | const relativeTo = configPath || cwd;
|
27 | return ignoreRules.ignores(path.relative(relativeTo, file));
|
28 | }
|
29 |
|
30 | function createIgnoreFilter(cwd) {
|
31 | loadIgnoreConfig();
|
32 | return file => !shouldIgnoreFile(file, cwd);
|
33 | }
|
34 |
|
35 | module.exports = {
|
36 | loadIgnoreConfig,
|
37 | shouldIgnoreFile,
|
38 | createIgnoreFilter,
|
39 | };
|