-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.js
executable file
·88 lines (78 loc) · 2.36 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
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const logSymbols = require('log-symbols');
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
const errorNotifier = require('./lib/error-notifier');
const cli = meow(`
Usage
$ onerror <command> [options]
Options
--title, -t Sets the title of the notification.
Default: "An error has occured"
--message, -m Sets the message body of the notification.
Default: "Check the terminal for more information"
--icon, -i Sets an icon. Can be any absolute path.
--sound, -s Defines which sound to use.
Use "mute" to disable default sound notification.
Options: Mute, Basso, Blow, Bottle, Frog, Funk, Glass, Hero,
Morse, Ping, Pop, Purr, Sosumi, Submarine, Tink
Default: Bottle
--error -e A JavaScript code string to customize the content of the
\${error} variable e.g. "error.stderr.substr(0, 9) + '...'"
Default: "error.toString()"
--version -v Displays the version number.
--help -h Displays the help.
Examples
$ onerror "wget unknown-host.xyz"
$ onerror "wget unknown-host.xyz" -s mute
$ onerror "wget unknown-host.xyz" -t Error -m "My error message"
$ onerror "wget unknown-host.xyz" -s Glass -i https://cdn.rawgit.com/npm/logos/31945b5c/npm%20square/n-64.png
`, {
alias: {
h: 'help',
v: 'version'
},
flags: {
title: {
type: 'string',
alias: 't'
},
message: {
type: 'string',
alias: 'm'
},
icon: {
type: 'string',
alias: 'i'
},
sound: {
type: 'string',
alias: 's'
},
error: {
type: 'string',
alias: 'e'
}
}
});
updateNotifier({pkg}).notify();
if (cli.input.length === 0 && cli.flags.v === true) {
cli.showVersion();
}
if (cli.input.length === 0 && cli.flags.h === true) {
cli.showHelp(0);
}
if (cli.input.length !== 1) {
console.log(`\n${logSymbols.error} Invalid input. Please check the help below:`);
cli.showHelp();
}
if (Object.keys(cli.flags).map(key => typeof cli.flags[key]).some(type => type === 'boolean')) {
console.log(`\n${logSymbols.error} Wrong option(s) provided. Please check the help below:`);
cli.showHelp();
}
errorNotifier(cli.input[0], cli.flags)
.catch(error => {
process.exit(error.code);
});