Skip to content

Commit

Permalink
Merge pull request #148 from bambooCZ/fuckedupCopyFile
Browse files Browse the repository at this point in the history
Fix copyFile method somewhat
  • Loading branch information
adam-lynch committed Jan 28, 2015
2 parents 2081efe + 06d07c8 commit 413df87
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,39 +82,35 @@ module.exports = {
return absolutePath.split(path.sep).length;
},
copyFile: function (src, dest, _event) {
var copy = function(resolve, reject, retryCount){
var onFailure = function(err){
if(retryCount > 2){
return reject(err);
}
else {
setTimeout(function(){
copy(resolve, reject, retryCount + 1);
}, 200);
}
};

return new Promise(function(resolve, reject) {
var stats = fs.lstatSync(src);
fs.copy(src, dest, function (err) {
if(err) return reject(err);

fs.exists(dest, function(exists){
var retryCount = 0;
var existsCallback = function(exists){
if(exists){
fs.chmod(dest, stats.mode, function(err){
// ignore error
_event.emit('log', 'chmod ' + stats.mode + ' on ' + dest + ' failed after copying, ignoring');
if (err) {
_event.emit('log', 'chmod ' + stats.mode + ' on ' + dest + ' failed after copying, ignoring');
}

resolve();
});
} else if (retryCount++ < 2) {
// This is antipattern!!!
// Callback should be called when the copy is finished!!!!
setTimeout(function(){
fs.exists(dest, existsCallback);
}, 1000);
} else {
reject(new Error("Copied file (" + dest + ") doesn't exist in destination after copying"));
}
else {
onFailure(new Error("Copied file (" + dest + ") doesn't exist in destination after copying"));
}
});
});
};
}

return new Promise(function(resolve, reject) {
copy(resolve, reject, 0);
fs.exists(dest, existsCallback);
});
});
},
mergeFiles: function (app, zipfile, chmod) {
Expand Down

0 comments on commit 413df87

Please sign in to comment.