diff --git a/Gruntfile.js b/Gruntfile.js index 088b307..9e736a7 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -42,7 +42,7 @@ module.exports = function (grunt) { withExpand: { expand: true, cwd: 'test/fixtures', - src: ['*'], + src: ['*.png'], dest: 'test/tmp/expand' }, withSummaryAttributeName: { @@ -51,6 +51,39 @@ module.exports = function (grunt) { }, src: ['test/fixtures/file.png', 'test/fixtures/another.png'], dest: 'test/tmp' + }, + withCopyTrue: { + options: { + algorithm: 'sha1', + length: 4, + copy: 1 + }, + src: ['test/tmp/another.png'], + dest: 'test/tmp' + }, + withCopyFalse: { + options: { + algorithm: 'sha1', + length: 4, + copy: 0 + }, + src: ['test/tmp/movedfile.png'], + dest: 'test/tmp/copyfalse' + }, + withCopyNullDest: { + options: { + algorithm: 'sha1', + length: 4 + }, + src: ['test/tmp/another.png'], + dest: 'test/tmp/nocopy' + }, + withCopyNullNoDest: { + options: { + algorithm: 'sha1', + length: 4 + }, + src: ['test/tmp/movednocopyfile.png'] } }, simplemocha: { diff --git a/README.md b/README.md index c739eb5..d4846ef 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# grunt-filerev [![Build Status](https://secure.travis-ci.org/yeoman/grunt-filerev.svg?branch=master)](http://travis-ci.org/yeoman/grunt-filerev) +# grunt-filerev [![Build Status](https://secure.travis-ci.org/yeoman/grunt-filerev.png?branch=master)](http://travis-ci.org/yeoman/grunt-filerev) [![Built with Grunt](https://cdn.gruntjs.com/builtwith.png)](http://gruntjs.com/) > Static asset revisioning through file content hash @@ -61,6 +61,14 @@ Default: `8` The number of characters of the file hash to prefix the file name with. +#### options.copy + +Type: `Boolean` +Default: `null` + +Flag controlling whether the original file is copied or moved. If not set, +other parameters, such as setting a [destination](#destination) control the behaviour. + ### Destination It will overwrite the `src` files if you don't specify a `dest`: diff --git a/tasks/filerev.js b/tasks/filerev.js index d0ee183..e53a774 100644 --- a/tasks/filerev.js +++ b/tasks/filerev.js @@ -10,13 +10,13 @@ module.exports = function (grunt) { var options = this.options({ encoding: 'utf8', algorithm: 'md5', - length: 8 + length: 8, + copy: null }); var target = this.target; var filerev = grunt.filerev || {summary: {}}; eachAsync(this.files, function (el, i, next) { - var move = true; // If dest is furnished it should indicate a directory if (el.dest) { @@ -28,20 +28,15 @@ module.exports = function (grunt) { try { var stat = fs.lstatSync(el.dest); if (stat && !stat.isDirectory()) { - grunt.fail.fatal('Destination ' + el.dest + ' for target ' + target + ' is not a directory'); + grunt.fail.fatal('Destination for target %s is not a directory', target); } } catch (err) { grunt.log.writeln('Destination dir ' + el.dest + ' does not exists for target ' + target + ': creating'); grunt.file.mkdir(el.dest); } - // We need to copy file as we now have a dest different from the src - move = false; } el.src.forEach(function (file) { - if (grunt.file.isDir(file)) { - return; - } var dirname; var hash = crypto.createHash(options.algorithm).update(grunt.file.read(file), options.encoding).digest('hex'); var suffix = hash.slice(0, options.length); @@ -49,14 +44,13 @@ module.exports = function (grunt) { var newName = [path.basename(file, ext), suffix, ext.slice(1)].join('.'); var resultPath; - if (move) { - dirname = path.dirname(file); - resultPath = path.resolve(dirname, newName); - fs.renameSync(file, resultPath); - } else { - dirname = el.dest; - resultPath = path.resolve(dirname, newName); + dirname = el.dest ? el.dest : path.dirname(file); + resultPath = path.resolve(dirname, newName); + + if (options.copy || (options.copy === null && el.dest)) { grunt.file.copy(file, resultPath); + } else { + fs.renameSync(file, resultPath); } filerev.summary[path.normalize(file)] = path.join(dirname, newName); diff --git a/test/filerev_test.js b/test/filerev_test.js index bbcbdae..1dae884 100644 --- a/test/filerev_test.js +++ b/test/filerev_test.js @@ -26,4 +26,36 @@ describe('filerev', function () { var revisioned= fs.statSync('test/tmp/expand/file.a0539763.png').size; assert(revisioned === original); }); + + it('should copy the file when copy option is true', function () { + var original = fs.statSync('test/fixtures/another.png').size; + var revisioned= fs.statSync('test/tmp/another.37ba.png').size; + assert(revisioned === original); + var fileExists = fs.existsSync('test/tmp/another.png'); + assert(fileExists === true); + }); + + it('should move the file when copy option is false', function () { + var original = fs.statSync('test/fixtures/movedfile.png').size; + var revisioned= fs.statSync('test/tmp/copyfalse/movedfile.37ba.png').size; + assert(revisioned === original); + var fileExists = fs.existsSync('test/tmp/movedfile.png'); + assert(fileExists === false); + }); + + it('should copy the file without copy option when dest is specified', function () { + var original = fs.statSync('test/fixtures/another.png').size; + var revisioned= fs.statSync('test/tmp/nocopy/another.37ba.png').size; + assert(revisioned === original); + var fileExists = fs.existsSync('test/tmp/another.png'); + assert(fileExists === true); + }); + + it('should move the file without copy option when dest is not specified', function () { + var original = fs.statSync('test/fixtures/movednocopyfile.png').size; + var revisioned= fs.statSync('test/tmp/movednocopyfile.37ba.png').size; + assert(revisioned === original); + var fileExists = fs.existsSync('test/tmp/movednocopyfile.png'); + assert(fileExists === false); + }); }); diff --git a/test/fixtures/dir/.gitkeep b/test/fixtures/dir/.gitkeep deleted file mode 100644 index 45adbb2..0000000 --- a/test/fixtures/dir/.gitkeep +++ /dev/null @@ -1 +0,0 @@ -.gitkeep \ No newline at end of file diff --git a/test/fixtures/movedfile.png b/test/fixtures/movedfile.png new file mode 100644 index 0000000..f0a907d Binary files /dev/null and b/test/fixtures/movedfile.png differ diff --git a/test/fixtures/movednocopyfile.png b/test/fixtures/movednocopyfile.png new file mode 100644 index 0000000..f0a907d Binary files /dev/null and b/test/fixtures/movednocopyfile.png differ