grunt-changed
Advanced tools
Comparing version 1.1.1 to 2.0.0
113
lib/util.js
@@ -0,1 +1,2 @@ | ||
/* global console */ | ||
var crypto = require('crypto'); | ||
@@ -15,4 +16,5 @@ var fs = require('fs'); | ||
*/ | ||
var getHashPath = exports.getHashPath = function(cacheDir, taskName, targetName, | ||
filePath) { | ||
var getHashPath = exports.getHashPath = function (cacheDir, taskName, | ||
targetName, | ||
filePath) { | ||
var hashedName = crypto.createHash('md5').update(filePath).digest('hex'); | ||
@@ -82,2 +84,3 @@ var dir = path.join(cacheDir, taskName, targetName, 'hashes'); | ||
* @param {string} targetName Target name. | ||
* @param {function(string, function(boolean))} override Override. | ||
* @param {function(Error, Array.<string>)} callback Callback called with any | ||
@@ -89,3 +92,3 @@ * error and a filtered list of files that only includes files with hashes | ||
paths, cacheDir, | ||
taskName, targetName, callback | ||
taskName, targetName, override, callback | ||
) { | ||
@@ -104,23 +107,17 @@ async.filter(paths, function(filePath, done) { | ||
} | ||
console.log(filePath); | ||
var changed = String(hashes.previous) !== String(hashes.current); | ||
if (changed) { | ||
writeFileHash( | ||
cacheDir, | ||
taskName, | ||
targetName, | ||
filePath, | ||
hashes.current, | ||
function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
done(null, changed); | ||
done(null, true); | ||
} else { | ||
override(filePath, function (newChanged) { | ||
if (newChanged) { | ||
done(null, true); | ||
} else { | ||
done(null, false); | ||
} | ||
); | ||
} else { | ||
done(null, changed); | ||
} | ||
}); | ||
}, callback); | ||
}); | ||
} | ||
}); | ||
}, callback); | ||
}; | ||
@@ -134,2 +131,3 @@ | ||
* @param {string} targetName Target name. | ||
* @param {function(string, function(boolean))} override Override. | ||
* @param {function(Error, Array.<Object>)} callback Callback called with a | ||
@@ -141,6 +139,6 @@ * filtered list of file config objects. Object returned will only include | ||
exports.filterFilesByHash = function( | ||
files, cacheDir, taskName, targetName, callback | ||
files, cacheDir, taskName, targetName, override, callback | ||
) { | ||
async.map(files, function(obj, done) { | ||
filterPathsByHash(obj.src, cacheDir, taskName, targetName, | ||
filterPathsByHash(obj.src, cacheDir, taskName, targetName, override, | ||
function(err, src) { | ||
@@ -181,1 +179,70 @@ if (obj.dest && !fs.existsSync(obj.dest)) { | ||
}; | ||
var doGenerateFileHashes = function ( | ||
paths, cacheDir, | ||
taskName, targetName, override, callback | ||
) { | ||
async.filter(paths, function (filePath, done) { | ||
async.parallel({ | ||
current: function (cb) { | ||
generateFileHash(filePath, cb); | ||
} | ||
}, function (err, hashes) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
writeFileHash( | ||
cacheDir, | ||
taskName, | ||
targetName, | ||
filePath, | ||
hashes.current, | ||
done | ||
); | ||
}); | ||
}, callback); | ||
}; | ||
exports.generateFileHashes = function ( | ||
files, cacheDir, taskName, targetName, override, callback | ||
) { | ||
async.map(files, function (obj, done) { | ||
doGenerateFileHashes(obj.src, cacheDir, taskName, targetName, override, | ||
function (err, src) { | ||
if (obj.dest && !fs.existsSync(obj.dest)) { | ||
done(null, obj); | ||
} else if (src && src.length > 0) { | ||
if ( | ||
obj.dest && | ||
!(obj.src.length === 1 && obj.dest === obj.src[ 0 ]) | ||
) { | ||
done(null, obj); | ||
} else { | ||
done( | ||
null, { | ||
src: src, | ||
dest: obj.dest | ||
} | ||
); | ||
} | ||
} else { | ||
done(null, null); | ||
} | ||
} | ||
); | ||
}, function (e, mappedFiles) { | ||
async.filter( | ||
mappedFiles, | ||
function (obj, generateDone) { | ||
if (obj) { | ||
generateDone(null, true); | ||
} else { | ||
generateDone(null, false); | ||
} | ||
}, callback | ||
); | ||
}); | ||
}; |
{ | ||
"name": "grunt-changed", | ||
"description": "Run Grunt tasks with only those source files where the content changed to the last run.", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/researchgate/grunt-changed", | ||
@@ -31,3 +31,4 @@ "author": { | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"chai": "^4.0.1", | ||
"fs-extra": "^5.0.0", | ||
"grunt": "^1.0.0", | ||
@@ -37,7 +38,5 @@ "grunt-contrib-clean": "^1.0.0", | ||
"grunt-contrib-watch": "^1.0.0", | ||
"grunt-mocha-test": "^0.12.7", | ||
"mocha": "^3.0.0", | ||
"mock-fs": "^3.7.0", | ||
"tmp": "^0.0.28", | ||
"wrench": "^1.5.8" | ||
"grunt-mocha-test": "^0.13.0", | ||
"mocha": "^4.0.0", | ||
"tmp": "^0.0.33" | ||
}, | ||
@@ -55,5 +54,5 @@ "peerDependencies": { | ||
"async": "^2.0.0", | ||
"rimraf": "^2.5.1", | ||
"mkdirp": "^0.5.1" | ||
"mkdirp": "^0.5.1", | ||
"rimraf": "^2.5.1" | ||
} | ||
} |
@@ -8,5 +8,9 @@ var path = require('path'); | ||
function cacheConfig(config) { | ||
function cacheConfig(config, changedFiles, override) { | ||
++counter; | ||
configCache[counter] = config; | ||
configCache[counter] = { | ||
config: config, | ||
changedFiles: changedFiles, | ||
override: override | ||
}; | ||
return counter; | ||
@@ -24,2 +28,6 @@ } | ||
function nullOverride(details, include) { | ||
include(false); | ||
} | ||
function createTask(grunt) { | ||
@@ -43,2 +51,3 @@ return function(taskName, targetName) { | ||
var options = this.options({ | ||
override: nullOverride, | ||
cache: path.join(__dirname, '..', '.cache') | ||
@@ -68,2 +77,23 @@ }); | ||
function override(filePath, include) { | ||
var details = { | ||
task: taskName, | ||
target: targetName, | ||
path: filePath, | ||
getExistingHash: function (fp, cb) { | ||
util.getExistingHash(fp, | ||
options.cache, | ||
taskName, | ||
targetName, | ||
cb); | ||
}, | ||
generateFileHash: function (fp, cb) { | ||
util.generateFileHash(filePath, | ||
cb); | ||
} | ||
}; | ||
options.override(details, include); | ||
} | ||
var files = grunt.task.normalizeMultiTaskFiles(config, targetName); | ||
@@ -75,2 +105,3 @@ util.filterFilesByHash( | ||
targetName, | ||
override, | ||
function(e, changedFiles) { | ||
@@ -101,9 +132,9 @@ if (e) { | ||
// because we modified the task config, cache the original | ||
var id = cacheConfig(originalConfig); | ||
var id = cacheConfig(originalConfig, changedFiles, override); | ||
// run the task, and attend to postrun tasks | ||
// run the task, and attend to postrun tasks | ||
var qualified = taskName + ':' + targetName; | ||
var tasks = [ | ||
qualified + (args ? ':' + args : ''), | ||
'changed-postrun:' + qualified + ':' + id | ||
'changed-postrun:' + qualified + ':' + id + ':' + options.cache | ||
]; | ||
@@ -119,3 +150,2 @@ grunt.task.run(tasks); | ||
/** @param {Object} grunt Grunt. */ | ||
@@ -130,6 +160,30 @@ module.exports = function(grunt) { | ||
grunt.registerTask( | ||
'changed-postrun', internal, function(taskName, targetName, id) { | ||
// reconfigure task with original config | ||
grunt.config.set([taskName, targetName], pluckConfig(id)); | ||
'changed-postrun', | ||
internal, | ||
function(taskName, targetName, id, cacheDir) { | ||
var done = this.async(); | ||
var config = pluckConfig(id); | ||
// if cacheDir includes a ':', grunt will split it among multiple args | ||
cacheDir = Array.prototype.slice.call(arguments, 3).join(':'); | ||
util.generateFileHashes( | ||
config.changedFiles, | ||
cacheDir, | ||
taskName, | ||
targetName, | ||
config.override, | ||
function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
// reconfigure task with original config | ||
grunt.config.set([ taskName, targetName ], config.config); | ||
done(); | ||
} | ||
); | ||
}); | ||
@@ -136,0 +190,0 @@ |
22354
9
459