Last active
May 22, 2022 22:45
-
-
Save coolaj86/992478 to your computer and use it in GitHub Desktop.
fs.copy and fs.move for Node.JS
This file contains 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
/* | |
UPDATE: this has finally been pushed to npm as `fs.extra` | |
URL: https://github.com/coolaj86/utile-fs/tree/master/fs.extra | |
*/ | |
(function () { | |
"use strict"; | |
console.warn('[Deprecated] See https://github.com/coolaj86/utile-fs'); | |
var fs = require('fs') | |
, util = require('util') | |
; | |
fs.copy = function (src, dst, cb) { | |
console.warn('[Deprecated] See https://github.com/coolaj86/utile-fs'); | |
function copy(err) { | |
var is | |
, os | |
; | |
if (!err) { | |
return cb(new Error("File " + dst + " exists.")); | |
} | |
fs.stat(src, function (err) { | |
if (err) { | |
return cb(err); | |
} | |
is = fs.createReadStream(src); | |
os = fs.createWriteStream(dst); | |
util.pump(is, os, cb); | |
}); | |
} | |
fs.stat(dst, copy); | |
}; | |
fs.move = function (src, dst, cb) { | |
console.warn('[Deprecated] See https://github.com/coolaj86/utile-fs'); | |
function copyIfFailed(err) { | |
if (!err) { | |
return cb(null); | |
} | |
fs.copy(src, dst, function(err) { | |
if (!err) { | |
// TODO | |
// should we revert the copy if the unlink fails? | |
fs.unlink(src, cb); | |
} else { | |
cb(err); | |
} | |
}); | |
} | |
fs.stat(dst, function (err) { | |
if (!err) { | |
return cb(new Error("File " + dst + " exists.")); | |
} | |
fs.rename(src, dst, copyIfFailed); | |
}); | |
}; | |
}()); |
This file contains 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
/* | |
UPDATE: this has finally been pushed to npm as `fs.extra` | |
URL: https://github.com/coolaj86/node-examples-js/tree/master/fs.extra | |
*/ | |
(function () { | |
"use strict"; | |
require('./fs.copy'); | |
var fs = require('fs') | |
, sequence = require('futures').sequence() | |
, exec = require('child_process').exec | |
, count = 0 | |
; | |
function fsop(op, a, b, shouldpass) { | |
var c = count; | |
count += 1; | |
return function (n) { | |
function log(err) { | |
if (shouldpass && !err) { | |
console.log(c, op, a, b, 'passed'); | |
n(); | |
} else if (!shouldpass && err) { | |
console.log(c, op, a, b, 'passed'); | |
n(); | |
} else { | |
console.log(c, op, a, b, 'failed'); | |
} | |
} | |
fs[op]( | |
'file.' + a + '.t.txt' | |
, 'file.' + b + '.t.txt' | |
, log | |
); | |
}; | |
} | |
sequence | |
.then(function (next) { | |
exec('rm ./file.*.t.txt; touch file.0.t.txt', next); | |
}) // 0 | |
.then(fsop('copy', 0, 1, true)) // 0, 1 | |
.then(fsop('copy', 0, 2, true)) // 0, 1, 2 | |
.then(fsop('copy', 0, 1, false)) // 0, 1, 2 | |
.then(fsop('copy', 3, 4, false)) // 0, 1, 2 | |
.then(fsop('move', 0, 3, true)) // 1, 2, 3 | |
.then(fsop('move', 0, 4, false)) // 1, 2, 3 | |
.then(fsop('move', 4, 0, false)) // 1, 2, 3 | |
.then(fsop('move', 5, 6, false)) // 1, 2, 3 | |
.then(fsop('move', 3, 0, true)) // 0, 1, 2 | |
.then(fsop('move', 2, 0, false)) // 0, 1, 2 | |
.then(function () { | |
console.log('All Done.'); | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice job!