2020-04-07 11:53:28 +03:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
|
|
|
|
const defaultOptions = {
|
|
|
|
type: 'zip',
|
|
|
|
glob: '**/*',
|
2020-05-08 18:15:02 +03:00
|
|
|
dot: true,
|
2020-04-07 11:53:28 +03:00
|
|
|
ignore: ['node_modules/**']
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compress
|
|
|
|
*
|
|
|
|
* - Create a zip file from a folder
|
|
|
|
*
|
|
|
|
* @param {String} folderToZip - full path to the folder to be zipped
|
|
|
|
* @param {String} destination - full path to the resulting zip file
|
|
|
|
* @param {Object} [options]
|
|
|
|
* @param {String} options.type - zip by default see archiver for other options
|
|
|
|
* @param {String} options.glob - the files to include, defaults to all files and folders
|
2020-05-08 18:15:02 +03:00
|
|
|
* @param {Boolean} options.dot - include all dotfiles and dotfolders
|
2020-04-07 11:53:28 +03:00
|
|
|
* @param {Array} options.ignore - any paths that should be ignored, sets node_modules by default
|
2020-05-08 18:15:02 +03:00
|
|
|
*
|
2020-04-07 11:53:28 +03:00
|
|
|
*/
|
|
|
|
module.exports = (folderToZip, destination, options = {}) => {
|
|
|
|
const opts = Object.assign({}, defaultOptions, options);
|
|
|
|
|
|
|
|
const archiver = require('archiver');
|
|
|
|
const output = fs.createWriteStream(destination);
|
2020-05-08 18:15:02 +03:00
|
|
|
const archive = archiver.create(opts.type);
|
2020-04-07 11:53:28 +03:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// If folder to zip is a symlink, we want to get the target
|
|
|
|
// of the link and zip that instead of zipping the symlink
|
|
|
|
if (fs.lstatSync(folderToZip).isSymbolicLink()) {
|
|
|
|
folderToZip = fs.realpathSync(folderToZip);
|
|
|
|
}
|
|
|
|
|
|
|
|
output.on('close', function () {
|
|
|
|
resolve({path: destination, size: archive.pointer()});
|
|
|
|
});
|
|
|
|
|
|
|
|
archive.on('error', function (err) {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
archive.glob(opts.glob, {
|
|
|
|
cwd: folderToZip,
|
2020-05-08 18:15:02 +03:00
|
|
|
dot: opts.dot,
|
2020-04-07 11:53:28 +03:00
|
|
|
ignore: opts.ignore
|
|
|
|
});
|
|
|
|
archive.pipe(output);
|
|
|
|
archive.finalize();
|
|
|
|
});
|
|
|
|
};
|