This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (61 loc) · 1.88 KB
/
index.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
var EventEmitter = require('events').EventEmitter,
TidalWave = require('./build/Release/tidalwave').TidalWave,
glob = require('glob-stream'),
Path = require('path'),
FS = require('fs');
TidalWave.prototype.__proto__ = EventEmitter.prototype;
module.exports.create = create;
function create(targetDir, options) {
var t = new TidalWave(options);
options = options || {};
var getExpectedPath;
if (typeof options.expectDir === 'string') {
getExpectedPath = function(shortPath) {
return Path.resolve(options.expectDir, shortPath);
};
} else if (typeof options.getExpectedPath === 'function') {
getExpectedPath = options.getExpectedPath;
} else {
throw new Error('An option must have "expectDir" or "getExpectedPath" property.');
}
calcAll(t, targetDir, getExpectedPath);
return t;
}
function calcAll(tidalwave, targetDir, getExpectedPath) {
var fileExists = false;
var globEnded = false;
var requested = 0;
var stream = glob.create(Path.resolve(targetDir, '**/*.*'));
stream.once('end', function() {
globEnded = true;
if (!fileExists) {
tidalwave.dispose();
}
});
stream.on('data', function(target) {
fileExists = true;
var shortPath = Path.relative(target.base, target.path);
var expectedFile = getExpectedPath.call(this, shortPath);
if (!expectedFile) {
return;
} else if (typeof expectedFile === 'string') {
calcIfExists(expectedFile)
} else if (typeof expectedFile.then === 'function') {
expectedFile.then(calcIfExists);
}
function calcIfExists(expectedFile) {
FS.exists(expectedFile, function(exists) {
tidalwave.calc(expectedFile, target.path);
requested++;
});
}
});
tidalwave.on('data', function() {
if (globEnded && --requested <= 0) {
tidalwave.dispose();
}
});
tidalwave.on('error', function() {
requested--;
});
}