node-archiver icon indicating copy to clipboard operation
node-archiver copied to clipboard

Ignore does not work in the subfolder in the archive.

Open BeycanDeveloper opened this issue 2 years ago • 0 comments

My code is as follows and what I want to do is this. Exporting everything in the current folder into a zip. However, a folder will be created in the zip and this folder will be the name of the current folder. So far everything ok. But the ignore parameter doesn't work. If I don't use an "archive.directory" method ignore works and dumps everything else into the zip without getting the ".git" folder. However, I want to have a folder in the zip. When I do this, "ignore" doesn't work and it also zips the ".git" folder.

How can I solve this?

#!/usr/bin/env node

const path = require('path');
const fs = require('fs');
const fse = require('fs-extra');
const archiver = require('archiver');

const currentFolder = process.cwd();
const fileName = path.basename(currentFolder);
const parentFolder = path.resolve(currentFolder, '..');
const outputZipFile = fs.createWriteStream(currentFolder + '/'+fileName+'.zip');

module.exports = function() {
    const archive = archiver('zip', { zlib: { level: 9 } });

    outputZipFile.on('close', function () {
        console.log(archive.pointer() + ' total bytes');
        console.log('archiver has been finalized and the output file descriptor has closed.');
    });
    
    archive.on('error', function(err){
        throw err;
    });

    archive.pipe(outputZipFile);

    archive.directory(currentFolder, fileName);

    archive.glob('**', {
        cwd: currentFolder,
        ignore: ['.git', 'node_modules', '*.zip'],
        expand:true, flatten:true
    });

    archive.finalize(function(err, bytes) {
		if(err) {
			console.log(err);
		}
	});
};

BeycanDeveloper avatar Dec 31 '22 18:12 BeycanDeveloper