forked from fobos/gulp-archiver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
113 lines (87 loc) · 2.83 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"use strict";
let path = require("path");
let through = require("through2");
let concatStream = require("concat-stream");
let Archiver = require("archiver");
let PluginError = require("plugin-error");
let Vinyl = require("vinyl");
let GulpArchiver = function (format, opts) {
let supported = ["zip", "tar", "tar.gz", "tgz"];
let found = -1;
if (!format || (found = supported.indexOf(format)) === -1)
throw new PluginError("gulp-archiver2", "Unsupported archive format");
//If they want a compressed tarball, the archiver considers that a regular tarball.. with compression added
if(found > 1){
format = "tar";
opts.gzip = true;
}
opts = opts || {};
let archive = new Archiver(format, opts);
let firstFile;
let self = this;
let close = function (cb) {
if(!firstFile) return cb();
archive.finalize().then(() =>
archive.pipe(concatStream(data => {
//add our new archive to the stream
this.push(new Vinyl({
cwd: firstFile.cwd,
base: firstFile.base,
path: path.join(firstFile.base, self.fileOut),
contents: data
}))
cb();
}))
);
}
this.add = (dest, closeAfter) => {
//add file to pending, then if pending is 0, call triggerAdd()
if (!dest) dest = "";
dest = path.join(dest, "/")
return through.obj(function (file, encoding, cb) {
if (file.isStream()) {
this.emit("error", new PluginError("gulp-archiver2", "Streaming not supported"));
cb();
return;
}
if (!firstFile) firstFile = file;
if (file.isDirectory())
archive.append(null, { name: path.join(dest, file.relative), type: "directory" })
else
archive.append(file.contents, { name: path.join(dest, file.relative) });
if (closeAfter)
cb(null); //don't return file to stream, because we are going to replace it with the archive
else
cb(null, file);
}, function (cb) {
if (closeAfter)
close.call(this, cb);
else
cb();
});
}
this.close = function (fileOut) {
if (!fileOut)
throw new PluginError("gulp-archiver2", "Missing argument or invalid filename");
this.fileOut = fileOut;
let passthru = through.obj(); //simple pass-through stream
close.call(passthru, () => { //add archive to stream
passthru.push(null); //send EOF to signal stream finished
})
return passthru;
}
return this;
}
GulpArchiver.create = function (fileOut, opts) {
//this is a static method that acts as wrapper for the class above
//creates instance, exposes this.add, closes after task completion
let matches, format;
if (typeof fileOut === "string" && (matches = fileOut.match(/\.(zip|tar|tar\.gz|tgz)$/)))
format = matches[1] || matches[2];
else
throw new PluginError("gulp-archiver2", "Unsupported archive format for gulp-archiver");
let archInst = new GulpArchiver(format, opts);
archInst.fileOut = fileOut;
return archInst.add(null, true);
}
module.exports = GulpArchiver;