Skip to content

Instantly share code, notes, and snippets.

@okunishinishi
Created August 8, 2015 07:32
Show Gist options
  • Save okunishinishi/3ada94ce25f668766186 to your computer and use it in GitHub Desktop.
Save okunishinishi/3ada94ce25f668766186 to your computer and use it in GitHub Desktop.
Node.jsでファイルをコピーする ref: http://qiita.com/okunishinishi@github/items/ec5cba6602a5bb872f13
$ cp src/*.html dest
var r = fs.createReadStream(src),
w = fs.createWriteStream(dest);
r.pip(w);
function _copySingle(src, dest, callback) {
var aborted = false;
var r = fs.createReadStream(src),
w = fs.createWriteStream(dest);
r.on("error", function (err) {
done(err);
});
w.on("error", function (err) {
done(err);
});
w.on("close", function (ex) {
done();
});
r.pipe(w);
function done(err) {
if (aborted) {
return;
}
callback(err);
aborted = true;
}
}
var argx = require('argx'),
async = require('async'),
path = require('path'),
glob = require('glob'),
mkdirp = require('mkdirp'),
fs = require('fs');
async.waterfall([
function (callback) {
async.concatSeries([].concat(src), glob, callback);
},
function (src, callback) {
_isExistingDir(dest, function (destIsDir) {
async.each(src, function (src, callback) {
var srcFilename = path.resolve(src),
destFilename = destIsDir ? path.join(dest, path.basename(src)) : path.resolve(dest);
async.series([
function (callback) {
if (options.mkdirp) {
mkdirp(path.dirname(destFilename), callback);
} else {
callback(null);
}
},
function (callback) {
_copySingle(srcFilename, destFilename, callback);
}
], callback);
}, callback);
});
}
], callback);
function _isExistingDir(filename, callback) {
fs.exists(filename, function (exists) {
if (exists) {
fs.stat(filename, function (err, stat) {
callback(!err && stat.isDirectory());
});
} else {
callback(false);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment