UNPKG

851 BJavaScriptView Raw
1const fs = require('fs');
2const path = require('path');
3const ignore = require('ignore');
4const findup = require('findup-sync');
5
6const ignoreRules = ignore().add('node_modules');
7
8let configPath = null;
9let loaded = false;
10function 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
24function shouldIgnoreFile(file, cwd) {
25 loadIgnoreConfig();
26 const relativeTo = configPath || cwd;
27 return ignoreRules.ignores(path.relative(relativeTo, file));
28}
29
30function createIgnoreFilter(cwd) {
31 loadIgnoreConfig();
32 return file => !shouldIgnoreFile(file, cwd);
33}
34
35module.exports = {
36 loadIgnoreConfig,
37 shouldIgnoreFile,
38 createIgnoreFilter,
39};