Comparing version 0.2.0 to 0.2.1
@@ -70,2 +70,5 @@ var fs = require('fs'); | ||
parentZip = folders[dir]; | ||
if (options.each) { | ||
options.each(fullPath); | ||
} | ||
folders[fullPath] = parentZip.folder(file); | ||
@@ -75,2 +78,5 @@ dive(fullPath, cb); | ||
fs.readFile(fullPath, function (err, data) { | ||
if (options.each) { | ||
options.each(path.join(dir, file)); | ||
} | ||
folders[dir].file(file, data); | ||
@@ -77,0 +83,0 @@ cb(err); |
{ | ||
"name": "zip-dir", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Zips up directories into buffers or saves zipped files to disk", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,5 +7,11 @@ # zip-dir | ||
## install | ||
``` | ||
$ npm install zip-dir | ||
``` | ||
## example | ||
``` | ||
```javascript | ||
var zipdir = require('zip-dir'); | ||
@@ -21,2 +27,14 @@ | ||
}); | ||
// Use a filter option to prevent zipping other zip files! | ||
// Keep in mind you have to allow a directory to descend into! | ||
zipdir('/path/to/be/zipped', { filter: (path, stat) => !/\.zip$/.test(path) }, function (err, buffer) { | ||
}); | ||
// Use an `each` option to call a function everytime a file is added, and receives the path | ||
zipdir('/path/to/be/zipped', { each: path => console.log(p, "added!"), function (err, buffer) { | ||
}); | ||
``` | ||
@@ -33,5 +51,5 @@ | ||
Zips up `dirPath` recursively preserving directory structure and returns | ||
the compressed buffer into `callback` on success. If `savePath` defined, the | ||
buffer will also be saved to disk at that path, with the callback also firing | ||
upon completion with the buffer. | ||
the compressed buffer into `callback` on success. If `options` defined with a | ||
`saveTo` path, then the callback will be delayed until the buffer has also | ||
been saved to disk. | ||
@@ -42,8 +60,7 @@ #### Options | ||
* `filter` A function that is called for all items to determine whether or not they should be added to the zip buffer. Function is called with the `fullPath` and a `stats` object ([fs.Stats](http://nodejs.org/api/fs.html#fs_class_fs_stats)). Return true to add the item; false otherwise. To include files within directories, directories must also pass this filter. | ||
* `each` A function that is called everytime a file or directory is added to the zip. | ||
## install | ||
## TODO | ||
``` | ||
$ npm install zip-dir | ||
``` | ||
* Add an option to not add empty directories if there are no valid children inside | ||
@@ -50,0 +67,0 @@ ## license |
@@ -125,2 +125,58 @@ var Zip = require("../jszip"); | ||
}); | ||
describe("`each` option", function () { | ||
afterEach(cleanUp); | ||
it("calls `each` with each path added to zip", function (done) { | ||
var paths = []; | ||
function each (p) { | ||
paths.push(p); | ||
} | ||
zipDir(sampleZipPath, { each: each }, function (err, buffer) { | ||
var files = [ | ||
"file1.json", | ||
"tiny.gif", | ||
"dir/", | ||
"dir/file2.json", | ||
"dir/file3.json", | ||
"dir/deepDir", | ||
"dir/deepDir/deeperDir", | ||
"dir/deepDir/deeperDir/file4.json" | ||
].map(function (p) { return path.join.apply(path, [sampleZipPath].concat(p.split("/"))); }); | ||
files.forEach(function (p) { | ||
expect(paths.indexOf(p)).to.not.equal(-1); | ||
return p; | ||
}); | ||
expect(paths.length).to.be.equal(files.length); | ||
done(); | ||
}); | ||
}); | ||
it("calls `each`, ignoring unadded files", function (done) { | ||
var paths = []; | ||
function each (p) { paths.push(p); } | ||
function filter (p) { return /\.json$/.test(p) || fs.statSync(p).isDirectory(); } | ||
zipDir(sampleZipPath, { each: each, filter: filter }, function (err, buffer) { | ||
var files = [ | ||
"file1.json", | ||
"dir/file2.json", | ||
"dir/file3.json", | ||
"dir/", | ||
"dir/deepDir", | ||
"dir/deepDir/deeperDir", | ||
"dir/deepDir/deeperDir/file4.json" | ||
].map(function (p) { return path.join.apply(path, [sampleZipPath].concat(p.split("/"))); }); | ||
files.forEach(function (p) { | ||
expect(paths.indexOf(p)).to.not.equal(-1); | ||
return p; | ||
}); | ||
expect(paths.length).to.be.equal(files.length); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -127,0 +183,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
145360
2425
66