Skip to content

Commit 7a115e8

Browse files
committedJul 14, 2017
feature(jag) drop support of node version < 4
1 parent 3ae9be3 commit 7a115e8

File tree

3 files changed

+69
-77
lines changed

3 files changed

+69
-77
lines changed
 

‎README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Pack files and folders with tar and gzip. Use [jaguar](https://github.com/codera
1010
## Hot to use?
1111

1212
```js
13-
var jag = require('jag'),
14-
fn = function(error) {
15-
if (error)
16-
console.error(error.message);
17-
};
13+
const jag = require('jag');
14+
const fn = function(error) {
15+
if (error)
16+
console.error(error.message);
17+
};
1818

1919
jag.pack('/tmp/lib', '/tmp/1/lib', fn); /* extenstion would be added */
2020
jag.unpack('/tmp/lib.tar.gz', '/tmp/lib', fn);

‎lib/jag.js

+60-71
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,96 @@
11
'use strict';
22

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'),
139

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) => {
1515
check
1616
.type('callback', callback, 'function')
1717
.check({
18-
to: to,
19-
from: from
18+
to,
19+
from,
2020
});
2121

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+
};
3730

3831
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;
4040
} 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');
5743

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);
6450
}
51+
52+
pipe([
53+
streamDir,
54+
streamTar,
55+
streamZip,
56+
streamFile
57+
], callback);
6558
});
6659
};
6760

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+
};
7769

7870
check
7971
.type('callback', callback, 'function')
8072
.check({
81-
to: to,
82-
from: from
73+
to,
74+
from,
8375
});
8476

77+
let write;
8578
if (isTarGz)
8679
write = tar.Extract({ path: path.dirname(to) });
8780
else if (!isStr || isGz)
8881
write = to;
89-
else
90-
error = Error('wrong file type: can be ".gz" or ".tar.gz"!');
91-
92-
if (error)
93-
callback(error);
9482
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);
9686
};
9787

9888
function isDir(name, callback) {
99-
fs.stat(name, function(error, stat) {
100-
var isDir;
101-
89+
fs.stat(name, (error, stat) => {
10290
if (!error)
10391
isDir = stat.isDirectory();
10492

10593
callback(isDir);
10694
});
10795
}
96+

‎package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"nyc": "^11.0.2"
1414
},
1515
"devDependencies": {},
16+
"engines": {
17+
"node": ">=4"
18+
},
1619
"scripts": {
1720
"test": "echo \"Error: no test specified\" && exit 1"
1821
},
@@ -31,4 +34,4 @@
3134
"url": "https://github.com/coderaiser/node-jag/issues"
3235
},
3336
"homepage": "https://github.com/coderaiser/node-jag"
34-
}
37+
}

0 commit comments

Comments
 (0)
Please sign in to comment.