-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-watchable-dirs.js
87 lines (87 loc) · 3.19 KB
/
get-watchable-dirs.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
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const async = require("async");
const path = require("path");
const bunion_1 = require("bunion");
const fs = require("fs");
const constants_1 = require("../constants");
exports.flattenDeep = (a) => {
return a.reduce((acc, val) => Array.isArray(val) ? acc.concat(exports.flattenDeep(val)) : acc.concat(val), []);
};
let callable = true;
exports.getWatchableDirs = (config, cb) => {
if (!callable) {
throw 'Why call me twice?';
}
callable = false;
const paths = exports.flattenDeep([config.codeRoots]).map(v => path.resolve(v));
const uniquePaths = Array.from(new Set(paths));
const q = async.queue((task, cb) => task(cb), 15);
const alreadySeen = new Set();
const uniqueFolders = new Set();
const goThroughDir = (dir, isWatchingNow, relevantGitRepo) => {
if (alreadySeen.has(dir)) {
return;
}
alreadySeen.add(dir);
for (let ignore of constants_1.ignorePathsRegex) {
if (ignore.test(path.resolve(dir))) {
bunion_1.default.debug('path ignored:', dir);
return;
}
if (ignore.test(path.resolve(dir + '/'))) {
bunion_1.default.debug('path ignored:', dir);
return;
}
if (ignore.test(path.resolve(dir + '/') + '/')) {
bunion_1.default.debug('path ignored:', dir);
return;
}
}
q.push(cb => {
fs.stat(dir, (err, stats) => {
if (err) {
return cb(null);
}
if (!stats.isDirectory()) {
return cb(null);
}
try {
var cprevjs = require(path.resolve(dir + '/.cprev.js'));
bunion_1.default.info(dir, { cprevjs });
isWatchingNow = true;
}
catch (err) {
}
const potentialGitFolder = path.resolve(dir + '/.git');
fs.stat(potentialGitFolder, (err, stats) => {
if (stats && stats.isDirectory()) {
relevantGitRepo = potentialGitFolder;
}
if (isWatchingNow) {
uniqueFolders.add({ dirpath: dir, git_repo: relevantGitRepo });
}
fs.readdir(dir, (err, results) => {
cb(null);
if (err) {
bunion_1.default.warn('bb97184f-4a07-4acd-b7bc-59dc8e5fb0e4:', err);
return cb(null);
}
for (const v of results) {
goThroughDir(path.resolve(dir + '/' + v), isWatchingNow, relevantGitRepo);
}
});
});
});
});
};
q.error(err => {
bunion_1.default.error(err);
});
q.drain(() => {
cb(null, Array.from(uniqueFolders));
});
for (const p of uniquePaths) {
goThroughDir(p, false, path.normalize('/tmp/unknown/git/repo/path'));
}
};