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.'); | |
}); | |
}()); |
Haha sorry. I read it quickly the first time and came back when I discovered the difference between rename and mv. Should have re-read it then. My bad.
Anyway, maybe my public bumbling stupidity will be helpful to someone else. Cheers!
UPDATE: this has finally been pushed to npm as fs.extra
URL: https://github.com/coolaj86/utile-fs/tree/master/fs.extra
good stuff
nice job!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dear Friend,
I kindly command that you retract your assertion at once!
See line 51
First I try fs.rename (same file-system) and if that fails, I try a copy (cross-filesystem).
In this fashion it is the same as
mv
: http://linux.die.net/man/1/mv:-D
P.S. If ye dare to cross me again and I shall tell you that your father smelt of elderberries and or fart in your general direction.
P.P.S. I'm not French, but for the sake of this argument I'd be willing to stoop to that level.
P.P.P.S. I, of course, intend no offense by my silly comments.