Skip to content

Commit

Permalink
Use vinyl and vinyl-fs to handle files
Browse files Browse the repository at this point in the history
  • Loading branch information
kevva committed Sep 13, 2014
1 parent d355033 commit 5a174ff
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 195 deletions.
1 change: 0 additions & 1 deletion .gitignore
@@ -1,2 +1 @@
node_modules
test/tmp
164 changes: 46 additions & 118 deletions index.js
@@ -1,9 +1,10 @@
'use strict';

var each = require('each-async');
var fs = require('fs-extra');
var path = require('path');
var Ware = require('ware');
var combine = require('stream-combiner');
var concat = require('concat-stream');
var File = require('vinyl');
var fs = require('vinyl-fs');
var through = require('through2');

/**
* Initialize Decompress
Expand All @@ -19,25 +20,13 @@ function Decompress(opts) {

this.opts = opts || {};
this.opts.mode = parseInt(this.opts.mode, 8) || null;
this.ware = new Ware();
this.streams = [];
}

/**
* Add a plugin to the middleware stack
*
* @param {Function} plugin
* @api public
*/

Decompress.prototype.use = function (plugin) {
this.ware.use(plugin);
return this;
};

/**
* Get or set the source file
* Get or set the source files
*
* @param {String|Buffer} file
* @param {Array|Buffer|String} file
* @api public
*/

Expand All @@ -51,143 +40,82 @@ Decompress.prototype.src = function (file) {
};

/**
* Get or set the destination path
* Get or set the destination folder
*
* @param {String} path
* @param {String} dir
* @api public
*/

Decompress.prototype.dest = function (path) {
Decompress.prototype.dest = function (dir) {
if (!arguments.length) {
return this._dest;
}

this._dest = path;
this._dest = dir;
return this;
};

/**
* Decompress archive
*
* @param {Function} cb
* @api public
*/

Decompress.prototype.decompress = function (cb) {
cb = cb || function () {};
var self = this;

this.read(function (err, file) {
if (!file || file.contents.length === 0) {
cb();
return;
}

if (err) {
cb(err);
return;
}

self.run(file, function (err) {
if (err) {
cb(err);
return;
}

self.write(self.files, function (err) {
cb(err, file);
});
});
});
};

/**
* Run a file through the middleware
* Add a plugin to the middleware stack
*
* @param {Object} file
* @param {Function} cb
* @param {Function} plugin
* @api public
*/

Decompress.prototype.run = function (file, cb) {
this.ware.run(file, this, cb);
Decompress.prototype.use = function (plugin) {
this.streams.push(plugin);
return this;
};

/**
* Read the archive
* Decompress archive
*
* @param {Function} cb
* @api public
*/

Decompress.prototype.read = function (cb) {
var file = {};
var src = this.src();
Decompress.prototype.run = function (cb) {
cb = cb || function () {};
this.streams.unshift(this.read(this.src()));

if (Buffer.isBuffer(src)) {
file.contents = src;
cb(null, file);
return;
if (this.dest()) {
this.streams.push(fs.dest(this.dest(), this.opts));
}

fs.readFile(src, function (err, buf) {
if (err) {
cb(err);
return;
}

file.contents = buf;
file.path = src;

var pipe = combine(this.streams);
var end = concat(function (file) {
cb(null, file);
});

pipe.on('error', function (err) {
cb(err);
return;
});

pipe.pipe(end);
};

/**
* Write files to destination
* Read the source files
*
* @param {Array} files
* @param {Function} cb
* @api public
* @param {Array|Buffer|String} src
* @api private
*/

Decompress.prototype.write = function (files, cb) {
var dest = this.dest();
var mode = this.opts.mode;
Decompress.prototype.read = function (src) {
if (Buffer.isBuffer(src)) {
var stream = through.obj(function (file, enc, cb) {
cb(null, file);
});

stream.end(new File({
contents: src
}));

if (!dest || !files) {
cb();
return;
return stream;
}

each(files, function (file, i, done) {
fs.outputFile(path.join(dest, file.path), file.contents, function (err) {
if (err) {
done(err);
return;
}

if (mode) {
return fs.chmod(path.join(dest, file.path), mode, function (err) {
if (err) {
cb(err);
return;
}

done();
});
}

done();
});
}, function (err) {
if (err) {
cb(err);
return;
}

cb();
});
return fs.src(src);
};

/**
Expand Down
8 changes: 5 additions & 3 deletions package.json
Expand Up @@ -30,14 +30,16 @@
"zip"
],
"dependencies": {
"concat-stream": "^1.4.6",
"decompress-tar": "^1.0.0",
"decompress-tarbz2": "^1.0.1",
"decompress-targz": "^1.0.0",
"decompress-unzip": "^1.0.0",
"each-async": "^1.0.0",
"fs-extra": "^0.11.0",
"nopt": "^3.0.1",
"ware": "^0.3.0"
"stream-combiner": "^0.2.1",
"through2": "^0.6.1",
"vinyl": "^0.4.3",
"vinyl-fs": "^0.3.7"
},
"devDependencies": {
"ava": "0.0.4",
Expand Down
94 changes: 21 additions & 73 deletions test/test.js
@@ -1,122 +1,70 @@
'use strict';

var Decompress = require('../');
var fs = require('fs');
var path = require('path');
var rm = require('rimraf');
var test = require('ava');

test('extract .tar', function (t) {
t.plan(2);

var decompress = new Decompress()
.src(path.join(__dirname, 'fixtures/test.tar'))
.dest(path.join(__dirname, 'tmp'))
.use(Decompress.tar());

decompress.decompress(function (err) {
decompress.run(function (err, files) {
t.assert(!err);

fs.exists(path.join(decompress.dest(), 'test.jpg'), function (exists) {
t.assert(exists);

rm(decompress.dest(), function (err) {
t.assert(!err);
});
});
t.assert(files[0].path === 'test.jpg');
});
});

test('extract .tar.bz2', function (t) {
t.plan(2);

var decompress = new Decompress()
.src(path.join(__dirname, 'fixtures/test.tar.bz2'))
.dest(path.join(__dirname, 'tmp'))
.use(Decompress.tarbz2());

decompress.decompress(function (err) {
decompress.run(function (err, files) {
t.assert(!err);

fs.exists(path.join(decompress.dest(), 'test.jpg'), function (exists) {
t.assert(exists);

rm(decompress.dest(), function (err) {
t.assert(!err);
});
});
t.assert(files[0].path === 'test.jpg');
});
});

test('extract .tar.gz', function (t) {
t.plan(2);

var decompress = new Decompress()
.src(path.join(__dirname, 'fixtures/test.tar.gz'))
.dest(path.join(__dirname, 'tmp'))
.use(Decompress.targz());

decompress.decompress(function (err) {
decompress.run(function (err, files) {
t.assert(!err);

fs.exists(path.join(decompress.dest(), 'test.jpg'), function (exists) {
t.assert(exists);

rm(decompress.dest(), function (err) {
t.assert(!err);
});
});
t.assert(files[0].path === 'test.jpg');
});
});

test('extract .zip', function (t) {
t.plan(2);

var decompress = new Decompress()
.src(path.join(__dirname, 'fixtures/test.zip'))
.dest(path.join(__dirname, 'tmp'))
.use(Decompress.zip());

decompress.decompress(function (err) {
decompress.run(function (err, files) {
t.assert(!err);

fs.exists(path.join(decompress.dest(), 'test.jpg'), function (exists) {
t.assert(exists);

rm(decompress.dest(), function (err) {
t.assert(!err);
});
});
t.assert(files[0].path === 'test.jpg');
});
});

test('extract .zip using the strip option', function (t) {
test('extract using the strip option', function (t) {
t.plan(2);

var decompress = new Decompress()
.src(path.join(__dirname, 'fixtures/test-strip.zip'))
.dest(path.join(__dirname, 'tmp'))
.use(Decompress.zip({ strip: 1 }));

decompress.decompress(function (err) {
t.assert(!err);

fs.exists(path.join(decompress.dest(), 'test-strip.jpg'), function (exists) {
t.assert(exists);

rm(decompress.dest(), function (err) {
t.assert(!err);
});
});
});
});

test('extract .zip and set mode on extracted file', function (t) {
var decompress = new Decompress({ mode: 777 })
.src(path.join(__dirname, 'fixtures/test.zip'))
.dest(path.join(__dirname, 'tmp'))
.use(Decompress.zip());

decompress.decompress(function (err) {
decompress.run(function (err, files) {
t.assert(!err);

fs.stat(path.join(decompress.dest(), 'test.jpg'), function (err, stats) {
t.assert(!err);
t.assert(stats.mode.toString(8) === '100777');

rm(decompress.dest(), function (err) {
t.assert(!err);
});
});
t.assert(files[0].path === 'test-strip.jpg');
});
});

0 comments on commit 5a174ff

Please sign in to comment.