Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

read dir content but ignore it in the pipe #9

Open
jfromaniello opened this issue Jun 5, 2012 · 2 comments
Open

read dir content but ignore it in the pipe #9

jfromaniello opened this issue Jun 5, 2012 · 2 comments

Comments

@jfromaniello
Copy link

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
Copy link
Author

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
Copy link

AaronO commented Mar 15, 2013

@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;

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants