This repository has been archived by the owner on May 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.ts
63 lines (54 loc) · 1.53 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
import { minify, Language } from "./mod.ts";
import {
green,
bold,
red,
blue,
gray,
} from "https://deno.land/[email protected]/fmt/colors.ts";
const { args } = Deno;
switch (args[0]) {
case "--help":
case "-H":
console.log(
` <file-location> minifies the given file from the file location
<file-location> <output-location> minifies the given file from the file location and saves it as the output file
-H, --help provides info on all of the commands available
-V, --version gives the version info for the CLI`
);
Deno.exit(1);
break;
case undefined:
case "--version":
case "-V":
console.log("minifier v1.1.0");
Deno.exit(1);
break;
}
const [fileName, ...rest] = args;
function msg(emoji: string, message: string) {
return `${emoji} ${message}`;
}
if (rest.length > 1) {
console.error(msg("❌", "Too many arguments passed in."));
Deno.exit();
}
const fileContents = await Deno.readTextFile(fileName).catch((err) => {
console.error(msg("❌", `Cannot find ${bold(red(fileName))}.`));
Deno.exit();
});
const fileNameParts = fileName.split(".");
const fileNameEnding = fileNameParts[fileNameParts.length - 1].toLowerCase();
const targetFile = rest[0] ?? fileName;
await Deno.writeTextFile(
targetFile,
minify(fileNameEnding.toLowerCase() as Language, fileContents)
);
console.log(
msg(
"✅",
`Successfully minified ${bold(green(fileName))}${
targetFile === fileName ? "" : ` as ${bold(blue(targetFile))}`
}! ${gray(`(${performance.now().toFixed(0)}ms)`)}`
)
);