Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ zipdir('/path/to/be/zipped', { filter: (path, stat) => !/\.zip$/.test(path) }, f
// 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) {

});

// Use a `innerFolderName` option which allows you to add an inner folder with a given name.
zipdir('/path/to/be/zipped', { innerFolderName: 'someFolderNameAsString'), function (err, buffer) {

});

```
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ function zipBuffer (rootDir, options, callback) {
// Resolve the path so we can remove trailing slash if provided
rootDir = path.resolve(rootDir);

folders[rootDir] = zip;
//Add option to change inner folder name
if(options.innerFolderName && typeof options.innerFolderName === 'string'){
folders[rootDir] = zip.folder(options.innerFolderName);
}else{
folders[rootDir] = zip;
}

dive(rootDir, function (err) {
if (err) return callback(err);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "zip-dir",
"version": "2.0.0",
"description": "Zips up directories into buffers or saves zipped files to disk",
"main": "index.js",
"main": "./test/zip-dir.test.js",
"scripts": {
"test": "./node_modules/.bin/mocha --reporter spec --ui bdd"
},
Expand Down
81 changes: 81 additions & 0 deletions test/zip-dir.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var xpiPath = path.join(__dirname, "my.xpi");
var outputPath = path.join(__dirname, "myxpi/");
var emptyDirPath = path.join(sampleZipPath, "emptyDir");
var emptyDirOutputPath = path.join(outputPath, "emptyDir");
const innerFolderName = "exampleOfAnInnerFolderName";

describe("zip-dir", function () {
describe("creates a zip buffer", function () {
Expand Down Expand Up @@ -99,6 +100,86 @@ describe("zip-dir", function () {
});
});

describe("Ensures that inner folder names can be defined", function () {
afterEach(cleanUp);

function customCompareMethod(file, customFilePath = "", combineCustomFilePathOrReplace = true) {
const zipBuffer = fs.readFileSync(path.join(sampleZipPath, file));
const combinedFilePath = path.join(customFilePath, file);
// Compare the different file buffers.
const fileBuffer = fs.readFileSync(
path.join(
outputPath,
combineCustomFilePathOrReplace ? combinedFilePath : customFilePath
)
);

expect(bufferEqual(zipBuffer, fileBuffer)).to.be.ok;
}
Comment on lines +106 to +118
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can move the this function to the global def, but thought i would ask before changing the other aspects. :)


//Had to do the below, since otherwise the file did not zip in enough time.
function customZippingFunction(options, customFilePath) {
return new Promise((resolve, reject) => {
zipDir(
customFilePath || sampleZipPath,
options,
async function (err, buffer) {
if (err) {
reject(err);
return;
}
await fs
.createReadStream(xpiPath)
.pipe(unzip.Extract({ path: outputPath }))
.on("entry", (entry) => entry.autodrain())
.promise()
.then(() => resolve())
.catch(e=>reject(e));
}
);
});
}
Comment on lines +120 to +141
Copy link
Author

@jeesonjohnson jeesonjohnson Jan 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to wrap the zipping command into its own promise since it was being flaky when running tests, since the zipping often took way too long, and zipDir cannot directly be awaited.


it("Ensure inner folder name is not changed unless specified ", function (done) {
zipAndUnzip({ saveTo: xpiPath }, function () {
var files = [
"file1.json",
"tiny.gif",
"dir/file2.json",
"dir/file3.json",
"dir/deepDir/deeperDir/file4.json",
];
files.forEach((file) => customCompareMethod(file));
done();
});
});

it("Ensure that with multiple files the inner folder name is changed.", function (done) {
zipAndUnzip({ saveTo: xpiPath, innerFolderName }, function () {
var files = [
"file1.json",
"tiny.gif",
"dir/file2.json",
"dir/file3.json",
"dir/deepDir/deeperDir/file4.json",
];
files.forEach((file) => customCompareMethod(file, innerFolderName));
done();
});
});

it("compresses and unpacks and all files into ", async function () {
const fileFolderPath = "dir/deepDir/deeperDir";

const singleFileToBeZipped = path.join(sampleZipPath, fileFolderPath);
addEmpty(await customZippingFunction({ saveTo: xpiPath, innerFolderName },singleFileToBeZipped));

const fileName = "file4.json";
const fileToBeZippedPath = path.join(fileFolderPath, fileName);
const fileToBeComparedTo = path.join(innerFolderName, fileName);
customCompareMethod(fileToBeZippedPath, fileToBeComparedTo, false);
});
});
describe("uses `filter` to select items", function () {
afterEach(cleanUp);

Expand Down