@involvex/emoji-cli 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,421 @@
1
+ #!/usr/bin/env node
2
+
3
+ import * as cli from '../src/index.js'
4
+
5
+ // Type definitions for better type safety
6
+ interface CLIConfig {
7
+ name: string
8
+ description: string
9
+ version: string
10
+ }
11
+
12
+ interface Command {
13
+ name: string
14
+ aliases: string[]
15
+ description: string
16
+ handler: (args?: string) => void | Promise<void>
17
+ requiresArgs: boolean
18
+ }
19
+
20
+ interface ParsedArgs {
21
+ command: string
22
+ args: string[]
23
+ help: boolean
24
+ }
25
+
26
+ // Configuration
27
+ const CONFIG: CLIConfig = {
28
+ name: 'emoji-cli',
29
+ description: 'Friendly emoji lookups and parsing utilities for Node.js',
30
+ version: await import('../package.json').then(pkg => pkg.default.version),
31
+ }
32
+
33
+ // Available commands
34
+ const COMMANDS: Command[] = [
35
+ {
36
+ name: 'search',
37
+ aliases: ['--search'],
38
+ description: 'Search for emojis by name or pattern',
39
+ handler: runSearch,
40
+ requiresArgs: true,
41
+ },
42
+ {
43
+ name: 'emojify',
44
+ aliases: ['--emojify'],
45
+ description: 'Convert text to emojis',
46
+ handler: runEmojify,
47
+ requiresArgs: true,
48
+ },
49
+ {
50
+ name: 'unemojify',
51
+ aliases: ['--unemojify'],
52
+ description: 'Convert emojis back to text',
53
+ handler: runUnemojify,
54
+ requiresArgs: true,
55
+ },
56
+ {
57
+ name: 'get',
58
+ aliases: ['--get'],
59
+ description: 'Get a specific emoji by name',
60
+ handler: runGet,
61
+ requiresArgs: true,
62
+ },
63
+ {
64
+ name: 'has',
65
+ aliases: ['--has'],
66
+ description: 'Check if an emoji exists',
67
+ handler: runHas,
68
+ requiresArgs: true,
69
+ },
70
+ {
71
+ name: 'find',
72
+ aliases: ['--find', '-f'],
73
+ description: 'Find emojis by name',
74
+ handler: runFind,
75
+ requiresArgs: true,
76
+ },
77
+ {
78
+ name: 'random',
79
+ aliases: ['--random', '--rnd', '-r'],
80
+ description: 'Get a random emoji',
81
+ handler: runRandom,
82
+ requiresArgs: false,
83
+ },
84
+ ]
85
+
86
+ // Error handling
87
+ class CLIError extends Error {
88
+ constructor(
89
+ message: string,
90
+ public code: number = 1,
91
+ ) {
92
+ super(message)
93
+ this.name = 'CLIError'
94
+ }
95
+ }
96
+
97
+ class ValidationError extends CLIError {
98
+ constructor(message: string) {
99
+ super(message, 2)
100
+ this.name = 'ValidationError'
101
+ }
102
+ }
103
+
104
+ // Utility functions
105
+ function createOutputFormatter() {
106
+ return {
107
+ success: (
108
+ data:
109
+ | never
110
+ | string
111
+ | {
112
+ emoji: string
113
+ name?: string
114
+ key?: string
115
+ }
116
+ | (
117
+ | string
118
+ | {
119
+ emoji: string
120
+ name?: string
121
+ key?: string
122
+ }
123
+ )[],
124
+ ) => {
125
+ if (Array.isArray(data)) {
126
+ if (data.length === 0) {
127
+ console.log('No results found')
128
+ return
129
+ }
130
+
131
+ data.forEach((item, index) => {
132
+ if (typeof item === 'string') {
133
+ console.log(`${item}`)
134
+ } else if (item && typeof item === 'object') {
135
+ const displayName = item.name || item.key || 'unknown'
136
+ console.log(`${index + 1}. ${item.emoji} ${displayName}`)
137
+ } else {
138
+ console.log(`${index + 1}. ${item}`)
139
+ }
140
+ })
141
+ } else if (data && typeof data === 'object') {
142
+ const displayName = data.name || data.key || 'unknown'
143
+ console.log(`${data.emoji} ${displayName}`)
144
+ } else if (data !== undefined && data !== null) {
145
+ console.log(data)
146
+ } else {
147
+ console.log('No result found')
148
+ }
149
+ },
150
+ error: (message: string) => {
151
+ console.error(`❌ Error: ${message}`)
152
+ },
153
+ warning: (message: string) => {
154
+ console.warn(`⚠️ Warning: ${message}`)
155
+ },
156
+ info: (message: string) => {
157
+ console.log(`ℹ️ ${message}`)
158
+ },
159
+ }
160
+ }
161
+
162
+ const output = createOutputFormatter()
163
+
164
+ // Argument parsing
165
+ function parseArgs(args: string[]): ParsedArgs {
166
+ if (args.length === 0) {
167
+ return { command: '', args: [], help: true }
168
+ }
169
+
170
+ const [first, ...rest] = args
171
+
172
+ // Check for help flags
173
+ if (first === '--help' || first === '-h') {
174
+ return { command: '', args: [], help: true }
175
+ }
176
+
177
+ return {
178
+ command: first,
179
+ args: rest,
180
+ help: false,
181
+ }
182
+ }
183
+
184
+ // Command resolution
185
+ function resolveCommand(input: string): Command | null {
186
+ if (!input) {
187
+ return null
188
+ }
189
+
190
+ return (
191
+ COMMANDS.find(cmd => cmd.name === input || cmd.aliases.includes(input)) ||
192
+ null
193
+ )
194
+ }
195
+
196
+ // Validation
197
+ function validateArgs(command: Command, args: string[]): void {
198
+ if (command.requiresArgs && args.length === 0) {
199
+ throw new ValidationError(`Command "${command.name}" requires arguments`)
200
+ }
201
+
202
+ if (!command.requiresArgs && args.length > 0) {
203
+ throw new ValidationError(
204
+ `Command "${command.name}" does not accept arguments`,
205
+ )
206
+ }
207
+ }
208
+
209
+ // Command handlers
210
+ async function runSearch(query?: string): Promise<void> {
211
+ if (!query) {
212
+ throw new ValidationError('Search query is required')
213
+ }
214
+
215
+ try {
216
+ const results = cli.search(query)
217
+ output.success(results)
218
+ } catch (error) {
219
+ output.error(
220
+ `Failed to search for "${query}": ${
221
+ error instanceof Error ? error.message : 'Unknown error'
222
+ }`,
223
+ )
224
+ }
225
+ }
226
+
227
+ async function runEmojify(text?: string): Promise<void> {
228
+ if (!text) {
229
+ throw new ValidationError('Text to emojify is required')
230
+ }
231
+
232
+ try {
233
+ const result = cli.emojify(text)
234
+ output.success(result)
235
+ } catch (error) {
236
+ output.error(
237
+ `Failed to emojify text: ${
238
+ error instanceof Error ? error.message : 'Unknown error'
239
+ }`,
240
+ )
241
+ }
242
+ }
243
+
244
+ async function runUnemojify(text?: string): Promise<void> {
245
+ if (!text) {
246
+ throw new ValidationError('Text to unemojify is required')
247
+ }
248
+
249
+ try {
250
+ const result = cli.unemojify(text)
251
+ output.success(result)
252
+ } catch (error) {
253
+ output.error(
254
+ `Failed to unemojify text: ${
255
+ error instanceof Error ? error.message : 'Unknown error'
256
+ }`,
257
+ )
258
+ }
259
+ }
260
+
261
+ async function runGet(name?: string): Promise<void> {
262
+ if (!name) {
263
+ throw new ValidationError('Emoji name is required')
264
+ }
265
+
266
+ try {
267
+ const result = cli.get(name)
268
+ if (result) {
269
+ output.success(result)
270
+ } else {
271
+ output.warning(`Emoji "${name}" not found`)
272
+ }
273
+ } catch (error) {
274
+ output.error(
275
+ `Failed to get emoji "${name}": ${
276
+ error instanceof Error ? error.message : 'Unknown error'
277
+ }`,
278
+ )
279
+ }
280
+ }
281
+
282
+ async function runHas(name?: string): Promise<void> {
283
+ if (!name) {
284
+ throw new ValidationError('Emoji name is required')
285
+ }
286
+
287
+ try {
288
+ const result = cli.has(name)
289
+ output.success(
290
+ result ? `✅ Emoji "${name}" exists` : `❌ Emoji "${name}" not found`,
291
+ )
292
+ } catch (error) {
293
+ output.error(
294
+ `Failed to check emoji "${name}": ${
295
+ error instanceof Error ? error.message : 'Unknown error'
296
+ }`,
297
+ )
298
+ }
299
+ }
300
+
301
+ async function runFind(name?: string): Promise<void> {
302
+ if (!name) {
303
+ throw new ValidationError('Search term is required')
304
+ }
305
+
306
+ try {
307
+ const result = cli.find(name)
308
+ if (result) {
309
+ output.success(result)
310
+ } else {
311
+ output.warning(`No emojis found for "${name}"`)
312
+ }
313
+ } catch (error) {
314
+ output.error(
315
+ `Failed to find emojis for "${name}": ${
316
+ error instanceof Error ? error.message : 'Unknown error'
317
+ }`,
318
+ )
319
+ }
320
+ }
321
+
322
+ async function runRandom(): Promise<void> {
323
+ try {
324
+ const result = cli.random()
325
+ output.success(result)
326
+ } catch (error) {
327
+ output.error(
328
+ `Failed to get random emoji: ${
329
+ error instanceof Error ? error.message : 'Unknown error'
330
+ }`,
331
+ )
332
+ }
333
+ }
334
+
335
+ // Help and usage information
336
+ function showHelp(): void {
337
+ console.log(`${CONFIG.name} v${CONFIG.version}`)
338
+ console.log(`${CONFIG.description} 💖`)
339
+ console.log('\nUsage:')
340
+ console.log(` ${CONFIG.name} <command> [arguments]`)
341
+ console.log('\nCommands:')
342
+
343
+ COMMANDS.forEach(cmd => {
344
+ const aliasesText =
345
+ cmd.aliases.length > 1 ? ` (${cmd.aliases.join(', ')})` : ''
346
+ console.log(` ${cmd.name}${aliasesText} ${cmd.description}`)
347
+ })
348
+
349
+ console.log('\nExamples:')
350
+ console.log(` ${CONFIG.name} --search "heart"`)
351
+ console.log(` ${CONFIG.name} --get "heart"`)
352
+ console.log(` ${CONFIG.name} --emojify "I love you"`)
353
+ console.log(` ${CONFIG.name} --random`)
354
+ }
355
+
356
+ function showVersion(): void {
357
+ console.log(`${CONFIG.name} v${CONFIG.version}`)
358
+ }
359
+
360
+ // Main execution
361
+ async function run(): Promise<void> {
362
+ try {
363
+ const args = process.argv.slice(2)
364
+ const { command: commandInput, args: commandArgs, help } = parseArgs(args)
365
+
366
+ // Handle global flags
367
+ if (help || !commandInput) {
368
+ showHelp()
369
+ return
370
+ }
371
+
372
+ if (commandInput === '--version' || commandInput === '-v') {
373
+ showVersion()
374
+ return
375
+ }
376
+
377
+ // Resolve and execute command
378
+ const command = resolveCommand(commandInput)
379
+ if (!command) {
380
+ throw new CLIError(
381
+ `Unknown command: ${commandInput}. Use --help for available commands.`,
382
+ )
383
+ }
384
+
385
+ // Validate arguments
386
+ validateArgs(command, commandArgs)
387
+
388
+ // Execute command
389
+ await command.handler(commandArgs[0])
390
+ } catch (error) {
391
+ if (error instanceof CLIError) {
392
+ output.error(error.message)
393
+ process.exit(error.code)
394
+ } else if (error instanceof ValidationError) {
395
+ output.error(error.message)
396
+ output.info('Use --help for usage information')
397
+ process.exit(error.code)
398
+ } else {
399
+ output.error(
400
+ `Unexpected error: ${
401
+ error instanceof Error ? error.message : 'Unknown error'
402
+ }`,
403
+ )
404
+ process.exit(1)
405
+ }
406
+ }
407
+ }
408
+
409
+ // Execute if this file is run directly
410
+ if (import.meta.url === `file://${process.argv[1]}`) {
411
+ run().catch(error => {
412
+ output.error(
413
+ `Failed to run CLI: ${
414
+ error instanceof Error ? error.message : 'Unknown error'
415
+ }`,
416
+ )
417
+ process.exit(1)
418
+ })
419
+ }
420
+ run()
421
+ export default run
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/env node
2
+
3
+ import * as cli from '../src/index.js'
4
+ export default async function run() {
5
+ console.log('Emoji cli')
6
+ const args = process.argv.slice(2)
7
+ const command = args[0]
8
+
9
+ if (!command || command === '--help' || command === '-h') {
10
+ showhelp()
11
+ return
12
+ }
13
+
14
+ switch (command) {
15
+ case '--help':
16
+ case '-h':
17
+ showhelp()
18
+ break
19
+ case '--search':
20
+ runsearch(args[1])
21
+ break
22
+ case '--emojify':
23
+ runemojify(args[1])
24
+ break
25
+ case '--unemojify':
26
+ rununemojify(args[1])
27
+ break
28
+ case '--get':
29
+ runget(args[1])
30
+ break
31
+ case '--has':
32
+ runhas(args[1])
33
+ break
34
+ case '--find':
35
+ case '-f':
36
+ runfind(args[1])
37
+ break
38
+ case '--random':
39
+ case '--rnd':
40
+ case '-r':
41
+ runrandom()
42
+ break
43
+ case '--version':
44
+ case '-v':
45
+ await import('../package.json').then(pkg => {
46
+ console.log(pkg.default.version)
47
+ })
48
+ break
49
+
50
+ default:
51
+ showhelp()
52
+ break
53
+ }
54
+
55
+ function showhelp() {
56
+ console.log('Help:')
57
+ console.log(
58
+ 'Commands: --search --emojify --unemojify --get --has --random --find',
59
+ )
60
+ }
61
+ }
62
+
63
+ function runsearch(args: string) {
64
+ console.log('searching for: ', args)
65
+ cli.search(args)
66
+ console.log(cli.search(args))
67
+ }
68
+
69
+ function runemojify(args: string) {
70
+ console.log(cli.emojify(args))
71
+ }
72
+
73
+ function rununemojify(args: string) {
74
+ console.log(cli.unemojify(args))
75
+ }
76
+
77
+ function runget(args: string) {
78
+ console.log(cli.get(args))
79
+ }
80
+
81
+ function runhas(args: string) {
82
+ console.log(cli.has(args))
83
+ }
84
+
85
+ function runfind(args: string) {
86
+ console.log(cli.find(args))
87
+ }
88
+
89
+ function runrandom() {
90
+ console.log(cli.random())
91
+ }
92
+
93
+ run()