diff --git a/Gruntfile.js b/Gruntfile.js index 2d84573..b9562bb 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -76,6 +76,39 @@ module.exports = function (grunt) { cwd: 'test/fixtures', src: ['*.js', '*.css'], dest: 'test/tmp/withSourceMaps' + }, + 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 8c793dd..2420028 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,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. + #### options.process(basename, name, extension) Type: `function` diff --git a/tasks/filerev.js b/tasks/filerev.js index 28e570a..26ecf9d 100644 --- a/tasks/filerev.js +++ b/tasks/filerev.js @@ -12,11 +12,11 @@ module.exports = function (grunt) { var filerev = grunt.filerev || {summary: {}}; var options = this.options({ algorithm: 'md5', - length: 8 + length: 8, + copy: null }); eachAsync(this.files, function (el, i, next) { - var move = true; // If dest is furnished it should indicate a directory if (el.dest) { @@ -35,8 +35,6 @@ module.exports = function (grunt) { grunt.verbose.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) { @@ -62,14 +60,13 @@ module.exports = function (grunt) { 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); } // Source maps @@ -80,10 +77,10 @@ module.exports = function (grunt) { var resultPathMap = resultPath + '.map'; if (grunt.file.exists(map)) { - if (move) { - fs.renameSync(map, resultPathMap); - } else { + if (options.copy || (options.copy === null && el.dest)) { grunt.file.copy(map, resultPathMap); + } else { + fs.renameSync(map, resultPathMap); } // rewrite the sourceMappingURL in files 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 diff --git a/test/test.js b/test/test.js index 8e97ae8..ccf71b2 100644 --- a/test/test.js +++ b/test/test.js @@ -98,3 +98,35 @@ it('should allow a filename processing function', function () { var revisioned = fs.statSync(hashes[file]).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.6d5a.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.6d5a.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.6d5a.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.6d5a.png').size; + assert(revisioned === original); + var fileExists = fs.existsSync('test/tmp/movednocopyfile.png'); + assert(fileExists === false); +});