diff --git a/README.md b/README.md index e7131c2..83b91df 100644 --- a/README.md +++ b/README.md @@ -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) { + }); ``` diff --git a/index.js b/index.js index 651898a..da75282 100644 --- a/index.js +++ b/index.js @@ -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); diff --git a/package.json b/package.json index 793a686..51a28df 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test/zip-dir.test.js b/test/zip-dir.test.js index cb38695..cad8550 100644 --- a/test/zip-dir.test.js +++ b/test/zip-dir.test.js @@ -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 () { @@ -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; + } + + //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)); + } + ); + }); + } + + 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);