Spaces:
Running
Running
File size: 1,915 Bytes
a07d36d |
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 |
import { resolve } from "node:path";
import { createWriteStream, statSync } from "node:fs";
import { EOL } from "node:os";
import archiver from "archiver";
import tasuku from "tasuku";
import { greenBright, redBright } from "colorette";
// @ts-ignore
import crossExecFile from "cross-exec-file";
// @ts-ignore
import efficientCompressionTool from "ect-bin";
// @ts-ignore
import zipstats from "zipstats";
const publicFolderPath = resolve(__dirname, "..", "js13kserver", "public");
const zipFilePath = resolve(__dirname, "..", "game.zip");
const archive = archiver("zip", { zlib: { level: 9 } });
tasuku.group((task) => [
task("Creating zip file", async () => {
return new Promise((resolve, reject) => {
const output = createWriteStream(zipFilePath);
output.on("close", resolve);
output.on("error", reject);
archive.pipe(output);
archive.directory(publicFolderPath, "");
archive.finalize();
});
}),
task("Optimizing zip file", async ({ setOutput, setError }) => {
const result: { stdout: string; stderr: string } = await crossExecFile(efficientCompressionTool, [
"-9",
"-zip",
zipFilePath,
]);
if (result.stderr.length) {
setError(result.stderr);
} else {
setOutput(result.stdout);
}
}),
task("Checking zip file", async ({ setOutput }) => {
setOutput(zipstats(zipFilePath));
}),
task("Checking size limit", async ({ setOutput, setError }) => {
const maxSizeAllowed = 13 * 1024;
const fileSize = statSync(zipFilePath).size;
const fileSizeDifference = Math.abs(maxSizeAllowed - fileSize);
const isUnderSizeLimit = fileSize <= maxSizeAllowed;
const message = `${fileSizeDifference} bytes ${isUnderSizeLimit ? "under" : "over"} the 13KB limit!${EOL}`;
if (isUnderSizeLimit) {
setOutput(greenBright(message));
} else {
setError(redBright(message));
}
}),
]);
|