aboutsummaryrefslogtreecommitdiff
path: root/node_modules/gulp-tar/index.js
blob: fbf064eb6cb252587a9b8319f7dff363859ae0cf (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
'use strict';
const path = require('path');
const gutil = require('gulp-util');
const through = require('through2');
const archiver = require('archiver');

module.exports = (filename, opts) => {
	if (!filename) {
		throw new gutil.PluginError('gulp-tar', '`filename` required');
	}

	let firstFile;
	const archive = archiver('tar', opts);

	return through.obj((file, enc, cb) => {
		if (file.relative === '') {
			cb();
			return;
		}

		if (firstFile === undefined) {
			firstFile = file;
		}

		archive.append(file.contents, Object.assign({
			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();
	});
};