UNPKG

3.69 kBJavaScriptView Raw
1var $, M, _, archiver, fs, kleur, ora;
2
3$ = {};
4
5$.info = require('../dist/info');
6
7$.source_ = require('../dist/source_');
8
9$.normalizePathToArray = require('../dist/normalizePathToArray');
10
11$.getDirname = require('../dist/getDirname');
12
13$.normalizePath = require('../dist/normalizePath');
14
15$.getBasename = require('../dist/getBasename');
16
17$.wrapList = require('../dist/wrapList');
18
19$.type = require('../dist/type');
20
21_ = {};
22
23_.trim = require('lodash/trim');
24
25archiver = require('archiver');
26
27fs = require('fs');
28
29kleur = require('kleur');
30
31ora = require('ora');
32
33M = class M {
34 /*
35 ---
36 archive_(option)
37 execute_(arg...)
38 getBase(source)
39 getOption(option)
40 */
41 async archive_(option) {
42 var base, filename, source, spinner, target;
43 ({base, filename, source, target} = option);
44 spinner = ora().start();
45 await new Promise(async function(resolve) {
46 var archive, i, len, msg, name, output, ref;
47 output = fs.createWriteStream(`${target}/${filename}`);
48 archive = archiver('zip', {
49 zlib: {
50 level: 9
51 }
52 });
53 msg = null;
54 /*
55 end
56 entry
57 error
58 progress
59 warning
60 */
61 archive.on('end', function() {
62 spinner.succeed();
63 return resolve();
64 });
65 archive.on('entry', function(e) {
66 return msg = $.info().renderPath(e.sourcePath);
67 });
68 archive.on('error', function(e) {
69 spinner.fail(e.message);
70 throw new Error(e.message);
71 });
72 archive.on('progress', function(e) {
73 var gray, magenta;
74 if (!msg) {
75 return;
76 }
77 gray = kleur.gray(`${Math.floor(e.fs.processedBytes * 100 / e.fs.totalBytes)}%`);
78 magenta = kleur.magenta(msg);
79 msg = `${gray} ${magenta}`;
80 spinner.text = msg;
81 return msg = null;
82 });
83 archive.on('warning', function(e) {
84 spinner.warn(e.message);
85 throw new Error(e.message);
86 });
87 // execute
88 archive.pipe(output);
89 ref = (await $.source_(source));
90 for (i = 0, len = ref.length; i < len; i++) {
91 source = ref[i];
92 name = source.replace(base, '');
93 archive.file(source, {name});
94 }
95 return archive.finalize();
96 });
97 return this;
98 }
99
100 async execute_(source, target, option) {
101 var _source, base, filename;
102 _source = source;
103 source = $.normalizePathToArray(source);
104 target || (target = $.getDirname(source[0]).replace(/\*/g, ''));
105 target = $.normalizePath(target);
106 [base, filename] = this.getOption(option);
107 base = $.normalizePath(base || this.getBase(_source));
108 filename || (filename = `${$.getBasename(target)}.zip`);
109 await this.archive_({base, filename, source, target});
110 $.info('zip', `zipped ${$.wrapList(source)} to '${target}', named as '${filename}'`);
111 return this;
112 }
113
114 getBase(source) {
115 var type;
116 type = $.type(source);
117 source = (function() {
118 switch (type) {
119 case 'array':
120 return source[0];
121 case 'string':
122 return source;
123 default:
124 throw new Error(`zip_/error: invalid type '${type}'`);
125 }
126 })();
127 if (~source.search(/\*/)) {
128 return _.trim(source.replace(/\*.*/, ''), '/');
129 }
130 return $.getDirname(source); // return
131 }
132
133 getOption(option) {
134 var type;
135 type = $.type(option);
136 switch (type) {
137 case 'object':
138 return [option.base, option.filename];
139 case 'string':
140 return [null, option];
141 default:
142 return [null, null];
143 }
144 }
145
146};
147
148module.exports = async function(...arg) {
149 var m;
150 m = new M();
151 await m.execute_(...arg);
152 return this;
153};