-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
cli.js
executable file
·44 lines (37 loc) · 921 Bytes
/
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
#!/usr/bin/env node
import process from 'node:process';
import meow from 'meow';
import getStdin from 'get-stdin';
import terminalImage from 'terminal-image';
import {fileTypeFromFile, fileTypeFromBuffer} from 'file-type';
const cli = meow(`
Usage
$ image <image>
$ cat <image> | image
Examples
$ image unicorn.jpg
$ cat unicorn.jpg | image
`, {
importMeta: import.meta,
});
const [input] = cli.input;
if (!input && process.stdin.isTTY) {
console.error('Specify the path to an image');
process.exit(1);
}
if (input) {
const {ext} = await fileTypeFromFile(input);
if (ext === 'gif') {
terminalImage.gifFile(input);
} else {
console.log(await terminalImage.file(input));
}
} else {
const stdin = await getStdin.buffer();
const {ext} = await fileTypeFromBuffer(stdin);
if (ext === 'gif') {
terminalImage.gifBuffer(stdin);
} else {
console.log(await terminalImage.buffer(stdin));
}
}