UNPKG

5.97 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.outputFileSync = undefined;
7
8var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); // TODO Replace `sync` functions with async versions
9
10exports.zip = zip;
11exports.zipSources = zipSources;
12exports.unzip = unzip;
13
14var _lodash = require('lodash.size');
15
16var _lodash2 = _interopRequireDefault(_lodash);
17
18var _temp = require('temp');
19
20var _temp2 = _interopRequireDefault(_temp);
21
22var _jszip = require('jszip');
23
24var _jszip2 = _interopRequireDefault(_jszip);
25
26var _fsExtra = require('fs-extra');
27
28var _path2 = require('path');
29
30var _q = require('q');
31
32var _util = require('util');
33
34function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35
36var debug = !!process.env.DEBUG;
37
38// ./zip.js module is excluded from browser-like environments. We take advantage of that here.
39exports.outputFileSync = _fsExtra.outputFileSync;
40function zip(files, cwd) {
41 debug && console.log('Zipping files', (0, _util.inspect)(files));
42 var deferred = (0, _q.defer)();
43 // Flag to detect if any file was added to the zip archive
44 var hasFiles = false;
45 // Sanitize `cwd`
46 if (cwd) {
47 cwd = (0, _path2.normalize)(cwd);
48 }
49 // If it's already a zip file
50 if (files.length === 1 && /^.*\.zip$/.test(files[0])) {
51 hasFiles = true;
52 var _zip = new _jszip2.default();
53 var zipFile = (0, _fsExtra.readFileSync)(files[0]);
54
55 (0, _fsExtra.outputFileSync)(_temp2.default.openSync({ suffix: '.zip' }).path, zipFile);
56 zipFile = _zip.load(zipFile);
57 deferred.resolve(zipFile);
58 } else {
59 var _zip2 = new _jszip2.default();
60 for (var i = 0, l = files.length; i < l; ++i) {
61 // Sanitise path
62 if (typeof files[i] === 'string') {
63 files[i] = (0, _path2.normalize)(files[i]);
64 if (files[i].indexOf('../') === 0) {
65 files[i] = (0, _path2.resolve)(files[i]);
66 }
67 }
68 // Bypass unwanted patterns from `files`
69 if (/.*\.(git|hg)(\/.*|$)/.test(files[i].path || files[i])) {
70 continue;
71 }
72 var buffer = void 0,
73 name = void 0;
74 var sPath = void 0;
75 if (cwd && files[i].indexOf && files[i].indexOf(cwd) !== 0) {
76 sPath = (0, _path2.join)(cwd, files[i]);
77 } else {
78 sPath = files[i];
79 }
80 // If buffer
81 if (files[i].contents) {
82 name = (0, _path2.relative)(files[i].cwd, files[i].path);
83 buffer = files[i].contents;
84 } else if (!(0, _fsExtra.statSync)(sPath).isDirectory()) {
85 // Else if it's a path and not a directory
86 if (cwd && files[i].indexOf && files[i].indexOf(cwd) === 0) {
87 name = files[i].substring(cwd.length);
88 } else {
89 name = files[i];
90 }
91 buffer = (0, _fsExtra.readFileSync)(sPath);
92 } else {
93 // Else if it's a directory path
94 _zip2.folder(sPath);
95 }
96 if (name) {
97 hasFiles = true;
98 _zip2.file(name, buffer);
99 }
100 }
101 if (hasFiles) {
102 var tempFile = _temp2.default.openSync({ suffix: '.zip' });
103 (0, _fsExtra.outputFileSync)(tempFile.path, _zip2.generate({ type: 'nodebuffer' }), {
104 encoding: 'base64'
105 });
106 files[0] = tempFile.path;
107 files.length = 1;
108 deferred.resolve(_zip2);
109 } else {
110 throw new Error('No source files found. If you intend to send a whole directory sufix your path with "**" (e.g. ./my-directory/**)');
111 }
112 }
113
114 return deferred.promise;
115}
116
117function zipSources(sources) {
118 var zipFile = new _jszip2.default();
119 var fileNames = sources.map(function (source) {
120 zipFile.file(source.filename, source.content);
121 return source.filename;
122 });
123
124 if (debug) {
125 console.log('Zipping files', (0, _util.inspect)(fileNames));
126 }
127 return Promise.resolve(zipFile);
128}
129
130function isWinAbsolutePath(path) {
131 return (0, _path2.isAbsolute)(path) && /^([a-z]:)(.*)/i.test(path);
132}
133
134function parseWinAbsolutePath(_path) {
135 var _path$match = _path.match(/^([a-z]:)(.*)/i),
136 _path$match2 = _slicedToArray(_path$match, 3),
137 full = _path$match2[0],
138 drv = _path$match2[1],
139 path = _path$match2[2];
140
141 return {
142 drv: drv,
143 path: path
144 };
145}
146
147function unzip(zipFile, dest) {
148 var stream = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
149
150 var zip = new _jszip2.default(zipFile);
151 var _size = (0, _lodash2.default)(zip.files);
152
153 var results = [];
154
155 for (var file in zip.files) {
156 if (!zip.files[file].options.dir) {
157 var buffer = zip.file(file).asNodeBuffer();
158
159 if (typeof dest === 'function') {
160 if (stream) {
161 dest(buffer, file);
162 } else {
163 results.push({ filename: file, content: buffer.toString() });
164 }
165 } else if (dest && typeof dest === 'string') {
166 var destPath;
167
168 var lastDestChar = dest[dest.length - 1];
169 if (_size === 1 && lastDestChar !== '/' && lastDestChar !== '\\') {
170 destPath = dest;
171 } else {
172 var _file = file;
173 // Deal with win path join c:\dest\:c\src
174 if (isWinAbsolutePath(_file)) {
175 _file = parseWinAbsolutePath(_file).path;
176 }
177 destPath = (0, _path2.join)(dest, _file);
178 }
179 (0, _fsExtra.outputFileSync)(destPath, buffer);
180 }
181 }
182 }
183
184 if (!stream) {
185 dest(results);
186 }
187}
\No newline at end of file