2016-10-10 03:43:44 +02:00
|
|
|
'use strict';
|
2017-08-14 05:01:11 +02:00
|
|
|
const path = require('path');
|
|
|
|
const gutil = require('gulp-util');
|
|
|
|
const through = require('through2');
|
|
|
|
const archiver = require('archiver');
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
module.exports = (filename, opts) => {
|
2016-10-10 03:43:44 +02:00
|
|
|
if (!filename) {
|
|
|
|
throw new gutil.PluginError('gulp-tar', '`filename` required');
|
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
let firstFile;
|
|
|
|
const archive = archiver('tar', opts);
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
return through.obj((file, enc, cb) => {
|
2016-10-10 03:43:44 +02:00
|
|
|
if (file.relative === '') {
|
|
|
|
cb();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (firstFile === undefined) {
|
|
|
|
firstFile = file;
|
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
archive.append(file.contents, Object.assign({
|
2016-10-10 03:43:44 +02:00
|
|
|
name: file.relative.replace(/\\/g, '/') + (file.isNull() ? '/' : ''),
|
|
|
|
mode: file.stat && file.stat.mode,
|
|
|
|
date: file.stat && file.stat.mtime ? file.stat.mtime : null
|
|
|
|
}, opts));
|
|
|
|
|
|
|
|
cb();
|
|
|
|
}, function (cb) {
|
|
|
|
if (firstFile === undefined) {
|
|
|
|
cb();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
archive.finalize();
|
|
|
|
|
|
|
|
this.push(new gutil.File({
|
|
|
|
cwd: firstFile.cwd,
|
|
|
|
base: firstFile.base,
|
|
|
|
path: path.join(firstFile.base, filename),
|
|
|
|
contents: archive
|
|
|
|
}));
|
|
|
|
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
};
|