From 3b7e67240a600f9f9a677a77e7909e6ab2db26b2 Mon Sep 17 00:00:00 2001 From: Bacra Date: Wed, 21 Oct 2015 22:35:36 +0800 Subject: [PATCH] Fix: repeatedly execute `done` --- package.json | 99 +++++++++++++++++++++++-------------------------- tasks/rename.js | 22 ++++++----- 2 files changed, 59 insertions(+), 62 deletions(-) diff --git a/package.json b/package.json index 28dde07..2ce36f2 100644 --- a/package.json +++ b/package.json @@ -1,55 +1,48 @@ { - "name": "grunt-rename", - "description": "Move and/or rename files.", - "version": "0.1.4", - "homepage": "https://github.com/jdavis/grunt-rename", - - "author": { - "name": "Josh Davis", - "email": "josh@joshldavis.com", - "url": "http://joshldavis.com" - }, - - "repository": { - "type": "git", - "url": "git://github.com/jdavis/grunt-rename.git" - }, - - "bugs": { - "url": "https://github.com/jdavis/grunt-rename/issues" - }, - - "licenses": [{ - "type": "MIT", - "url": "https://github.com/jdavis/grunt-rename/blob/master/LICENSE-MIT" - }], - - "main": "Gruntfile.js", - - "engines": { - "node": ">= 0.8.0" - }, - - "scripts": { - "test": "grunt test" - }, - - "dependencies": {}, - - "devDependencies": { - "grunt-contrib-jshint": "~0.1.1", - "grunt-contrib-clean": "~0.4.0", - "grunt-contrib-nodeunit": "~0.1.2", - "grunt": "~0.4.1" - }, - - "peerDependencies": { - "grunt": "~0.4.1" - }, - - "keywords": [ - "gruntplugin", - "move", - "rename" - ] + "name": "grunt-rename", + "description": "Move and/or rename files.", + "version": "0.1.4", + "homepage": "https://github.com/jdavis/grunt-rename", + "author": { + "name": "Josh Davis", + "email": "josh@joshldavis.com", + "url": "http://joshldavis.com" + }, + "repository": { + "type": "git", + "url": "git://github.com/jdavis/grunt-rename.git" + }, + "bugs": { + "url": "https://github.com/jdavis/grunt-rename/issues" + }, + "licenses": [ + { + "type": "MIT", + "url": "https://github.com/jdavis/grunt-rename/blob/master/LICENSE-MIT" + } + ], + "main": "Gruntfile.js", + "engines": { + "node": ">= 0.8.0" + }, + "scripts": { + "test": "grunt test" + }, + "dependencies": { + "async": "^1.4.2" + }, + "devDependencies": { + "grunt-contrib-jshint": "~0.1.1", + "grunt-contrib-clean": "~0.4.0", + "grunt-contrib-nodeunit": "~0.1.2", + "grunt": "~0.4.1" + }, + "peerDependencies": { + "grunt": "~0.4.1" + }, + "keywords": [ + "gruntplugin", + "move", + "rename" + ] } diff --git a/tasks/rename.js b/tasks/rename.js index 0260323..fe98295 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() { @@ -25,7 +26,7 @@ module.exports = function(grunt) { return done(); } - this.files.forEach(function (f) { + async.eachLimit(this.files, 5, function (f, cb) { var dest = f.dest, dir = path.dirname(dest); @@ -33,10 +34,10 @@ module.exports = function(grunt) { if (f.src.length === 0) { // Continue if ignore is set if (options.ignore) { - return done(); + return cb(); } else { grunt.fail.warn('Could not move file to ' + f.dest + ' it did not exist.'); - return done(); + return cb(); } } @@ -55,7 +56,7 @@ module.exports = function(grunt) { // Easy peasy if (!err) { grunt.verbose.writeln('Moved ' + file + ' to ' + dest); - return done(); + return cb(); } // Now fallback to copying/unlinking @@ -64,12 +65,14 @@ module.exports = function(grunt) { read.on('error', function (err) { grunt.fail.warn('Failed to read ' + file); - return done(); + cb && cb(); + cb = null; }); write.on('error', function (err) { grunt.fail.warn('Failed to write to ' + dest); - return done(); + cb && cb(); + cb = null; }); write.on('close', function () { @@ -77,12 +80,13 @@ module.exports = function(grunt) { grunt.file.delete(file); grunt.verbose.writeln('Moved ' + file + ' to ' + dest); - return done(); + cb && cb(); + cb = null; }); read.pipe(write); }); }); - }); + }, done); }); };