-
Notifications
You must be signed in to change notification settings - Fork 1
/
zip.js
42 lines (32 loc) · 1.21 KB
/
zip.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
const fs = require("fs");
const path = require("path");
const archiver = require("archiver");
const advzip = require("advzip-bin");
const util = require('util');
const execFile = util.promisify(require('child_process').exec);
const chalk = require("chalk");
const distDir = process.cwd() + "/dist";
const zipPath = distDir + "/game.zip"
const files = fs.readdirSync(distDir);
const output = fs.createWriteStream(zipPath);
const archive = archiver("zip", { zlib: { level: 9 } });
archive.pipe(output);
for (let file of files) {
const filePath = `${distDir}/${file}`;
console.log(filePath);
archive.file(filePath, { name: path.basename(filePath) });
}
archive
.finalize()
.then(() => execFile(advzip, ["--recompress", "--shrink-insane", "--iter 10", zipPath]))
.then(() => {
const MAX_BYTES = 13312;
const filename = "./dist/game.zip";
fileSize = fs.statSync(filename).size;
const chalkText = MAX_BYTES - fileSize >= 0 ? chalk.blue : chalk.red;
console.log();
console.log(chalkText(`Build size: ${fileSize} bytes`));
console.log(chalk.yellow(`Target size: ${MAX_BYTES} bytes`));
console.log();
})
.finally(() => process.exit(0));