-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwait-file-exists.js
40 lines (36 loc) · 1.15 KB
/
wait-file-exists.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
const path = require('path')
const fs = require('fs')
/**
* From @xprudhomme.
* Check if file exists, watching containing directory meanwhile.
* Resolve if the file exists, or if the file is created before the timeout
* occurs.
* @param {string} filePath
* @param {integer} timeout
* @returns {!Promise<undefined>} Resolves when file has been created. Rejects
* if timeout is reached.
*/
module.exports = function waitForFileExists(filePath, timeout=15000) {
return new Promise((resolve, reject) => {
const dir = path.dirname(filePath);
const basename = path.basename(filePath);
const watcher = fs.watch(dir, (eventType, filename) => {
if (eventType === 'rename' && filename === basename) {
clearTimeout(timer);
watcher.close();
resolve();
}
});
const timer = setTimeout(() => {
watcher.close();
reject(new Error(' [checkFileExists] File does not exist, and was not created during the timeout delay.'));
}, timeout);
fs.access(filePath, fs.constants.R_OK, err => {
if (!err) {
clearTimeout(timer);
watcher.close();
resolve();
}
});
});
}