aboutsummaryrefslogtreecommitdiff
path: root/node_modules/gulp-tar/index.js
blob: 40ccbb3fd9fc9ec8fe3a15589be9df2b69504557 (plain)
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
50
'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, opts) {
	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,
			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();
	});
};