-
Notifications
You must be signed in to change notification settings - Fork 13
/
mtime.js
62 lines (54 loc) · 1.64 KB
/
mtime.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
var fs = require('fs')
var async = require('async')
function getFilesMtimes(files, concurrencyLimit, done) {
var filesMtimes = {}
async.eachLimit(
files,
concurrencyLimit,
function (file, fileDone) {
fs.stat(file, function (statErr, stat) {
if (statErr) {
if (statErr.code === 'ENOENT') return fileDone()
return fileDone(statErr)
}
filesMtimes[file] = stat.mtime.getTime()
fileDone()
})
},
function (err) {
if (err) return done(err)
done(null, filesMtimes)
}
)
}
function getFilesChanges(filesMtimes, concurrencyLimit, done) {
var changed = []
var deleted = []
var files = Object.keys(filesMtimes)
function eachFile(file, fileDone) {
fs.stat(file, function (err, stat) {
if (err) {
deleted.push(file)
return fileDone()
}
var mtimeNew = stat.mtime.getTime()
if (mtimeNew > Number(filesMtimes[file])) {
changed.push(file)
}
fileDone()
})
}
async.eachLimit(files, concurrencyLimit, eachFile, function () {
done(deleted, changed)
})
}
function hasAnyFileChanged(filesMtimes, concurrencyLimit, done) {
getFilesChanges(filesMtimes, concurrencyLimit, function (deleted, changed) {
var numFilesChanged = deleted.length + changed.length
done(null, numFilesChanged > 0)
})
}
module.exports = {
getFilesMtimes: getFilesMtimes,
hasAnyFileChanged: hasAnyFileChanged,
}