Created
August 8, 2015 07:32
-
-
Save okunishinishi/3ada94ce25f668766186 to your computer and use it in GitHub Desktop.
Node.jsでファイルをコピーする ref: http://qiita.com/okunishinishi@github/items/ec5cba6602a5bb872f13
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
$ cp src/*.html dest |
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
var r = fs.createReadStream(src), | |
w = fs.createWriteStream(dest); | |
r.pip(w); |
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
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; | |
} | |
} | |
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
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