Skip to content

Commit

Permalink
Add format switches
Browse files Browse the repository at this point in the history
  • Loading branch information
astralarya committed Aug 15, 2016
1 parent c641b78 commit 07bf347
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,48 @@ WebpackArchivePlugin.prototype.apply = function(compiler) {
const output = options.output?
options.output:compiler.options.output.path;

// Create archivers
let zip = archiver('zip');
zip.pipe(fs.createWriteStream(`${output}.zip`));
let tar = archiver('tar', {
gzip: true,
gzipOptions: {
level: 1
// Create archive streams
let streams = [];
let zip = true;
let tar = true;
if(options.format) {
if(typeof options.format === 'string') {
zip = (options.format === 'zip');
tar = (options.format === 'tar');
} else if(Array.isArray(options.format)) {
zip = (options.format.indexOf('zip') != -1);
tar = (options.format.indexOf('tar') != -1);
}
});
tar.pipe(fs.createWriteStream(`${output}.tar.gz`));
}
if(zip) {
let stream = archiver('zip');
stream.pipe(fs.createWriteStream(`${output}.zip`));
streams.push(stream);
}
if(tar) {
let stream = archiver('tar', {
gzip: true,
gzipOptions: {
level: 1
}
});
stream.pipe(fs.createWriteStream(`${output}.tar.gz`));
streams.push(stream);
}

// Add assets
for(let asset in compiler.assets) {
if(compiler.assets.hasOwnProperty(asset)) {
zip.append(fs.createReadStream(compiler.assets[asset].existsAt), {name: asset});
tar.append(fs.createReadStream(compiler.assets[asset].existsAt), {name: asset});
for(let stream of streams) {
stream.append(fs.createReadStream(compiler.assets[asset].existsAt), {name: asset});
}
}
}

zip.finalize();
tar.finalize();
// Finalize streams
for(let stream of streams) {
stream.finalize();
}

callback();
});
Expand Down

0 comments on commit 07bf347

Please sign in to comment.