Last active
August 29, 2015 14:05
-
-
Save artjomb/ba62deba39ca966c9422 to your computer and use it in GitHub Desktop.
phantomjs fs.overwrite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
fs.overwrite = function(source, destination, maxTrials){ | |
var overwritten = false; | |
var trials = 0; | |
maxTrials = parseInt(maxTrials) | |
maxTrials = !!maxTrials ? maxTrials : null; | |
while(!overwritten) { | |
if (maxTrials && trials > maxTrials) { | |
return -1; | |
} | |
try { | |
this.copy(source, destination); | |
overwritten = true; | |
} catch(e) { | |
if (fs.exists(destination)) { | |
fs.remove(destination); | |
} else { | |
return -2; | |
} | |
} | |
trials++; | |
} | |
return trials; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment