forked from sindresorhus/gulp-tar
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
49 lines (40 loc) · 996 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var objectAssign = require('object-assign');
var archiver = require('archiver');
module.exports = function (filename, options) {
if (!filename) {
throw new gutil.PluginError('gulp-tar', '`filename` required');
}
var firstFile;
var archive = archiver('tar');
return through.obj(function (file, enc, cb) {
if (file.relative === '') {
cb();
return;
}
if (firstFile === undefined) {
firstFile = file;
}
archive.append(file.contents, objectAssign({
name: file.relative.replace(/\\/g, '/') + (file.isNull() ? '/' : ''),
mode: file.stat && file.stat.mode
}, options || {}));
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();
});
};