forked from SocketCluster/socketcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsutil.js
48 lines (43 loc) · 1.24 KB
/
fsutil.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
var fs = require('fs');
var scErrors = require('sc-errors');
var TimeoutError = scErrors.TimeoutError;
var fileExists = function (filePath, callback) {
fs.access(filePath, fs.constants.F_OK, (err) => {
callback(!err);
});
};
var waitForFile = function (filePath, checkInterval, startTime, maxWaitDuration, timeoutErrorMessage) {
return new Promise((resolve, reject) => {
if (!filePath) {
resolve();
return;
}
var checkIsReady = () => {
var now = Date.now();
fileExists(filePath, (exists) => {
if (exists) {
resolve();
} else {
if (now - startTime >= maxWaitDuration) {
var errorMessage;
if (timeoutErrorMessage != null) {
errorMessage = timeoutErrorMessage;
} else {
errorMessage = `Could not find a file at path ${filePath} ` +
`before the timeout was reached`;
}
var volumeBootTimeoutError = new TimeoutError(errorMessage);
reject(volumeBootTimeoutError);
} else {
setTimeout(checkIsReady, checkInterval);
}
}
});
};
checkIsReady();
});
};
module.exports = {
fileExists: fileExists,
waitForFile: waitForFile
};