-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compress.js
40 lines (29 loc) · 995 Bytes
/
compress.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
// require modules
const fs = require('fs');
const archiver = require('archiver');
const output = fs.createWriteStream(__dirname + '/archive.zip');
const archive = archiver('zip', {
zlib: { level: 9 }
});
const rootPath = __dirname + '/src-minified';
archive.on('error', function(err) {
throw err;
});
archive.pipe(output);
function createArchive() {
const appendRecursive = (dirs, path = rootPath, prefix = '') => {
for(let dir of dirs) {
const actualDir = `${path}/${dir}`;
const isDir = fs.existsSync(actualDir) && fs.lstatSync(actualDir).isDirectory();
if(isDir) {
archive.file(actualDir, { name: `${prefix}${dir}` });
appendRecursive(fs.readdirSync(actualDir), actualDir, `${dir}/`);
} else {
archive.file(actualDir, { name: `${prefix}${dir}` });
}
}
}
appendRecursive(fs.readdirSync(rootPath));
archive.finalize();
}
createArchive();