Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
}
},

Expand All @@ -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', ''));
});
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"test": "grunt test"
},

"dependencies": {},
"dependencies": {
"async": "~0.9.0"
},

"devDependencies": {
"grunt-contrib-jshint": "~0.1.1",
Expand Down
106 changes: 55 additions & 51 deletions tasks/rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
});
};
23 changes: 23 additions & 0 deletions test/rename_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},

};