-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (75 loc) · 2.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// simple wrapper around decompress-zip, that preserves the executable bits
// on extracted files. it also filters all the rubbish that OSX puts into zip archives
// by Tomas Pollak. The octal function is also taken from decompress-zip, by the way.
'use strict';
var path = require('path'),
chmod = require('fs').chmod,
queue = require('async').queue,
DecompressZip = require('decompress-zip'),
is_windows = process.env == 'win32';
var skip_files = ['__MACOSX', '.DS_Store', '._.DS_Store'];
var debugging = process.env.DEBUG;
function debug(str) {
if (debugging) console.log(str)
}
function octal(number, digits) {
var result = '';
for (var i = 0; i < digits; i++) {
result = (number & 0x07) + result;
number >>= 3;
}
return result;
};
exports.open = function(file, new_path, cb) {
var error,
result,
zip = new DecompressZip(file);
if (!new_path || typeof new_path == 'function') {
var cb = cb || new_path;
var file_path = path.dirname(path.resolve(file));
var new_path = path.join(file_path, path.basename(file).split('.')[0]);
}
function done(err, res) {
if (err || q.length() == 0)
return cb && cb(err, res);
debug('Done. Found ' + q.length() + ' elements in queue!');
result = res; // store result so we can return it when queue finishes
q.resume();
}
var q = queue(function(task, callback) {
var file = path.join(new_path, task.file);
debug('Chmodding ' + file + ' to ' + task.mode);
chmod(file, task.mode, function(err) {
if (err) debug(err);
callback();
});
}, 2);
// called when the queue reaches zero, meaning all the work is done
q.drain = function() {
return cb && cb(error, result);
}
q.pause(); // the queue will begin when the files are extracted, not before
zip.on('file', function(file) {
// console.log(file.path);
if (!is_windows && file.type == 'File' && file.path.indexOf('node_modules') == -1) {
var mode = octal(file.mode, 4);
if (mode != '0644') {
debug('Queueing mode set for ' + file.path)
q.push({ file: file.path, mode: mode });
}
}
});
zip.on('extract', function(result) {
done(null, result);
});
zip.on('error', function(err) {
done(err);
});
zip.extract({
path : path.resolve(new_path),
filter : function (file) {
return skip_files.indexOf(file.filename) == -1;
// return file.type !== "SymbolicLink";
}
});
}