-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
executable file
·68 lines (54 loc) · 2.21 KB
/
cli.ts
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
#! /usr/bin/env -S deno run --allow-read --allow-write
import { parseArgs } from "jsr:@std/[email protected]/parse-args";
import { Spinner } from "jsr:@std/[email protected]/spinner";
import { svgc } from "./index.ts";
import { basename } from "node:path";
const args = parseArgs(Deno.args, {
boolean: ["help", "optimise-pngs"],
string: ["output", "input", "quality"],
alias: { "help": "h", "input": "i", "output": "o", "quality": "q", "optimise-pngs": "a" },
});
if (args.help) {
console.log("Usage: svgc [options] -i <input> [-o <output>]");
console.log("");
console.log("Options:");
console.log(" -i, --input <input> Input file");
console.log(" -o, --output <output> Output file");
console.log(" -q, --quality <quality> Quality for JPEG images (default: 80)");
console.log(" -a, --optimise-pngs Optimise PNG images");
console.log(" -h, --help Show this help message");
Deno.exit(0);
}
const input = args.input;
if (!input) {
console.error("No input file specified");
console.error("");
console.error("Usage: svgc [options] -i <input> [-o <output>]");
console.error("svgc --help for more information");
Deno.exit(0);
}
const output = args.output ?? `${basename(input, ".svg")}-compressed.svg`;
const GREEN = "\u001b[32m";
const RESET = "\u001b[0m";
const green = (text: string) => `${GREEN}${text}${RESET}`;
const human = (bytes: number) => {
const units = ["B", "KiB", "MiB", "GiB", "TiB"];
let i = 0;
while (bytes >= 1024) (bytes /= 1024), i++;
return `${bytes.toFixed(2)} ${units[i]}`;
};
const spinner = new Spinner({ message: "Compressing..." });
spinner.start();
const data = await Deno.readFile(input);
const start = performance.now();
const result = await svgc(data, {
quality: args.quality ? parseInt(args.quality) : undefined,
optimisePngs: args["optimise-pngs"],
});
const diff = data.length - result.length;
const diffPercent = ((diff / data.length) * 100).toFixed(2);
const diffText = diff > 0 ? green(`Saved ${human(diff)}, ${diffPercent}%`) : "-";
spinner.stop();
console.log(`Done in in ${performance.now() - start}ms!`);
await Deno.writeFile(output, result);
console.log(`${basename(input)} (${human(data.length)}) -> ${basename(output)} (${human(result.length)}): ${diffText}`);