-
Notifications
You must be signed in to change notification settings - Fork 17
/
bestzip.js
173 lines (157 loc) · 4.75 KB
/
bestzip.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// creates a zip file using either the native `zip` command if available,
// or a node.js zip implementation otherwise.
"use strict";
const cp = require("child_process");
const fs = require("fs");
const path = require("path");
const archiver = require("archiver");
const async = require("async");
const glob = require("glob");
const which = require("which");
function hasNativeZip() {
return Boolean(which.sync("zip", { nothrow: true }));
}
function expandSources(cwd, source, done) {
// options to behave more like the native zip's glob support
const globOpts = {
cwd,
dot: false, // ignore .dotfiles
noglobstar: true, // treat ** as *
noext: true, // no (a|b)
nobrace: true, // no {a,b}
};
// first handle arrays
if (Array.isArray(source)) {
return async.concat(
source,
(_source, next) => expandSources(cwd, _source, next),
done
);
}
// then expand magic
if (typeof source !== "string") {
throw new Error(`source is (${typeof source}) `);
}
if (glob.hasMagic(source, globOpts)) {
// archiver uses this library but somehow ends up with different results on windows:
// archiver.glob('*') will include subdirectories, but omit their contents on windows
// so we'll use glob directly, and add all of the files it finds
glob(source, globOpts, done);
} else {
// or just trigger the callback with the source string if there is no magic
process.nextTick(() => {
// always return an array
done(null, [source]);
});
}
}
function walkDir(fullPath) {
const files = fs.readdirSync(fullPath).map((f) => {
const filePath = path.join(fullPath, f);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
return walkDir(filePath);
}
return filePath;
});
return files.reduce((acc, cur) => acc.concat(cur), []);
}
const nativeZip = (options) =>
new Promise((resolve, reject) => {
const cwd = options.cwd || process.cwd();
const command = "zip";
expandSources(cwd, options.source, (err, sources) => {
const args = ["--quiet", "--recurse-paths", options.destination].concat(
sources
);
const zipProcess = cp.spawn(command, args, {
stdio: "inherit",
cwd,
});
zipProcess.on("error", reject);
zipProcess.on("close", (exitCode) => {
if (exitCode === 0) {
resolve();
} else {
// exit code 12 means "nothing to do" right?
//console.log('rejecting', zipProcess)
reject(
new Error(
`Unexpected exit code from native zip: ${exitCode}\n executed command '${command} ${args.join(
" "
)}'\n executed in directory '${cwd}'`
)
);
}
});
});
});
// based on http://stackoverflow.com/questions/15641243/need-to-zip-an-entire-directory-using-node-js/18775083#18775083
const nodeZip = (options) =>
new Promise((resolve, reject) => {
const cwd = options.cwd || process.cwd();
const output = fs.createWriteStream(path.resolve(cwd, options.destination));
const archive = archiver("zip");
output.on("close", resolve);
archive.on("error", reject);
archive.pipe(output);
function addSource(source, next) {
const fullPath = path.resolve(cwd, source);
const destPath = source;
fs.stat(fullPath, function (err, stats) {
if (err) {
return next(err);
}
if (stats.isDirectory()) {
// Walk directory. Works on directories and directory symlinks.
const files = walkDir(fullPath);
files.forEach((f) => {
const subPath = f.substring(fullPath.length);
archive.file(f, {
name: destPath + subPath,
});
});
} else if (stats.isFile()) {
archive.file(fullPath, { stats: stats, name: destPath });
}
next();
});
}
expandSources(cwd, options.source, (err, expandedSources) => {
if (err) {
return reject(err);
}
async.forEach(expandedSources, addSource, (err) => {
if (err) {
return reject(err);
}
archive.finalize();
});
});
});
function zip(options) {
const compatMode = typeof options === "string";
if (compatMode) {
options = {
source: arguments[1],
destination: arguments[0],
};
}
let promise;
if (hasNativeZip()) {
promise = nativeZip(options);
} else {
promise = nodeZip(options);
}
if (compatMode) {
promise.then(arguments[2]).catch(arguments[2]);
} else {
return promise;
}
}
module.exports = zip;
module.exports.zip = zip;
module.exports.nodeZip = nodeZip;
module.exports.nativeZip = nativeZip;
module.exports.bestzip = zip;
module.exports.hasNativeZip = hasNativeZip;