Ghost/ghost/zip/lib/compress.js
Hannah Wolfe 7964f6ec82 Added dotfile support and set it on by default
refs https://github.com/TryGhost/Ghost/issues/11794

- archiver has an undocumented dot option, that allows you to include dotfiles
- our existing tests had a dotfile, but we didn't check properly that everything exists
- Swapped to using folder-hash to compare if the original, and compressed-then-decompressed folders are identical
- Added an example of a dotfolder with nested dotfile
- Updated compress to use the dot option, make it optional but on by default
2020-05-08 16:21:20 +01:00

55 lines
1.7 KiB
JavaScript

const fs = require('fs-extra');
const Promise = require('bluebird');
const defaultOptions = {
type: 'zip',
glob: '**/*',
dot: true,
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
* @param {Boolean} options.dot - include all dotfiles and dotfolders
* @param {Array} options.ignore - any paths that should be ignored, sets node_modules by default
*
*/
module.exports = (folderToZip, destination, options = {}) => {
const opts = Object.assign({}, defaultOptions, options);
const archiver = require('archiver');
const output = fs.createWriteStream(destination);
const archive = archiver.create(opts.type);
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,
dot: opts.dot,
ignore: opts.ignore
});
archive.pipe(output);
archive.finalize();
});
};