-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
109 lines (91 loc) · 2.9 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
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
102
103
104
105
106
107
108
109
import { parseArgs } from "jsr:@std/cli@1/parse-args";
import { fetchEmoji } from "./mod.ts";
import {
type FormatOptions,
makeJSONFileContent,
makeTypeScriptFileContent,
} from "./utilities.ts";
const CLI_VERSION = "v0.2.0";
const args = parseArgs(Deno.args, {
boolean: ["version", "help", "ts", "json", "dedupe", "map", "minimal"],
string: ["output", "unicode-version"],
alias: {
"output": "o",
"unicode-version": "v",
},
default: {
json: true,
},
unknown: (arg) => {
console.log(`Unknown argument: ${arg}. See --help`);
Deno.exit(1);
},
});
function leave(...message: string[]) {
if (message.length) console.log(...message);
Deno.exit(0);
}
function error(message: string) {
console.log(`%cerror%c: ${message}`, "color: red", "color: none");
Deno.exit(1);
}
function warn(message: string) {
console.log(`%cwarn%c: ${message}`, "color: orange", "color: none");
}
if (args.help) {
leave(`Emoji Scraper: A CLI tool for scraping emoji list from
https://unicode.org by a specified version.
FETCH OPTIONS
--unicode-version -v
Specify any valid Unicode version. Omitting this operator
will make the CLI fetch the latest version. Find all the
valid Unicode versions here: https://unicode.org/Public/emoji/.
--dedupe
Dedupe the emojis based on their qualifications.
OUTPUT OPTIONS
--output -o
Where to save the TypeScript/JSON output of the emoji list.
By default the output is streamed to STDOUT.
--ts
Output as TypeScript. Cannot be used with --json.
--json
Output as JSON. Cannot be used with --ts.
--map
Output an identifier to value object instead of the default array.
--minimal
Only include the identifier and the emoji itself (drops the other info)
--version
Print the scraper version.
--help
Print this help message.
For more information and for reporting issues, see
https://github.com/dcdunkan/emoji-scraper.`);
}
if (args.version) {
leave(`emoji-scraper ${CLI_VERSION}`);
}
if (args.ts && args.json) {
warn("either specify --ts or --json for output format");
}
if (!args["unicode-version"]?.trim()) {
warn("Unicode version is not specified, fetching the latest");
}
const version = args["unicode-version"]?.trim() || "latest";
console.log(`fetching ${version} emoji list`);
const emoji = await fetchEmoji(version, { dedupeQualifications: args.dedupe });
console.log(`fetched ${emoji.length} of unicode v${version} emoji`);
const formatOptions: FormatOptions = {
map: args.map,
minimal: args.minimal,
useTabs: false,
indentSize: 2,
};
const fileContent = args.ts
? makeTypeScriptFileContent(emoji, { ...formatOptions })
: makeJSONFileContent(emoji, { ...formatOptions });
if (args.output) {
await Deno.writeTextFile(args.output, fileContent);
console.log(`written emoji list to ${args.output}`);
} else {
console.log(fileContent);
}