Skip to content

Instantly share code, notes, and snippets.

@artjomb
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artjomb/ba62deba39ca966c9422 to your computer and use it in GitHub Desktop.
Save artjomb/ba62deba39ca966c9422 to your computer and use it in GitHub Desktop.
phantomjs fs.overwrite
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