feature(pipe) add all

This commit is contained in:
coderaiser 2014-11-01 05:05:38 -04:00
parent 6b4049d257
commit a6fd6bda2d
2 changed files with 61 additions and 9 deletions

View file

@ -6,6 +6,7 @@
path = require('path'),
fs = require('fs'),
zlib = require('zlib'),
tryRequire = require(DIR + 'tryRequire'),
@ -17,7 +18,16 @@
exports.pack = function(from, to, callback) {
isDir(from, function(is) {
var stream, dir, name,
var dir, name,
optionsDir = { path: from, type: 'Directory' },
optionsTar = { noProprietary: true },
streamDir,
streamTar,
streamZip = zlib.createGzip(),
streamFile,
isStr = Util.type.string(to),
options = {
gzip: true
@ -26,18 +36,25 @@
if (!is || !fstream || !tar) {
pipe(from, to, options, callback);
} else {
stream = fstream
.Reader({ path: from, type: 'Directory' })
.on('error', callback)
.pipe(tar.Pack({ noProprietary: true }));
streamDir = fstream.Reader(optionsDir);
streamTar = tar.Pack(optionsTar);
if (isStr) {
if (!isStr) {
streamFile = to;
} else {
dir = path.dirname(to);
name = path.basename(to);
name = path.basename(to, '.gz');
to = dir + path.sep + name + '.tar.gz';
streamFile = fs.createWriteStream(to);
}
pipe(stream, to, options, callback);
pipe.all([
streamDir,
streamTar,
streamZip,
streamFile
], callback);
}
});
};

View file

@ -8,7 +8,8 @@
type = Util.type;
module.exports = create;
module.exports.getBody = getBody;
module.exports.getBody = getBody;
module.exports.all = all;
/**
* create pipe
@ -94,6 +95,40 @@
emitter.on(event, callback);
}
function all(streams, callback) {
var n, write, isFSWrite;
Util.checkArgs(arguments, ['streams', 'callback']);
n = streams.length - 1;
write = streams[n];
isFSWrite = write instanceof fs.WriteStream;
Util.exec.if(!isFSWrite, function() {
pipe(streams, callback);
}, function(callback) {
write.on('open', callback);
});
}
function pipe(streams, callback) {
var main,
read = streams[0];
Util.checkArgs(arguments, ['streams', 'callback']);
streams.forEach(function(stream) {
on('error', stream, callback);
if (!main)
main = stream;
else
main = main.pipe(stream);
});
on('end', read, callback);
}
/**
* get body of readStream
*