-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·101 lines (82 loc) · 2.31 KB
/
cli.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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
import { blurhashFromURL } from "blurhash-from-url";
import updateNotifier from "update-notifier";
import packageJson from "./package.json" assert { type: "json" };
import chalk from "chalk";
import ora from "ora";
import meow from "meow";
const spinner = ora();
const cyan = chalk.cyan.bold("›");
const red = chalk.red.bold("›");
const green = chalk.green.bold("›");
const yellow = chalk.yellow.bold("›");
const notifier = updateNotifier({ pkg: packageJson });
notifier.notify();
const cli = meow(
`
${cyan} Pass an image URL or local path to generate a blurhash, width, and height
${red} Usage
$ npx blurhash-cli <url>
${red} Example
$ npx blurhash-cli https://i.imgur.com/NhfEdg2.png
${red} Example with size
$ npx blurhash-cli https://i.imgur.com/NhfEdg2.png --size=64
${red} Example with local image
$ npx blurhash-cli ./my-image.png --local
${red} Options
--size=<size> Set the size of the blurhash (default: 32)
--local, -l Use a local image instead of URL
--help, -h Show the help menu for the command
--version, -v Show the current version of the package
`,
{
importMeta: import.meta,
flags: {
help: {
type: "boolean",
alias: "h",
},
version: {
type: "boolean",
alias: "v",
},
size: {
type: "string",
alias: "s",
default: "32",
},
local: {
type: "boolean",
alias: "l",
default: false,
},
},
}
);
const getCLIUrl = cli.input[0];
let getSize = parseInt(cli.flags.size);
if (getSize <= 0 || isNaN(getSize)) {
console.log(red, "Invalid size specified. Using the default size of 32.");
getSize = 32;
}
if (!getCLIUrl) {
cli.showHelp();
}
async function blurhashCLI() {
spinner.start();
try {
spinner.text = "Generating Blurhash...";
const blurhash = await blurhashFromURL(getCLIUrl, {
size: getSize || 32,
offline: cli.flags.local,
});
spinner.stop();
spinner.succeed("🥳 Blurhash Generated");
console.log(cyan, "Hash:", blurhash.encoded);
console.log(red, "Width:", blurhash.width);
console.log(green, "Height:", blurhash.height);
} catch (error) {
spinner.fail(`${red} ${error}`);
}
}
blurhashCLI();