npm / fstream

Advanced FS Streaming for Node
ISC License
208 stars 43 forks source link

read dir content but ignore it in the pipe #9

Open jfromaniello opened 12 years ago

jfromaniello commented 12 years ago

I have directory like:

bar/
   foo.txt
   baz.bin
   dir/
      another.txt

and I do something like

var reader = fstream.Reader({path: source, type: "Directory"}),
    writer =  fstream.Writer(path.join(source,'test.tgz')), 
    pack = tar.Pack(),
    gzip = zlib.createGzip();

reader.pipe(pack)
    .pipe(gzip)
    .pipe(writer); 

the problem I have is that the generated .tgz has the "bar" directory in the root and I'd like to have only the content of that directory (i.e. foo.txt, bar.bin, folder).

Is there a way i can do this?

thanks,

jfromaniello commented 12 years ago

I just found a very hacky way to do this by overwriting the emit method, and removing the root property of every emitted entry;

    var reader = fstream.Reader({path: source, type: "Directory"});
    oldEmit = reader.emit;
    reader.emit = function(ev, entry){
        if(ev === "entry") entry.root = null;
        return oldEmit.apply(reader, arguments);
    };
AaronO commented 11 years ago

@jfromaniello

A "better" solution is to use the filter option, and use it to modify the root of all the immediate entries of the the given directory. Hope this helps :

// Requires
var path = require('path');

var tar = require("tar");
var zlib = require("zlib");
var fstream = require("fstream");

function pipeTarGzDir(dirPath) {
    var fullPath = path.resolve(dirPath);
    return fstream.Reader({
        path: fullPath,
        type: "Directory",
        filter: function () {
            if(this.dirname == fullPath) {
                this.root = null;
            }
            return !(this.basename.match(/^\.git/) || this.basename.match(/^node_modules/));
        }
    })
    .pipe(tar.Pack())
    .pipe(zlib.createGzip());
}

// Exports
exports.pipeTarGzDir = pipeTarGzDir;