Repository navigation
Expand file tree
/
Copy pathcli.ts
More file actions
55 lines (47 loc) · 1.57 KB
/
Copy pathcli.ts
File metadata and controls
55 lines (47 loc) · 1.57 KB
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
import type { ContribkitConfig } from './types'
import cac from 'cac'
import { version } from '../package.json'
import { run } from './run'
const RE_FILTER = /^(<=?|>=?)(\d+)$/
const cli = cac('contributors-svg')
.version(version)
.help()
cli
.command('[outputDir]', 'Generate contributors SVG')
.option('--width, -w <width>', 'SVG width', { default: 800 })
.option('--fallback-avatar, --fallback <url>', 'Fallback avatar URL')
.option('--force, -f', 'Force regeneration', { default: false })
.option('--name <name>', 'Name')
.option('--filter <filter>', 'Filter contributors')
.option('--output-dir, -o, --dir <dir>', 'Output directory')
.action(async (outputDir: string, options) => {
const config = options as ContribkitConfig
if (outputDir)
config.outputDir = outputDir
if (options.filter)
config.filter = createFilterFromString(options.filter)
await run(config)
})
cli.parse()
/**
* Create filter function from templates like
* - `<10`
* - `>=10`
* @param template
*/
function createFilterFromString(template: string): ContribkitConfig['filter'] {
const match = RE_FILTER.exec(template)
if (!match)
throw new Error(`Unable to parse filter template ${template}`)
const [, op, value] = match
const num = Number.parseInt(value)
if (op === '<')
return s => s.monthlyDollars < num
if (op === '<=')
return s => s.monthlyDollars <= num
if (op === '>')
return s => s.monthlyDollars > num
if (op === '>=')
return s => s.monthlyDollars >= num
throw new Error(`Unable to parse filter template ${template}`)
}