1 | (function() {
|
2 | module.exports = function($) {
|
3 | var _, fs, kleur, normalizePath, normalizePathToArray, path, wrapList;
|
4 | ({_} = $);
|
5 | ({normalizePath, normalizePathToArray, wrapList} = $.fn);
|
6 | fs = require('fs');
|
7 | kleur = require('kleur');
|
8 | path = require('path');
|
9 |
|
10 |
|
11 | return $.zip_ = async function(...arg) {
|
12 | var _source, base, filename, isSilent, option, source, target;
|
13 | [source, target, option] = arg;
|
14 | _source = source;
|
15 | source = normalizePathToArray(source);
|
16 | target || (target = $.getDirname(source[0]).replace(/\*/g, ''));
|
17 | target = normalizePath(target);
|
18 | [base, filename, isSilent] = (function() {
|
19 | switch ($.type(option)) {
|
20 | case 'object':
|
21 | return [option.base, option.filename, option.silent || option.isSilent];
|
22 | case 'string':
|
23 | return [null, option, false];
|
24 | default:
|
25 | return [null, null, false];
|
26 | }
|
27 | })();
|
28 | base || (base = (function() {
|
29 | _source = (function() {
|
30 | switch ($.type(_source)) {
|
31 | case 'array':
|
32 | return _source[0];
|
33 | case 'string':
|
34 | return _source;
|
35 | default:
|
36 | throw new Error('invalid type');
|
37 | }
|
38 | })();
|
39 | if (~_source.search(/\*/)) {
|
40 | return _.trim(_source.replace(/\*.*/, ''), '/');
|
41 | }
|
42 | return path.dirname(_source);
|
43 | })());
|
44 | base = normalizePath(base);
|
45 | filename || (filename = `${$.getBasename(target)}.zip`);
|
46 | await new Promise(async function(resolve) {
|
47 | var ansi, archive, archiver, i, len, listSource, msg, name, output, src;
|
48 |
|
49 |
|
50 | ansi = require('sisteransi');
|
51 | archiver = require('archiver');
|
52 | output = fs.createWriteStream(`${target}/${filename}`);
|
53 | archive = archiver('zip', {
|
54 | zlib: {
|
55 | level: 9
|
56 | }
|
57 | });
|
58 | archive.on('warning', function(err) {
|
59 | throw err;
|
60 | });
|
61 | archive.on('error', function(err) {
|
62 | throw err;
|
63 | });
|
64 | msg = null;
|
65 | archive.on('entry', function(e) {
|
66 | if (isSilent) {
|
67 | return;
|
68 | }
|
69 | return msg = $.info.renderPath(e.sourcePath);
|
70 | });
|
71 | archive.on('progress', function(e) {
|
72 | var gray, magenta;
|
73 | if (isSilent) {
|
74 | return;
|
75 | }
|
76 | if (!msg) {
|
77 | return;
|
78 | }
|
79 | gray = kleur.gray(`${Math.floor(e.fs.processedBytes * 100 / e.fs.totalBytes)}%`);
|
80 | magenta = kleur.magenta(msg);
|
81 | msg = `${gray} ${magenta}`;
|
82 | $.i([ansi.erase.line, msg, ansi.cursor.up()].join(''));
|
83 | return msg = null;
|
84 | });
|
85 | archive.on('end', function() {
|
86 | return resolve();
|
87 | });
|
88 | archive.pipe(output);
|
89 | listSource = (await $.source_(source));
|
90 | for (i = 0, len = listSource.length; i < len; i++) {
|
91 | src = listSource[i];
|
92 | name = src.replace(base, '');
|
93 | archive.file(src, {name});
|
94 | }
|
95 | return archive.finalize();
|
96 | });
|
97 | $.info('zip', `zipped ${wrapList(source)} to '${target}', named as '${filename}'`);
|
98 | return $;
|
99 | };
|
100 | };
|
101 |
|
102 | }).call(this);
|