diff --git a/Gruntfile.js b/Gruntfile.js index 5b97fae..7f8b257 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -64,13 +64,21 @@ module.exports = function(grunt) { src: 'test/files/8', dest: 'eight' }, - + nineAndTen: { + src: ['test/files/9', 'test/files/10'], + dest: 'tmp/tmp/tmp/' + }, eleven: { src: 'test/files/11', dest: 'tmp/eleven', options: { ignore: true, } + }, + twoHundreds: { + expand: true, + src: 'test/files/20*', + dest: 'tmp/20/' } }, @@ -91,16 +99,31 @@ module.exports = function(grunt) { // Whenever the "test" task is run, first clean the "tmp" dir, then run this // plugin's task(s), then test the result. - grunt.registerTask('test', ['clean', 'make', 'rename', 'nodeunit', 'clean']); + grunt.registerTask('test', ['clean', 'make', 'rename', 'sleep', 'nodeunit', 'clean']); // By default, lint and run all tests. grunt.registerTask('default', ['jshint', 'test']); + grunt.registerTask('sleep', function () { + var done = this.async(); + + grunt.file.write('test/files/sleep', 'Sleeping'); + + setTimeout(function() { + grunt.file.write('test/files/sleep', 'Slept'); + done(); + }, 10000); + }); + grunt.registerTask('make', function () { var p = './test/files/'; for(var i = 1; i <= 10; i++) { grunt.file.write(path.join(p, '' + i, '')); } + + grunt.file.write(path.join(p, '201', '')); + grunt.file.write(path.join(p, '202', '')); + grunt.file.write(path.join(p, '203', '')); }); }; diff --git a/package.json b/package.json index 28dde07..4573f23 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,9 @@ "test": "grunt test" }, - "dependencies": {}, + "dependencies": { + "async": "~0.9.0" + }, "devDependencies": { "grunt-contrib-jshint": "~0.1.1", diff --git a/tasks/rename.js b/tasks/rename.js index 0260323..82f28f8 100644 --- a/tasks/rename.js +++ b/tasks/rename.js @@ -9,7 +9,8 @@ 'use strict'; var fs = require('fs'), - path = require('path'); + path = require('path'), + async = require('async'); module.exports = function(grunt) { grunt.registerMultiTask('rename', 'Move and/or rename files.', function() { @@ -18,71 +19,74 @@ module.exports = function(grunt) { ignore: false, }); - //console.log(options); - if (!this.files.length) { grunt.log.writeln('Moved '+'0'.cyan+' files.'); return done(); } - this.files.forEach(function (f) { - var dest = f.dest, - dir = path.dirname(dest); - - // Check if no source files were found - if (f.src.length === 0) { - // Continue if ignore is set - if (options.ignore) { - return done(); - } else { - grunt.fail.warn('Could not move file to ' + f.dest + ' it did not exist.'); - return done(); - } - } - - f.src.filter(function (file) { - // Resolve some conflicts because path doesn't work as I would - // expect - if (dest.lastIndexOf(path.sep) === dest.length - 1) { - dir = dest; - dest = path.join(dir, path.basename(file)); + var functions = this.files.map(function (f) { + return function(outerCallback) { + var dest = f.dest, + dir = path.dirname(dest), + destIsDir = (dest.lastIndexOf(path.sep) === dest.length - 1); + + // Check if no source files were found + if (f.src.length === 0) { + // Continue if ignore is set + if (!options.ignore) { + grunt.fail.warn('Could not move file to ' + f.dest + ' it did not exist.'); + } + return outerCallback(); } - grunt.file.mkdir(dir); + async.each(f.src, function (file, callback) { + var destFile = dest; - // First try builtin rename ability - fs.rename(file, dest, function (err) { - // Easy peasy - if (!err) { - grunt.verbose.writeln('Moved ' + file + ' to ' + dest); - return done(); + // Resolve some conflicts because path doesn't work as I would + // expect + if (destIsDir) { + dir = dest; + destFile = path.join(dir, path.basename(file)); } - // Now fallback to copying/unlinking - var read = fs.createReadStream(file); - var write = fs.createWriteStream(dest); + grunt.file.mkdir(dir); - read.on('error', function (err) { - grunt.fail.warn('Failed to read ' + file); - return done(); - }); + // First try builtin rename ability + fs.rename(file, destFile, function (err) { + // Easy peasy + if (!err) { + grunt.verbose.writeln('Moved ' + file + ' to ' + dest); + return callback(); + } - write.on('error', function (err) { - grunt.fail.warn('Failed to write to ' + dest); - return done(); - }); + // Now fallback to copying/unlinking + var read = fs.createReadStream(file); + var write = fs.createWriteStream(destFile); - write.on('close', function () { - // Now remove original file - grunt.file.delete(file); + read.on('error', function (err) { + grunt.fail.warn('Failed to read ' + file); + return callback(false); + }); - grunt.verbose.writeln('Moved ' + file + ' to ' + dest); - return done(); - }); + write.on('error', function (err) { + grunt.fail.warn('Failed to write to ' + destFile); + return callback(false); + }); - read.pipe(write); - }); - }); + write.on('close', function () { + // Now remove original file + grunt.file.delete(file); + + grunt.verbose.writeln('Moved ' + file + ' to ' + destFile); + return callback(); + }); + + read.pipe(write); + }); + }, function(err) { outerCallback(err); }); + }; }); + + async.waterfall(functions, done); }); }; diff --git a/test/rename_test.js b/test/rename_test.js index 6686b5b..9c76335 100644 --- a/test/rename_test.js +++ b/test/rename_test.js @@ -82,4 +82,27 @@ exports.rename = { test.ok(!grunt.file.exists('test/files/8'), 'test/files/8 should NOT exist'); test.done(); }, + + nineAndTen: function(test) { + test.expect(5); + test.ok(grunt.file.exists('tmp/tmp/tmp/9'), 'tmp/tmp/tmp/9 should exist'); + test.ok(grunt.file.exists('tmp/tmp/tmp/10'), 'tmp/tmp/tmp/10 should exist'); + test.ok(!grunt.file.exists('test/files/9'), 'test/files/9 should NOT exist'); + test.ok(!grunt.file.exists('test/files/10'), 'test/files/10 should NOT exist'); + test.equal(grunt.file.read('test/files/sleep'), 'Slept', 'Sleep should have finished'); + test.done(); + }, + + twoHundreds: function(test) { + test.expect(7); + test.ok(grunt.file.exists('tmp/20/test/files/201'), 'tmp/20/test/files/201 should exist'); + test.ok(grunt.file.exists('tmp/20/test/files/202'), 'tmp/20/test/files/202 should exist'); + test.ok(grunt.file.exists('tmp/20/test/files/203'), 'tmp/20/test/files/203 should exist'); + test.ok(!grunt.file.exists('test/files/201'), 'test/files/201 should NOT exist'); + test.ok(!grunt.file.exists('test/files/202'), 'test/files/202 should NOT exist'); + test.ok(!grunt.file.exists('test/files/203'), 'test/files/203 should NOT exist'); + test.equal(grunt.file.read('test/files/sleep'), 'Slept', 'Sleep should have finished'); + test.done(); + }, + };