-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.js
executable file
·89 lines (73 loc) · 2.02 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
89
#!/usr/bin/env node
const mri = require('mri')
const { badgen } = require('badgen')
const icons = require('badgen-icons')
const help = `
Usage
$ badgen <options>
Options
-s, --status <text> Status of the badge, right part (required)
-j, --subject <text> Subject of the badge, left part
-c, --color <RGB / ColorName> Color of the status
-f, --flat Use the flat badge style
-i, --icon [<IconName>] Icon to use
-w, --icon-width <Number> Width of the icon if not square
Example
$ badgen --subject test --status ok --color green --icon terminal --flat > test.svg
`
const args = mri(process.argv.slice(2), {
string: ['status', 'subject', 'color', 'icon', 'icon-width'],
boolean: ['flat', 'help', 'version'],
alias: {
s: 'status',
j: 'subject',
c: 'color',
f: 'flat',
i: 'icon',
w: 'icon-width',
h: 'help',
v: 'version'
}
})
if (args.help) {
console.log(help)
process.exit()
}
if (args.version) {
console.log(require('./package.json').version)
process.exit()
}
try {
if (!args.status) {
throw new Error('`--status` is required.')
}
const opts = createBadgenOptions(args)
const svg = badgen(opts)
console.log(svg)
} catch (error) {
console.error(error.message)
process.exit(1)
}
function createBadgenOptions (args) {
const { status, subject = '', color, flat, icon, 'icon-width': iconWidth } = args
const options = {
status,
subject,
color,
flat,
icon,
iconWidth
}
// Normalize flag to keep the last override
Object.keys(options).forEach(key => {
const flag = options[key]
if (Array.isArray(flag)) {
options[key] = flag[flag.length - 1]
}
})
options.style = options.flat && 'flat'
const matchedIcon = icons[options.icon === '' ? options.subject : options.icon]
options.icon = (matchedIcon && matchedIcon.base64) || ''
options.iconWidth = options.iconWidth || (matchedIcon && matchedIcon.width) || null
return options
}