Skip to content

Instantly share code, notes, and snippets.

@coolaj86
Last active May 22, 2022 22:45
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save coolaj86/992478 to your computer and use it in GitHub Desktop.
Save coolaj86/992478 to your computer and use it in GitHub Desktop.
fs.copy and fs.move for Node.JS
/*
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);
});
};
}());
/*
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.');
});
}());
@coolaj86
Copy link
Author

UPDATE: this has finally been pushed to npm as fs.extra

URL: https://github.com/coolaj86/utile-fs/tree/master/fs.extra

@victusfate
Copy link

good stuff

@luics
Copy link

luics commented Nov 17, 2012

nice job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment