Skip to content
This repository has been archived by the owner on Jan 13, 2018. It is now read-only.

Commit

Permalink
util: Introduce isFileValid() and itTimeValid() convenient utilit…
Browse files Browse the repository at this point in the history
…y functions

The are used in `bem-www` `.bem/make.js`, for example
  • Loading branch information
arikon committed Oct 6, 2012
1 parent 957e6ba commit e661144
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/util.js
Expand Up @@ -263,6 +263,42 @@ exports.findLevel = function(path, startPath) {
return exports.findLevel(PATH.dirname(path), startPath);
};

/**
* Checks if `target` file is valid comparing it's mtime
* with all then `depends` files.
*
* @param {String} target Path to file to check validity status.
* @param {String[]|String} depends Target file dependencies to compare mtime to.
* @return {Boolean}
*/
exports.isFileValid = function(target, depends) {

Array.isArray(depends) || (depends = [depends]);
return Q.all([
QFS.lastModified(target)
.fail(function() {
return -1;
}),
Q.all(depends.map(QFS.lastModified.bind(QFS)))
])
.spread(exports.isTimeValid);

};

/**
* Checks if `target` if the largest number among `depends`.
*
* Useful to compare timestamps.
*
* @param {Number} target
* @param {Number[]|Number} depends
* @return {Boolean}
*/
exports.isTimeValid = function(target, depends) {
Array.isArray(depends) || (depends = [depends]);
return target >= Math.max.apply(Math, depends);
};

/**
* Filter out non-existent paths.
*
Expand Down

0 comments on commit e661144

Please sign in to comment.