-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·41 lines (34 loc) · 751 Bytes
/
cli.js
File metadata and controls
executable file
·41 lines (34 loc) · 751 Bytes
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
#!/usr/bin/env node
import process from 'node:process';
import meow from 'meow';
import {fileTypeFromFile, fileTypeFromStream} from 'file-type';
const cli = meow(`
Usage
$ file-type <filename>
$ file-type < <filename>
Example
$ file-type unicorn.png
png
image/png
`, {
importMeta: import.meta,
});
function printResult(type) {
if (!type) {
console.error('Unrecognized file type');
process.exit(65);
}
console.log(`${type.ext}\n${type.mime}`);
}
const input = cli.input[0];
if (!input && process.stdin.isTTY) {
console.error('Specify a file path');
process.exit(1);
}
(async () => {
if (input) {
printResult(await fileTypeFromFile(input));
} else {
printResult(await fileTypeFromStream(process.stdin));
}
})();