|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -var path = require('path'), |
4 |
| - fs = require('fs'), |
5 |
| - zlib = require('zlib'), |
6 |
| - |
7 |
| - tar = require('tar'), |
8 |
| - fstream = require('fstream'), |
9 |
| - |
10 |
| - check = require('checkup'), |
11 |
| - pipe = require('pipe-io/legacy'), |
12 |
| - files = require('files-io'); |
| 3 | +const path = require('path'), |
| 4 | +const fs = require('fs'), |
| 5 | +const zlib = require('zlib'), |
| 6 | + |
| 7 | +const tar = require('tar'), |
| 8 | +const fstream = require('fstream'), |
13 | 9 |
|
14 |
| -exports.pack = function(from, to, callback) { |
| 10 | +const check = require('checkup'), |
| 11 | +const pipe = require('pipe-io/legacy'), |
| 12 | +const files = require('files-io'); |
| 13 | + |
| 14 | +exports.pack = (from, to, callback) => { |
15 | 15 | check
|
16 | 16 | .type('callback', callback, 'function')
|
17 | 17 | .check({
|
18 |
| - to: to, |
19 |
| - from: from |
| 18 | + to, |
| 19 | + from, |
20 | 20 | });
|
21 | 21 |
|
22 |
| - isDir(from, function(is) { |
23 |
| - var dir, name, |
24 |
| - |
25 |
| - optionsDir = { path: from, type: 'Directory' }, |
26 |
| - optionsTar = { noProprietary: true }, |
27 |
| - |
28 |
| - streamDir, |
29 |
| - streamTar, |
30 |
| - streamZip = zlib.createGzip(), |
31 |
| - streamFile, |
32 |
| - |
33 |
| - isStr = typeof to === 'string', |
34 |
| - options = { |
35 |
| - gzip: true |
36 |
| - }; |
| 22 | + isDir(from, (is) => { |
| 23 | + const optionsDir = { path: from, type: 'Directory' }; |
| 24 | + const optionsTar = { noProprietary: true }; |
| 25 | + const streamZip = zlib.createGzip(); |
| 26 | + const isStr = typeof to === 'string'; |
| 27 | + const options = { |
| 28 | + gzip: true |
| 29 | + }; |
37 | 30 |
|
38 | 31 | if (!is) {
|
39 |
| - files.pipe(from, to, options, callback); |
| 32 | + return files.pipe(from, to, options, callback); |
| 33 | + |
| 34 | + const streamDir = fstream.Reader(optionsDir); |
| 35 | + const streamTar = tar.Pack(optionsTar); |
| 36 | + |
| 37 | + let streamFile; |
| 38 | + if (!isStr) { |
| 39 | + streamFile = to; |
40 | 40 | } else {
|
41 |
| - streamDir = fstream.Reader(optionsDir); |
42 |
| - streamTar = tar.Pack(optionsTar); |
43 |
| - |
44 |
| - if (!isStr) { |
45 |
| - streamFile = to; |
46 |
| - } else { |
47 |
| - dir = path.dirname(to); |
48 |
| - name = path.basename(to, '.gz'); |
49 |
| - |
50 |
| - if (dir !== '/') |
51 |
| - dir += path.sep; |
52 |
| - |
53 |
| - to = dir + name + '.tar.gz'; |
54 |
| - |
55 |
| - streamFile = fs.createWriteStream(to); |
56 |
| - } |
| 41 | + const dir = path.dirname(to); |
| 42 | + const name = path.basename(to, '.gz'); |
57 | 43 |
|
58 |
| - pipe([ |
59 |
| - streamDir, |
60 |
| - streamTar, |
61 |
| - streamZip, |
62 |
| - streamFile |
63 |
| - ], callback); |
| 44 | + if (dir !== '/') |
| 45 | + dir += path.sep; |
| 46 | + |
| 47 | + const to = dir + name + '.tar.gz'; |
| 48 | + |
| 49 | + streamFile = fs.createWriteStream(to); |
64 | 50 | }
|
| 51 | + |
| 52 | + pipe([ |
| 53 | + streamDir, |
| 54 | + streamTar, |
| 55 | + streamZip, |
| 56 | + streamFile |
| 57 | + ], callback); |
65 | 58 | });
|
66 | 59 | };
|
67 | 60 |
|
68 |
| -exports.unpack = function(from, to, callback) { |
69 |
| - var write, error, |
70 |
| - isStr = typeof from === 'string', |
71 |
| - isGz = /\.gz$/.test(from), |
72 |
| - isTarGz = /\.tar\.gz$/.test(from), |
73 |
| - |
74 |
| - options = { |
75 |
| - gunzip : true |
76 |
| - }; |
| 61 | +exports.unpack = (from, to, callback) => { |
| 62 | + const isStr = typeof from === 'string'; |
| 63 | + const isGz = /\.gz$/.test(from); |
| 64 | + const isTarGz = /\.tar\.gz$/.test(from); |
| 65 | + |
| 66 | + const options = { |
| 67 | + gunzip: true |
| 68 | + }; |
77 | 69 |
|
78 | 70 | check
|
79 | 71 | .type('callback', callback, 'function')
|
80 | 72 | .check({
|
81 |
| - to: to, |
82 |
| - from: from |
| 73 | + to, |
| 74 | + from, |
83 | 75 | });
|
84 | 76 |
|
| 77 | + let write; |
85 | 78 | if (isTarGz)
|
86 | 79 | write = tar.Extract({ path: path.dirname(to) });
|
87 | 80 | else if (!isStr || isGz)
|
88 | 81 | write = to;
|
89 |
| - else |
90 |
| - error = Error('wrong file type: can be ".gz" or ".tar.gz"!'); |
91 |
| - |
92 |
| - if (error) |
93 |
| - callback(error); |
94 | 82 | else
|
95 |
| - files.pipe(from, write, options, callback); |
| 83 | + return callback(Error('wrong file type: can be ".gz" or ".tar.gz"!')); |
| 84 | + |
| 85 | + files.pipe(from, write, options, callback); |
96 | 86 | };
|
97 | 87 |
|
98 | 88 | function isDir(name, callback) {
|
99 |
| - fs.stat(name, function(error, stat) { |
100 |
| - var isDir; |
101 |
| - |
| 89 | + fs.stat(name, (error, stat) => { |
102 | 90 | if (!error)
|
103 | 91 | isDir = stat.isDirectory();
|
104 | 92 |
|
105 | 93 | callback(isDir);
|
106 | 94 | });
|
107 | 95 | }
|
| 96 | + |
0 commit comments